From 7764a981492d27481db9f448a66c3f63ff9b1a58 Mon Sep 17 00:00:00 2001 From: Jannick Kremer Date: Wed, 2 Oct 2024 20:08:24 +0100 Subject: [PATCH] Automatically determine current version from git tags (#45) Determine the current version (git tag), revision and date/time during compilation via shell commands, and add them as preprocessor variables during compilation. --- .github/workflows/main.yml | 31 +++++++++++++++++-------------- .gitignore | 1 - src/QUAboutDialog.cpp | 5 ++--- src/QUMainWindow.cpp | 26 +++++++++++++------------- src/UltraStar-Manager.pro | 23 ++++++----------------- src/latest_version.xml | 1 - src/main.cpp | 8 +++++--- src/main.h | 8 ++++---- 8 files changed, 47 insertions(+), 56 deletions(-) delete mode 100644 src/latest_version.xml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f2c80bc9..b19f0667 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,6 +18,7 @@ jobs: uses: actions/checkout@v4 with: submodules: true + fetch-depth: 0 - name: Install Qt uses: jurplel/install-qt-action@v3 with: @@ -116,6 +117,7 @@ jobs: uses: actions/checkout@v4 with: submodules: true + fetch-depth: 0 - name: Determine Arch run: | if [ "${{ matrix.os }}" = "macos-13" ]; then @@ -139,20 +141,20 @@ jobs: export CFLAGS="-Wno-narrowing -O3" ./compile_libs.sh cp libcld2.dylib ../../../lib/MacOS - - name: Fix Qt lib rpaths # see: https://github.com/orgs/Homebrew/discussions/2823#discussioncomment-2010340) - run: | - install_name_tool -id '@rpath/QtCore.framework/Versions/A/QtCore' $(brew --prefix)/lib/QtCore.framework/Versions/A/QtCore - install_name_tool -id '@rpath/QtGui.framework/Versions/A/QtGui' $(brew --prefix)/lib/QtGui.framework/Versions/A/QtGui - install_name_tool -id '@rpath/QtNetwork.framework/Versions/A/QtNetwork' $(brew --prefix)/lib/QtNetwork.framework/Versions/A/QtNetwork - install_name_tool -id '@rpath/QtWidgets.framework/Versions/A/QtWidgets' $(brew --prefix)/lib/QtWidgets.framework/Versions/A/QtWidgets - install_name_tool -id '@rpath/QtPdf.framework/Versions/A/QtPdf' $(brew --prefix)/lib/QtPdf.framework/Versions/A/QtPdf - install_name_tool -id '@rpath/QtSvg.framework/Versions/A/QtSvg' $(brew --prefix)/lib/QtSvg.framework/Versions/A/QtSvg - install_name_tool -id '@rpath/QtVirtualKeyboard.framework/Versions/A/QtVirtualKeyboard' $(brew --prefix)/lib/QtVirtualKeyboard.framework/Versions/A/QtVirtualKeyboard - install_name_tool -id '@rpath/QtQuick.framework/Versions/A/QtQuick' $(brew --prefix)/lib/QtQuick.framework/Versions/A/QtQuick - install_name_tool -id '@rpath/QtQmlModels.framework/Versions/A/QtQmlModels' $(brew --prefix)/lib/QtQmlModels.framework/Versions/A/QtQmlModels - install_name_tool -id '@rpath/QtQml.framework/Versions/A/QtQml' $(brew --prefix)/lib/QtQml.framework/Versions/A/QtQml - install_name_tool -id '@rpath/QtOpenGL.framework/Versions/A/QtOpenGL' $(brew --prefix)/lib/QtOpenGL.framework/Versions/A/QtOpenGL - install_name_tool -id '@rpath/QtMultimedia.framework/Versions/A/QtMultimedia' $(brew --prefix)/lib/QtMultimedia.framework/Versions/A/QtMultimedia + - name: Fix Qt lib rpaths # see: https://github.com/orgs/Homebrew/discussions/2823#discussioncomment-2010340) + run: | + install_name_tool -id '@rpath/QtCore.framework/Versions/A/QtCore' $(brew --prefix)/lib/QtCore.framework/Versions/A/QtCore + install_name_tool -id '@rpath/QtGui.framework/Versions/A/QtGui' $(brew --prefix)/lib/QtGui.framework/Versions/A/QtGui + install_name_tool -id '@rpath/QtNetwork.framework/Versions/A/QtNetwork' $(brew --prefix)/lib/QtNetwork.framework/Versions/A/QtNetwork + install_name_tool -id '@rpath/QtWidgets.framework/Versions/A/QtWidgets' $(brew --prefix)/lib/QtWidgets.framework/Versions/A/QtWidgets + install_name_tool -id '@rpath/QtPdf.framework/Versions/A/QtPdf' $(brew --prefix)/lib/QtPdf.framework/Versions/A/QtPdf + install_name_tool -id '@rpath/QtSvg.framework/Versions/A/QtSvg' $(brew --prefix)/lib/QtSvg.framework/Versions/A/QtSvg + install_name_tool -id '@rpath/QtVirtualKeyboard.framework/Versions/A/QtVirtualKeyboard' $(brew --prefix)/lib/QtVirtualKeyboard.framework/Versions/A/QtVirtualKeyboard + install_name_tool -id '@rpath/QtQuick.framework/Versions/A/QtQuick' $(brew --prefix)/lib/QtQuick.framework/Versions/A/QtQuick + install_name_tool -id '@rpath/QtQmlModels.framework/Versions/A/QtQmlModels' $(brew --prefix)/lib/QtQmlModels.framework/Versions/A/QtQmlModels + install_name_tool -id '@rpath/QtQml.framework/Versions/A/QtQml' $(brew --prefix)/lib/QtQml.framework/Versions/A/QtQml + install_name_tool -id '@rpath/QtOpenGL.framework/Versions/A/QtOpenGL' $(brew --prefix)/lib/QtOpenGL.framework/Versions/A/QtOpenGL + install_name_tool -id '@rpath/QtMultimedia.framework/Versions/A/QtMultimedia' $(brew --prefix)/lib/QtMultimedia.framework/Versions/A/QtMultimedia - name: Build plugins run: | cd src/plugins/audiotag/ @@ -202,6 +204,7 @@ jobs: uses: actions/checkout@v4 with: submodules: true + fetch-depth: 0 - name: Install Qt uses: jurplel/install-qt-action@v3 with: diff --git a/.gitignore b/.gitignore index 65c036ca..bcbc555a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ *.stash *.user bin -src/version.h src/tmp src/*.autosave src/ui diff --git a/src/QUAboutDialog.cpp b/src/QUAboutDialog.cpp index 0cec992a..ac2d8f80 100644 --- a/src/QUAboutDialog.cpp +++ b/src/QUAboutDialog.cpp @@ -1,6 +1,5 @@ #include "QUAboutDialog.h" #include "main.h" -#include "version.h" #include #include #include @@ -107,10 +106,10 @@ QUAboutDialog::QUAboutDialog(QWidget *parent): QDialog(parent) { credits->setHtml(creditsText); - QDateTime dateTime = QDateTime::fromString(QString(date_time), "dd.MM.yyyy HH:mm"); + QDateTime dateTime = QDateTime::fromString(QString(xstr(DATE_TIME)), "dd.MM.yyyy HH:mm"); versionLbl->setText(QString(tr("Version: %1
Date: %2")) - .arg(QString(revision), dateTime.toString("yyyy-MM-dd, HH:mm"))); + .arg(QString(xstr(REVISION)), dateTime.toString("yyyy-MM-dd, HH:mm"))); resetText(); credits->document()->setDefaultStyleSheet("a {color: white}"); diff --git a/src/QUMainWindow.cpp b/src/QUMainWindow.cpp index 693ebdab..27458ae5 100644 --- a/src/QUMainWindow.cpp +++ b/src/QUMainWindow.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "QUSongItem.h" #include "QUDetailItem.h" @@ -993,9 +994,10 @@ void QUMainWindow::aboutMediaInfo() { } void QUMainWindow::checkForUpdate(bool silent) { - int currentVersion = MAJOR_VERSION*100 + MINOR_VERSION*10 + PATCH_VERSION; + QString currentVersion = QString(xstr(VERSION)); + int currentVersionNum = QString(currentVersion).remove('.').toInt(); - QUrl url("https://github.com/UltraStar-Deluxe/UltraStar-Manager/blob/master/src/latest_version.xml"); + QUrl url("https://api.github.com/repos/UltraStar-Deluxe/UltraStar-Manager/releases/latest"); QNetworkAccessManager *m_NetworkMngr = new QNetworkAccessManager(this); QNetworkReply *reply = m_NetworkMngr->get(QNetworkRequest(url)); @@ -1027,20 +1029,18 @@ void QUMainWindow::checkForUpdate(bool silent) { return; } - tmp.write(reply->readAll()); - tmp.seek(0); - QString line = QString(tmp.readLine()); + QJsonDocument json = QJsonDocument::fromJson(reply->readAll()); + QString line = json["tag_name"].toString(); QString latestVersionString = line; - int latestVersion = line.remove('.').toInt(); + int latestVersion = line.remove('.').remove('v').toInt(); delete reply; - tmp.remove(); - if (currentVersion < latestVersion) { + if (currentVersionNum < latestVersion) { QUMessageBox::information(this, tr("Update check successful."), - QString(tr("UltraStar Manager %1.%2.%3 is outdated.

" - "Download the most recent UltraStar-Manager %4 here.")) - .arg(MAJOR_VERSION).arg(MINOR_VERSION).arg(PATCH_VERSION) + QString(tr("UltraStar Manager %1 is outdated.

" + "Download the most recent UltraStar-Manager %2 here.")) + .arg(currentVersion) .arg(latestVersionString), BTN << ":/marks/accept.png" << "OK", 240); @@ -1051,8 +1051,8 @@ void QUMainWindow::checkForUpdate(bool silent) { QSettings settings; int result = QUMessageBox::information(this, tr("Update check successful."), - QString(tr("UltraStar Manager %1.%2.%3 is up to date!")) - .arg(MAJOR_VERSION).arg(MINOR_VERSION).arg(PATCH_VERSION), + QString(tr("UltraStar Manager %1 is up to date!")) + .arg(currentVersion), BTN << ":/marks/accept.png" << tr("OK. I will check again later.") << ":/marks/accept.png" << tr("OK. Check automatically on startup."), 240); diff --git a/src/UltraStar-Manager.pro b/src/UltraStar-Manager.pro index a3739d44..5f59e2bc 100644 --- a/src/UltraStar-Manager.pro +++ b/src/UltraStar-Manager.pro @@ -241,6 +241,9 @@ INCLUDEPATH += . \ remoteimages \ webinfo +DEFINES += REVISION=$$system(git describe --always --tags --match "[0-9].[0-9].[0-9]") \ + VERSION=$$system(git describe --tags --abbrev=0 --match "[0-9].[0-9].[0-9]") + win32 { INCLUDEPATH += ../include/cld2/public \ ../include/taglib \ @@ -256,6 +259,8 @@ win32 { -lzlib RC_ICONS += UltraStar-Manager.ico + + DEFINES += DATE_TIME=\"$$quote($$system(date /T) $$system(time /T))\" } macx { @@ -291,26 +296,10 @@ unix:!macx { QMAKE_LFLAGS += '-Wl,-rpath,\'\$$ORIGIN\'' } -QMAKE_EXTRA_TARGETS += revtarget -PRE_TARGETDEPS += version.h -revtarget.target = version.h - -win32 { - revtarget.commands = "$$system(echo 'const char *revision = \"$$system(git describe --always)\";' > $$revtarget.target)" - revtarget.commands += "$$system(echo 'const char *date_time = \"$$system(date /T) $$system(time /T)\";' >> $$revtarget.target)" -} - unix { -revtarget.commands = @echo \ - "const char *revision = \\\"`git describe --always`\\\"\\; \ - const char *date_time = \\\"`date +'%d.%m.%Y %H:%M'`\\\"\\;" \ - > $${PWD}/$$revtarget.target + DEFINES += DATE_TIME=\"$$quote($$system(date '+%d.%m.%Y\ %H:%M'))\" } -revtarget.depends = $$SOURCES \ - $$HEADERS \ - $$FORMS - unix:!macx { QMAKE_POST_LINK += $$sprintf($${QMAKE_MKDIR_CMD}, $$shell_path($${DESTDIR}/lib/)) $$escape_expand(\\n\\t) } diff --git a/src/latest_version.xml b/src/latest_version.xml deleted file mode 100644 index fe4e75fb..00000000 --- a/src/latest_version.xml +++ /dev/null @@ -1 +0,0 @@ -1.8.3 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fdb378d2..a465d4c6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,11 +45,13 @@ int main(int argc, char *argv[]) { versionFont.setPixelSize(24); painter.setFont(versionFont); + QStringList version_parts = QString(xstr(VERSION)).split("."); + QString maj_min_version = version_parts[0] + "." + version_parts[1]; + painter.drawText( 0, 0, 418, 130, Qt::AlignRight | Qt::AlignBottom, - QString("%1.%2").arg(MAJOR_VERSION).arg(MINOR_VERSION) - ); + maj_min_version); painter.end(); QSplashScreen splash(canvas); @@ -151,7 +153,7 @@ void initLanguage(QApplication &app, QTranslator &trContent, QTranslator &trQt, monty->initMessages(); - s.showMessage(QString(QObject::tr("%1.%2.%3 is loading...")).arg(MAJOR_VERSION).arg(MINOR_VERSION).arg(PATCH_VERSION), Qt::AlignBottom | Qt::AlignRight, Qt::white); + s.showMessage(QString(QObject::tr("%1 is loading...")).arg(xstr(VERSION)), Qt::AlignBottom | Qt::AlignRight, Qt::white); // message needs to be here because it can be translated only after installing the translator if(!settingFound) diff --git a/src/main.h b/src/main.h index 23d15609..d661f787 100644 --- a/src/main.h +++ b/src/main.h @@ -1,12 +1,12 @@ #ifndef MAIN_H_ #define MAIN_H_ -#define MAJOR_VERSION 0x0001 -#define MINOR_VERSION 0x0009 -#define PATCH_VERSION 0x0000 - #define WIP_TEXT "" //" *work in progress* -> Please do not distribute." //#define WIP_VERSION //#define RC_VERSION +// Stringifiers needed to parse the VERSION preprocessor define as string +#define xstr(a) astr(a) +#define astr(a) #a + #endif /*MAIN_H_*/