C++ (Qt)VERSIONINFO = $$system(git log --pretty=format:\\\"%cd\\ %H\\\" -n1 | cat)isEmpty(VERSIONINFO){ VERSIONINFO =1.0}DEFINES += VERSIONINFO=$$quote(\\\"$$VERSIONINFO\\\")
Bash$ git log --pretty=format:"%cd %H" -n1Sat Dec 15 23:29:53 2012 +0200 577dab733c05e556e3e837721f54a989e2f2478e
C++ (Qt)#include <QtCore/QCoreApplication>#include <QtCore/QStringList>#include <QtCore/QFile>#include <QtCore/QTextStream>#include <QDebug> int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Q_ASSERT(argc > 3); const QString splitter = QLatin1String("="); const QString inputName = a.arguments().at(1); const QString outputName = a.arguments().at(2); QString content; { QFile input(inputName); if (!input.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "Unable to open file" << inputName; return 1; } QTextStream inStream(&input); content = inStream.readAll(); } for (int i = 3; i < argc; ++i) { QString arg = a.arguments().at(i); QStringList params = arg.split(splitter, QString::SkipEmptyParts); Q_ASSERT(params.size() == 2); QString paramName = params.at(0); QString dest = params.at(1); QString source = QString(QLatin1String("%%1%")).arg(paramName); content = content.replace(source, dest); } QFile output(outputName); if (!output.open(QIODevice::WriteOnly | QIODevice::Text)) { qWarning() << "Unable to open file" << outputName; return 1; } QTextStream outStream(&output); outStream << content; return 0;}
Javascript MAJOR_VERSION = 1 MINOR_VERSION = 3 VERSION = $${MAJOR_VERSION}.$${MINOR_VERSION}.0 VERSION_HEADER = version.h version.target = $$VERSION_HEADER version.commands = $$PATH_TO_YOUR_TOOL/your_tool \ $$PATH_TO_YOUR_PATTERN/version.h.in \ ./$$VERSION_HEADER \ MAJOR_VERSION=$$MAJOR_VERSION \ MINOR_VERSION=$$MINOR_VERSION QMAKE_EXTRA_TARGETS += version PRE_TARGETDEPS += $$VERSION_HEADER
C++ (Qt)#ifndef VERSION_H#define VERSION_H #define MAJOR_VERSION %MAJOR_VERSION%#define MINOR_VERSION %MINOR_VERSION% #endif //VERSION_H
C++ (Qt)# pro файл # это центральное место хранения версии, 1.2.0 в данном случаеNVER1=1NVER2=2NVER3=0 VERSION=$$sprintf("%1.%2.%3", $$NVER1, $$NVER2, $$NVER3) DEFINES += NVER1=$$NVER1DEFINES += NVER2=$$NVER2DEFINES += NVER3=$$NVER3DEFINES += NVER_STRING=$$sprintf("\"\\\"%1\\\"\"", $$VERSION)
C++ (Qt)// rc файл #define stringify(v1) #v1#define quote(v1) stringify(v1) #define NVER NVER1.NVER2.NVER3 ...1 VERSIONINFO PRODUCTVERSION NVER1, NVER2, NVER3, 0...BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "040904e4" BEGIN ... VALUE "FileVersion", quote(NVER) ...
C++ (Qt)// в кодеui->label->setText(NVER_STRING);