Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSIS - put EXE version into name of installer

Tags:

nsis

NSIS has a Name variable that you define in the script:

Name "MyApp"

It defines the name of the installer, that gets displayed as the window title, etc.

Is there a way to pull the .NET Version number out of my main EXE and append it to the Name?

So that my installer name would automatically be 'MyApp V2.2.0.0" or whatever?

like image 858
codeulike Avatar asked Jun 14 '10 16:06

codeulike


People also ask

How to compile NSi?

NSIS installers are generated by using the 'MakeNSIS' program to compile a NSIS script (. NSI) into an installer executable. The NSIS development kit installer sets up your computer so that you can compile a . nsi file by simply right-clicking on it in Explorer and selecting 'compile'.

How do I run a NSI script?

NSIS Setup You can use the NSIS Menu and under the Compiler section click Compile NSI scripts to start MakeNSISW. The makensisw.exe in the NSIS installation folder is the actual compiler. It has a graphical front end that explains three ways to load scripts, so it's very easy to use.

How do I find my NSIS version?

Description. A simple function to return the version of Windows that the script is running on. It can detect Windows 95 through Windows 8.1, returns the version number if it is Windows NT 3 or 4 and returns a blank string if it can not determine the Windows version.


1 Answers

There might be a very simple way to do this, but I don't know what it is. When I first started using NSIS, I developed this workaround to suit my needs and haven't revisited the problem since to see if there's anything more elegant.

I wanted my installers to have the same version number, description, and copyright info as my main executable. So I wrote a short C# application called GetAssemblyInfoForNSIS that pulls that file info from an executable and writes it into a .nsh file that my installers include.

Here is the C# app:

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");
            }
        }
    }
}

So if you use that application like so:

GetAssemblyInfoForNSIS.exe MyApp.exe MyAppVersionInfo.nsh

You would get a file named MyAppVersionInfo.nsh that looks something like this (assuming this info is in your executable):

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

At the top of my NSIS script, I do something like this:

!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

The first 3 defines set up the file names to use in the !system call to GetAssemblyInfoForNSIS.exe. This system call takes place during your installer's compilation and generates the .nsh file right before you include it. I use the /NONFATAL switch so that my installer doesn't fail completely if an error occurs in generating the include file.

like image 58
Kyle Gagnet Avatar answered Oct 05 '22 23:10

Kyle Gagnet