Fix version checking (#4329)
https://github.com/Neargye/semver * Use semver library for version downgrade checking * Add test validating our current version is valid semver
This commit is contained in:
@@ -768,6 +768,9 @@ endif ()
|
||||
|
||||
target_include_directories(${LIBRARY_PROJECT} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_BINARY_DIR}/autogen/)
|
||||
|
||||
# semver dependency https://github.com/Neargye/semver
|
||||
target_include_directories(${LIBRARY_PROJECT} PUBLIC ${CMAKE_SOURCE_DIR}/lib/semver/include)
|
||||
|
||||
if (WinToast_FOUND)
|
||||
target_link_libraries(${LIBRARY_PROJECT}
|
||||
PUBLIC
|
||||
|
||||
@@ -3,6 +3,27 @@
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
|
||||
/**
|
||||
* Valid version formats, in order of latest to oldest
|
||||
*
|
||||
* Stable:
|
||||
* - 2.4.0
|
||||
*
|
||||
* Release candidate:
|
||||
* - 2.4.0-rc.3
|
||||
* - 2.4.0-rc.2
|
||||
* - 2.4.0-rc
|
||||
*
|
||||
* Beta:
|
||||
* - 2.4.0-beta.3
|
||||
* - 2.4.0-beta.2
|
||||
* - 2.4.0-beta
|
||||
*
|
||||
* Alpha:
|
||||
* - 2.4.0-alpha.3
|
||||
* - 2.4.0-alpha.2
|
||||
* - 2.4.0-alpha
|
||||
**/
|
||||
#define CHATTERINO_VERSION "2.4.0"
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
|
||||
+25
-33
@@ -15,6 +15,7 @@
|
||||
#include <QMessageBox>
|
||||
#include <QProcess>
|
||||
#include <QRegularExpression>
|
||||
#include <semver/semver.hpp>
|
||||
|
||||
namespace chatterino {
|
||||
namespace {
|
||||
@@ -23,37 +24,6 @@ namespace {
|
||||
return getSettings()->betaUpdates ? "beta" : "stable";
|
||||
}
|
||||
|
||||
/// Checks if the online version is newer or older than the current version.
|
||||
bool isDowngradeOf(const QString &online, const QString ¤t)
|
||||
{
|
||||
static auto matchVersion =
|
||||
QRegularExpression(R"((\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?)");
|
||||
|
||||
// Versions are just strings, they don't need to follow a specific
|
||||
// format so we can only assume if one version is newer than another
|
||||
// one.
|
||||
|
||||
// We match x.x.x.x with each version level being optional.
|
||||
|
||||
auto onlineMatch = matchVersion.match(online);
|
||||
auto currentMatch = matchVersion.match(current);
|
||||
|
||||
for (int i = 1; i <= 4; i++)
|
||||
{
|
||||
if (onlineMatch.captured(i).toInt() <
|
||||
currentMatch.captured(i).toInt())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (onlineMatch.captured(i).toInt() >
|
||||
currentMatch.captured(i).toInt())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Updates::Updates()
|
||||
@@ -71,6 +41,28 @@ Updates &Updates::instance()
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// Checks if the online version is newer or older than the current version.
|
||||
bool Updates::isDowngradeOf(const QString &online, const QString ¤t)
|
||||
{
|
||||
semver::version onlineVersion;
|
||||
if (!onlineVersion.from_string_noexcept(online.toStdString()))
|
||||
{
|
||||
qCWarning(chatterinoUpdate) << "Unable to parse online version"
|
||||
<< online << "into a proper semver string";
|
||||
return false;
|
||||
}
|
||||
|
||||
semver::version currentVersion;
|
||||
if (!currentVersion.from_string_noexcept(current.toStdString()))
|
||||
{
|
||||
qCWarning(chatterinoUpdate) << "Unable to parse current version"
|
||||
<< current << "into a proper semver string";
|
||||
return false;
|
||||
}
|
||||
|
||||
return onlineVersion < currentVersion;
|
||||
}
|
||||
|
||||
const QString &Updates::getCurrentVersion() const
|
||||
{
|
||||
return currentVersion_;
|
||||
@@ -313,8 +305,8 @@ void Updates::checkForUpdates()
|
||||
if (this->currentVersion_ != this->onlineVersion_)
|
||||
{
|
||||
this->setStatus_(UpdateAvailable);
|
||||
this->isDowngrade_ =
|
||||
isDowngradeOf(this->onlineVersion_, this->currentVersion_);
|
||||
this->isDowngrade_ = Updates::isDowngradeOf(
|
||||
this->onlineVersion_, this->currentVersion_);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -24,6 +24,8 @@ public:
|
||||
// fourtf: don't add this class to the application class
|
||||
static Updates &instance();
|
||||
|
||||
static bool isDowngradeOf(const QString &online, const QString ¤t);
|
||||
|
||||
void checkForUpdates();
|
||||
const QString &getCurrentVersion() const;
|
||||
const QString &getOnlineVersion() const;
|
||||
|
||||
@@ -107,6 +107,9 @@ AboutPage::AboutPage()
|
||||
addLicense(form.getElement(), "magic_enum",
|
||||
"https://github.com/Neargye/magic_enum",
|
||||
":/licenses/magic_enum.txt");
|
||||
addLicense(form.getElement(), "semver",
|
||||
"https://github.com/Neargye/semver",
|
||||
":/licenses/semver.txt");
|
||||
}
|
||||
|
||||
// Attributions
|
||||
|
||||
Reference in New Issue
Block a user