【问题标题】:NSIS - put EXE version into name of installerNSIS - 将 EXE 版本放入安装程序的名称中
【发布时间】:2011-03-03 14:43:50
【问题描述】:

NSIS 有一个您在脚本中定义的 Name 变量:

Name "MyApp"

它定义了安装程序的名称,显示为窗口标题等。

有没有办法从我的主 EXE 中提取 .NET 版本号并将其附加到名称中?

所以我的安装程序名称会自动成为“MyApp V2.2.0.0”或其他名称?

【问题讨论】:

  • 没有人谈论它,但我认为没有插件解决它的最好方法是使用函数“searchparse”,因为 NSIS > V2.39 我做了一篇关于它的小文章。 ti-r.com/?Articles/NSIS/NSIS-versionning

标签: nsis


【解决方案1】:

从 NSIS v3.0a0 开始,您可以直接在脚本中完成,无需外部工具:!getdllversion

示例代码(来自文档):

!getdllversion "$%WINDIR%\Explorer.exe" Expv_
!echo "Explorer.exe version is ${Expv_1}.${Expv_2}.${Expv_3}.${Expv_4}"

【讨论】:

    【解决方案2】:

    从 NSISv3.0 开始,这可以通过 !getddlversion 完成,无需使用任何第三方软件:

    !getdllversion "MyApp.exe" ver
    Name "MyName ${ver1}.${ver2}.${ver3}.${ver4}"
    OutFile "my_name_install_v.${ver1}.${ver2}.${ver3}.${ver4}.exe"
    

    【讨论】:

    • 你先生是我的英雄。我使用了你的方法,效果很好:) ``` !getdllversion "..\${app_name}\${app_name}.exe" ver !define PRODUCT_VERSION "${ver1}.${ver2}.${ ver3}.${ver4}" !define VERSION "${ver1}.${ver2}.${ver3}.${ver4}" VIProductVersion "${PRODUCT_VERSION}" VIAddVersionKey "ProductName" "${app_name}" VIAddVersionKey "FileVersion" "${PRODUCT_VERSION}" VIAddVersionKey "VIProductVersion" "${VERSION}" VIAddVersionKey "LegalCopyright" "(C) ${author}" VIAddVersionKey "FileDescription" "${app_name}" ```
    • 太棒了!谢谢。
    【解决方案3】:

    NSIS 编译后调用简单的 VBS 脚本:

    Set ddr = CreateObject("Scripting.FileSystemObject")
    Version = ddr.GetFileVersion( "..\path_to_version.exe" )
    ddr.MoveFile "OutputSetup.exe", "OutputSetup_" & Version & ".exe"
    

    【讨论】:

      【解决方案4】:

      您可以使用 MSBuild 实现此目的。

      1. 只需将您的 .nsi 脚本添加到项目并设置此文件属性 Copy to Output DirectoryCopy alwaysCopy if newer

      2. 按照代码添加到您的项目文件(例如.csproj 或.vbproj)(假设您的nsi 脚​​本名称为installer.nsi

        <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
          <!-- Getting assembly information -->
          <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
            <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
          </GetAssemblyIdentity>
          <!-- Compile NSIS installer script to get installer file -->
          <Exec Command='"%programfiles(x86)%\nsis\makensis.exe" /DVersion=%(myAssemblyInfo.Version) "$(TargetDir)installer.nsi"'>
            <!-- Just to show output from nsis to VS Output -->
            <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
          </Exec>
        </Target>
        
      3. 在您的 nsi 脚本中使用 $Version 变量:

        # define installer name
        OutFile "MyApp-${Version}.exe"
        

      【讨论】:

        【解决方案5】:

        我在 NSIS wiki 上找到了一种方法:

        http://nsis.sourceforge.net/Version_Info_manipulations_on_compile-time

        【讨论】:

        • 这很有趣。我喜欢 NSIS 的新 v3.x 如何通过 !getdllversion 和 VIProductVersion/VIAddVersionKey 实现这一点。但是,如果有人还在使用 v3.x。
        【解决方案6】:

        您可以在不使用 .NET 的情况下使用 GetVersion plugin 执行此操作,但遵循相同的基本逻辑:

        这里是 ExtractVersionInfo.nsi:

        !define File "...\path\to\your\app.exe"
        
        OutFile "ExtractVersionInfo.exe"
        SilentInstall silent
        RequestExecutionLevel user
        
        Section
        
         ## Get file version
         GetDllVersion "${File}" $R0 $R1
          IntOp $R2 $R0 / 0x00010000
          IntOp $R3 $R0 & 0x0000FFFF
          IntOp $R4 $R1 / 0x00010000
          IntOp $R5 $R1 & 0x0000FFFF
          StrCpy $R1 "$R2.$R3.$R4.$R5"
        
         ## Write it to a !define for use in main script
         FileOpen $R0 "$EXEDIR\App-Version.txt" w
          FileWrite $R0 '!define Version "$R1"'
         FileClose $R0
        
        SectionEnd
        

        你编译一次,然后从你的真实安装程序中调用它:

        ; We want to stamp the version of the installer into its exe name.
        ; We will get the version number from the app itself.
        !system "ExtractVersionInfo.exe"
        !include "App-Version.txt"
        Name "My App, Version ${Version}"
        OutFile "MyApp-${Version}.exe"
        

        【讨论】:

        • 知道会很有趣:为什么需要编译 exe 而不是只包含一些代码?
        【解决方案7】:

        可能有一个非常简单的方法可以做到这一点,但我不知道它是什么。当我第一次开始使用 NSIS 时,我开发了这个解决方法来满足我的需求,并且从那以后就没有重新审视这个问题,看看是否有更优雅的方法。

        我希望我的安装程序具有与我的主要可执行文件相同的版本号、描述和版权信息。因此,我编写了一个名为 GetAssemblyInfoForNSIS 的简短 C# 应用程序,它从可执行文件中提取该文件信息并将其写入我的安装程序包含的 .nsh 文件中。

        这是 C# 应用程序:

        using System;
        using System.Collections.Generic;
        using System.Text;
        
        namespace GetAssemblyInfoForNSIS {
            class Program {
                /// <summary>
                /// This program is used at compile-time by the NSIS Install Scripts.
                /// It copies the file properties of an assembly and writes that info a
                /// header file that the scripts use to make the installer match the program
                /// </summary>
                static void Main(string[] args) {
                    try {
                        String inputFile = args[0];
                        String outputFile = args[1];
                        System.Diagnostics.FileVersionInfo fileInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(inputFile);
                        using (System.IO.TextWriter writer = new System.IO.StreamWriter(outputFile, false, Encoding.Default)) {
                            writer.WriteLine("!define VERSION \"" + fileInfo.ProductVersion + "\"");
                            writer.WriteLine("!define DESCRIPTION \"" + fileInfo.FileDescription + "\"");
                            writer.WriteLine("!define COPYRIGHT \"" + fileInfo.LegalCopyright + "\"");
                            writer.Close();
                        }
                    } catch (Exception e) {
                        Console.WriteLine(e.Message + "\n\n");
                        Console.WriteLine("Usage: GetAssemblyInfoForNSIS.exe MyApp.exe MyAppVersionInfo.nsh\n");
                    }
                }
            }
        }
        

        因此,如果您像这样使用该应用程序:

        GetAssemblyInfoForNSIS.exe MyApp.exe MyAppVersionInfo.nsh

        您会得到一个名为 MyAppVersionInfo.nsh 的文件,看起来像这样(假设此信息在您的可执行文件中):

        !define VERSION "2.0" 
        !define DESCRIPTION "My awesome application"
        !define COPYRIGHT "Copyright © Me 2010"
        

        在我的 NSIS 脚本的顶部,我执行以下操作:

        !define GetAssemblyInfoForNSIS "C:\MyPath\GetAssemblyInfoForNSIS.exe"
        !define PrimaryAssembly "C:\MyPath\MyApp.exe"
        !define VersionHeader "C:\MyPath\MyAppVersionInfo.nsh"
        !system '"${GetAssemblyInfoForNSIS}" "${PrimaryAssembly}" "${VersionHeader}"'
        !include /NONFATAL "${VersionHeader}"
        
        !ifdef VERSION
            Name "My App ${VERSION}"
        !else
            Name "My App"
        !endif
        
        !ifdef DESCRIPTION
            VIAddVersionKey FileDescription "${DESCRIPTION}"
        !endif
        
        !ifdef COPYRIGHT
            VIAddVersionKey LegalCopyright "${COPYRIGHT}"
        !endif
        

        前 3 个定义设置文件名以在对 GetAssemblyInfoForNSIS.exe 的 !system 调用中使用。此系统调用在安装程序编译期间发生,并在您包含它之前生成 .nsh 文件。我使用 /NONFATAL 开关,以便在生成包含文件时发生错误时我的安装程序不会完全失败。

        【讨论】:

        • 现在这就是我所说的解决方案!谢谢:)
        猜你喜欢
        • 1970-01-01
        • 2017-05-25
        • 1970-01-01
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-13
        相关资源
        最近更新 更多