23:13
:16
2018
NSIS versionning
- Resume:
Tutorial to easily manage NSIS versionning from the source code without extra plugins or scripts.
- License:
BSD License
- Copyrights:
Renan Lavarec

Tutorial to easily manage NSIS versioning from the source code without extra plugins or scripts

This tutorial will show you how to easily manage versioning in one place in the source.

Dependencies:

  • NSIS > V2.39

Versioning file:

Create a file with the versioning inside. This file will be use to generate the versioning of the executable, but also to build the installer.

Example: C++

// VersionInfos.h

////--------------------------------------------------- VERSIONS

#define PRODUCT_NAME        "ProjectName for users"
#define PRODUCT_NAME_EXE    "ProjectName"
#define PRODUCT_NAME_DIR    "ProjectName"
#define PRODUCT_PUBLISHER   "YourCompany"
#define PRODUCT_WEB_SITE    "www.yourcompany.com"
#define PRODUCT_YEAR        "2018"
#define VERSION_MAJOR       1
#define VERSION_MINOR       0
#define VERSION_BUG_FIX     2
#define BUILD_NUMBER        0
#define PRODUCT_TRADEMARKS  "All Rights Reserved"


// "x86" or "x64"
#define PRODUCT_ARCH_TYPE   "x64"


////--------------------------------------------------- UTILS
#define TR_STRINGIFY(x) #x
#define TR_TOSTRING(x) TR_STRINGIFY(x)
#define TR_SPACE " "
#define TR_DOT "."
#define TR_COPYRIGHT "Copyright © "


////--------------------------------------------------- GENERATED INFOS

#define PRODUCT_COPYRIGHT   TR_COPYRIGHT TR_SPACE PRODUCT_YEAR TR_SPACE PRODUCT_PUBLISHER
#define PRODUCT_VERSION     TR_TOSTRING(VERSION_MAJOR) TR_DOT TR_TOSTRING(VERSION_MINOR) TR_DOT TR_TOSTRING(VERSION_BUG_FIX)

NSIS file:

Inside the NSIS file, use the function !searchparse

Example: nsis

; Install.nsis

; Get information from "the file of versioning".
; Take care of the spaces inside the string to mach exactly the one inside the file of versioning.

!searchparse /file ../VersionInfos.h `#define PRODUCT_NAME        "` PRODUCT_NAME `"`
!searchparse /file ../VersionInfos.h `#define PRODUCT_NAME_EXE    "` PRODUCT_NAME_EXE `"`
!searchparse /file ../VersionInfos.h `#define PRODUCT_NAME_DIR    "` PRODUCT_NAME_DIR `"`
!searchparse /file ../VersionInfos.h `#define PRODUCT_PUBLISHER   "` PRODUCT_PUBLISHER `"`
!searchparse /file ../VersionInfos.h `#define PRODUCT_WEB_SITE    "` PRODUCT_WEB_SITE `"`
!searchparse /file ../VersionInfos.h `#define PRODUCT_YEAR        "` PRODUCT_YEAR `"`
!searchparse /file ../VersionInfos.h `#define VERSION_MAJOR       ` VERSION_MAJOR ``
!searchparse /file ../VersionInfos.h `#define VERSION_MINOR       ` VERSION_MINOR ``
!searchparse /file ../VersionInfos.h `#define VERSION_BUG_FIX     ` VERSION_BUG_FIX ``
!searchparse /file ../VersionInfos.h `#define BUILD_NUMBER        ` BUILD_NUMBER ``
!searchparse /file ../VersionInfos.h `#define PRODUCT_ARCH_TYPE   "` PRODUCT_ARCH_TYPE `"`

; HM NIS Edit Wizard helper defines

!define PRODUCT_EXE_SRC "stage\Release_${PRODUCT_ARCH_TYPE}"

!define PRODUCT_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_BUG_FIX}"
!define PRODUCT_VERSION_FULL "${PRODUCT_VERSION}.${BUILD_NUMBER}"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_NAME_EXE}.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

RC file:

Inside the RC file, you can add a section like this.

Example: Windows RC (Resource Files): C++


/////////////////////////////////////////////////////////////////////////////
//
// Version
//

#include <windows.h>

////--------------------------------------------------- UTF-8
#pragma code_page(65001)

////--------------------------------------------------- VERSION INFOS
#include "VersionInfos.h"

////--------------------------------------------------- RC INFOS

#define TR_END_STR "\0"

#define RC_FILEVERSION             VERSION_MAJOR,VERSION_MINOR,VERSION_BUG_FIX,BUILD_NUMBER
#define RC_FILEVERSION_STR         TR_TOSTRING(VERSION_MAJOR) TR_DOT TR_TOSTRING(VERSION_MINOR) TR_DOT TR_TOSTRING(VERSION_BUG_FIX) TR_DOT TR_TOSTRING(BUILD_NUMBER) TR_END_STR
            
#define RC_PRODUCTVERSION          VERSION_MAJOR,VERSION_MINOR,VERSION_BUG_FIX,BUILD_NUMBER
#define RC_PRODUCTVERSION_STR      TR_TOSTRING(VERSION_MAJOR) TR_DOT TR_TOSTRING(VERSION_MINOR) TR_DOT TR_TOSTRING(VERSION_BUG_FIX) TR_END_STR



VS_VERSION_INFO VERSIONINFO
FILEVERSION     RC_FILEVERSION
PRODUCTVERSION  RC_PRODUCTVERSION
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
    BLOCK "040904E4"
    BEGIN
        VALUE "CompanyName",        PRODUCT_PUBLISHER
        VALUE "FileDescription",    PRODUCT_NAME
        VALUE "FileVersion",        RC_FILEVERSION_STR
        VALUE "InternalName",       PRODUCT_NAME_EXE
        VALUE "LegalCopyright",     PRODUCT_COPYRIGHT
        VALUE "LegalTrademarks1",   PRODUCT_TRADEMARKS
        VALUE "LegalTrademarks2",   PRODUCT_TRADEMARKS
        VALUE "ProductName",        PRODUCT_NAME_EXE
        VALUE "ProductVersion",     RC_PRODUCTVERSION_STR
    END
    END

    BLOCK "VarFileInfo"
    BEGIN
    VALUE "Translation", 0x409, 1252
    END
END