Automatically extract exe version of file to be inserted into NSIS installer metadata

I'm trying to remove another step in the process of releasing the application, automatically getting version information from my executable file (in this case, a .NET application).

Up to this point, I have managed to gain limited knowledge of NSIS, but I quickly find out that this is not enough.

Step 1: declare version information in the executable

In AssemblyInfo.cs, I declare [assembly: AssemblyVersion("1.0.0.1")] . This successfully makes the version information displayed in the compiled executable (in the "File Version" and "Product Version" sections).

Step 2. Extract version information from the executable

According to this article, in the "GetFileVersion" article, importing "FileFunc.nsh" allows you to get version information from an executable file.

Used code:

 Section Var /GLOBAL version ${GetFileVersion} "C:\test.exe" $version ... SectionEnd 

Step 3. Checking the contents of the function call

Based on section 5.1.7 of the documentation , I should be able to print on the command line at compile time using "! Echo". The difference between printing the contents of a variable (or constant, etc.) still confuses me, so I tried all four of these options:

 !echo $version !echo "$version" !echo "${version}" !echo ${version} 

It leads to:

 $version (InstallScript.nsi:15) $version (InstallScript.nsi:16) ${version} (InstallScript.nsi:17) ${version} (InstallScript.nsi:18) 

Step 4: Declare Installer Metadata

Based on section 4.8.3 , I should be able to add installer metadata using VIProductVersion and VIAddVersionKey .

 VIProductVersion $version VIAddVersionKey "FileVersion" "$version" 

In the built-in installer, this adds the string "$ version" to the specified fields.


Is there an equivalent ToString() in NSIS? How can I access the contents of a variable? Does printing a variable name mean it has no content? How to check if GetFileVersion is called GetFileVersion , is executed correctly and returns a value?

+6
source share
2 answers

Edit: NSIS v3 now includes preprocessor instructions !getdllversion , you only need a GetVersionLocal workaround if you are still using NSIS v2.

NSIS 2.47 has plans: getdllversion local , now you should use this workaround:

 outfile test.exe requestexecutionlevel user !macro GetVersionLocal file basedef !verbose push !verbose 1 !tempfile _GetVersionLocal_nsi !tempfile _GetVersionLocal_exe !appendfile "${_GetVersionLocal_nsi}" 'Outfile "${_GetVersionLocal_exe}"$\nRequestexecutionlevel user$\n' !appendfile "${_GetVersionLocal_nsi}" 'Section$\n!define D "$"$\n!define N "${D}\n"$\n' !appendfile "${_GetVersionLocal_nsi}" 'GetDLLVersion "${file}" $2 $4$\n' !appendfile "${_GetVersionLocal_nsi}" 'IntOp $1 $2 / 0x00010000$\nIntOp $2 $2 & 0x0000FFFF$\n' !appendfile "${_GetVersionLocal_nsi}" 'IntOp $3 $4 / 0x00010000$\nIntOp $4 $4 & 0x0000FFFF$\n' !appendfile "${_GetVersionLocal_nsi}" 'FileOpen $0 "${_GetVersionLocal_nsi}" w$\nStrCpy $9 "${N}"$\n' !appendfile "${_GetVersionLocal_nsi}" 'FileWrite $0 "!define ${basedef}1 $1$9"$\nFileWrite $0 "!define ${basedef}2 $2$9"$\n' !appendfile "${_GetVersionLocal_nsi}" 'FileWrite $0 "!define ${basedef}3 $3$9"$\nFileWrite $0 "!define ${basedef}4 $4$9"$\n' !appendfile "${_GetVersionLocal_nsi}" 'FileClose $0$\nSectionend$\n' !system '"${NSISDIR}\makensis" -NOCD -NOCONFIG "${_GetVersionLocal_nsi}"' = 0 !system '"${_GetVersionLocal_exe}" /S' = 0 !delfile "${_GetVersionLocal_exe}" !undef _GetVersionLocal_exe !include "${_GetVersionLocal_nsi}" !delfile "${_GetVersionLocal_nsi}" !undef _GetVersionLocal_nsi !verbose pop !macroend !insertmacro GetVersionLocal "$%windir%\Explorer.exe" MyVer_ VIProductVersion "${MyVer_1}.${MyVer_2}.${MyVer_3}.${MyVer_4}" VIAddVersionKey "FileVersion" "${MyVer_1}.${MyVer_2}.${MyVer_3}.${MyVer_4}" page instfiles section sectionend 

This macro:

  • Creates a temporary .nsi file
  • Compiles a temporary .nsi executable
  • Starts a temporary .exe
  • Deletes two files (.nsi and .exe)
  • Returns an array containing version information of the specified executable file.
+11
source

Updated information:

NSIS now includes the getdllversion command, which does the same. Using:

 !getdllversion "$%windir%\explorer.exe" expv_ !echo "Explorer.exe version is ${expv_1}.${expv_2}.${expv_3}.${expv_4}" 

UPDATE:! The getdllversion command is a compile-time command other than the GetDLLVersion statement. IMO, it should have been named as getdllversionlocal to prevent confusion, since it looks like a GetDLLVersionLocal statement, not GetDLLVersion

The user guide included with NSIS 3.0a1 says:

5.1.14! getdllversion

localfilename define_basename

This is similar to GetDLLVersionLocal, only it saves the version number in defines and can therefore be used anywhere, and not just inside functions and sections.

 !getdllversion "$%windir%\explorer.exe" expv_ !echo "Explorer.exe version is ${expv_1}.${expv_2}.${expv_3}.${expv_4}" 
+8
source

Source: https://habr.com/ru/post/901843/


All Articles