Clean up version.cpp, no functional changes.

This commit is contained in:
sawinglogz 2020-01-16 16:06:11 -05:00
parent 1f4bb2d435
commit 768f32821a
2 changed files with 47 additions and 62 deletions

View File

@ -8,28 +8,19 @@
#include "version.h" #include "version.h"
#include "git_info.h" #include "git_info.h"
#include "SleepLib/common.h"
#include <QRegularExpression> #include <QRegularExpression>
#define VERSION "1.1.0-beta-1" #define VERSION "1.1.0-beta-1"
#ifdef Q_OS_MAC // TODO: add preprocessor macros to build full version number including build metadata, accounting for tarball/non-git builds.
const QString PlatformString = "MacOSX"; static const Version s_Version(VERSION "+" + GIT_BRANCH + "-" + GIT_REVISION);
#elif defined(Q_OS_WIN32)
const QString PlatformString = "Win32";
#elif defined(Q_OS_WIN64)
const QString PlatformString = "Win64";
#elif defined(Q_OS_LINUX)
const QString PlatformString = "Linux";
#elif defined(Q_OS_HAIKU)
const QString PlatformString = "Haiku";
#endif
// Technically this is the date and time that version.cpp was compiled, but since // Technically this is the date and time that version.cpp was compiled, but since
// it gets recompiled whenever git_info.h changes, it's about as close as we can // it gets recompiled whenever git_info.h changes, it's about as close as we can
// come without forcing recompiles every single build. // come without forcing recompiles every single build.
static const QString s_BuildDateTime = __DATE__ " " __TIME__; static const QString s_BuildDateTime = __DATE__ " " __TIME__;
QString getPrereleaseSuffix() QString getPrereleaseSuffix()
{ {
QString suffix; QString suffix;
@ -54,42 +45,17 @@ const QString & getBuildDateTime()
return s_BuildDateTime; return s_BuildDateTime;
} }
// TODO: add preprocessor macros to build full version number including build metadata, accounting for tarball/non-git builds.
static const Version s_Version(VERSION "+" + GIT_BRANCH + "-" + GIT_REVISION);
const Version & getVersion() const Version & getVersion()
{ {
return s_Version; return s_Version;
} }
Version::Version(const QString & version_string) : mString(version_string), mIsValid(false)
{
ParseSemanticVersion();
FixLegacyVersions();
}
Version::operator const QString &() const
{
return toString();
}
const QString & Version::toString() const
{
return mString;
}
// Alternate formatting of the version string for display or logging // Alternate formatting of the version string for display or logging
const QString Version::minimalString() const const QString Version::minimalString() const
{ {
return toString().section("+", 0, 0); return toString().section("+", 0, 0);
} }
const QString & Version::getBuildMetadata() const
{
return mBuild;
}
const QString Version::displayString() const const QString Version::displayString() const
{ {
if (IsReleaseVersion()) if (IsReleaseVersion())
@ -110,7 +76,50 @@ const QString Version::PrereleaseType() const
return type.toLower(); return type.toLower();
} }
// Parse a version string as specified by Semantic Versioning 2.0.0, see https://semver.org/spec/v2.0.0.html // Deal with non-Semantic-Versioning numbers used before 1.1.0-beta-2 to make sure they
// will have proper (lower) precedence compared to later versions.
//
// TODO: THIS CAN PROBABLY BE REMOVED AFTER THE RELEASE OF 1.1.0, since the release
// version will take precedence over all 1.1.0 prereleases, as well as 1.0.1 of any
// release status.
//
// Right now we just need to make sure that 1.1.0-beta versions take precedence over
// 1.1.0-testing.
void Version::FixLegacyVersions()
{
if (mIsValid) {
// Replace prerelease "testing" with "alpha" for backwards compatibility with 1.1.0-testing-*
// versions: otherwise "testing" would take precedence over "beta".
mPrerelease.replace("testing", "alpha");
// Technically the use of "r1" in "1.0.1-r1" could also be corrected, as the code
// will incorrectly consider that release version to be a prerelease, but it doesn't
// matter because 1.1.0 and later will take precedence either way.
}
}
// ===================================================================================================
// Version class for parsing and comparing version strings as specified by Semantic Versioning 2.0.0
// See https://semver.org/spec/v2.0.0.html
Version::Version(const QString & version_string) : mString(version_string), mIsValid(false)
{
ParseSemanticVersion();
FixLegacyVersions();
}
Version::operator const QString &() const
{
return toString();
}
const QString & Version::toString() const
{
return mString;
}
// Parse a version string as specified by Semantic Versioning 2.0.0.
void Version::ParseSemanticVersion() void Version::ParseSemanticVersion()
{ {
// Use a C++11 raw string literal to keep the regular expression (mostly) legible. // Use a C++11 raw string literal to keep the regular expression (mostly) legible.
@ -136,28 +145,6 @@ void Version::ParseSemanticVersion()
// valid versions. // valid versions.
} }
// Deal with non-Semantic-Versioning numbers used before 1.1.0-beta-2 to make sure they
// will have proper (lower) precedence compared to later versions.
//
// TODO: THIS CAN PROBABLY BE REMOVED AFTER THE RELEASE OF 1.1.0, since the release
// version will take precedence over all 1.1.0 prereleases, as well as 1.0.1 of any
// release status.
//
// Right now we just need to make sure that 1.1.0-beta versions take precedence over
// 1.1.0-testing.
void Version::FixLegacyVersions()
{
if (mIsValid) {
// Replace prerelease "testing" with "alpha" for backwards compatibility with 1.1.0-testing-*
// versions: otherwise "testing" would take precedence over "beta".
mPrerelease.replace("testing", "alpha");
// Technically the use of "r1" in "1.0.1-r1" could also be corrected, as the code
// will incorrectly consider that release version to be a prerelease, but it doesn't
// matter because 1.1.0 and later will take precedence either way.
}
}
// Compare two version instances in accordance with Semantic Versionin 2.0.0 precedence rules. // Compare two version instances in accordance with Semantic Versionin 2.0.0 precedence rules.
int Version::Compare(const Version & a, const Version & b) int Version::Compare(const Version & a, const Version & b)
{ {

View File

@ -33,8 +33,6 @@ public:
const QString displayString() const; const QString displayString() const;
//!brief Returns the version string without any build metadata //!brief Returns the version string without any build metadata
const QString minimalString() const; const QString minimalString() const;
//!brief Returns the build metadata
const QString & getBuildMetadata() const;
protected: protected:
const QString mString; const QString mString;