Home > Blockchain >  WIX Installer: ensure successive Version numbers on build
WIX Installer: ensure successive Version numbers on build

Time:05-10

I'm trying to automate my Wix installer builds with my Visual Studio Builds. I am grabbing the application's Application Version via this in my installer.wicproj:

    <Target Name="BeforeBuild">
        <!-- Get Assembly Version -->
        <!-- Update built assembly below-->
        <GetAssemblyIdentity AssemblyFiles="..\DavesDonutsInstallerTest\bin\Debug\DavesDonuts.exe">
            <Output TaskParameter="Assemblies" ItemName="AssemblyVersion"/>
        </GetAssemblyIdentity>

        <PropertyGroup>
            <DefineConstants>BuildVersion=%(AssemblyVersion.Version)</DefineConstants>
        </PropertyGroup>        
        
    </Target>

Then I am using this in my main Product.wxs to populate the version number:

    <Product Id="*" 
                Name="!(loc.ProductName)"
                Language="1033" 
                Version="$(var.BuildVersion)"
                Manufacturer="!(loc.Developer)" 
                UpgradeCode="{40CB43C5-1910-4B9D-ABD2-92F620E7125C}">

This works well. However, what it does mean is that if I build the installer without increasing the Application Version in the main Project properties, the installer builds fine. However, if I run this on a dev machine, the installer runs again and I get two entries for the app in the uninstall facilities (essentially having two or more installations of the same app.

I don't want to be manually updating Product ID every time, thus the auto-gen.

I would like to have a system where the Wix installer builder detects that it's making a build with the same version number as last time, it stops the build and places a warning in the output window.

I would image this would be done in the install.wixproj BeforeBuild function, but have no idea how to implement it. I'd imagine it working thus. On a successful build, the build version is written to a text file in the WixINstaller project folder (via AfterBuild??). Then on next BeforeBuild this is retrieved and compared to the 'present' Application Version and if they match, the build is stopped and an error is thrown.

Brownie points: some routine to figure if it's a lower build version to control for mistake in setting the Application Version.

Wix seems powerful, but a little baffling.

CodePudding user response:

I did manage this totally outside of WIX via the Pre-Build Build Event in VS. Just place this batch scripting in your Pre-Build box. Also managed the brownie point of additionally checking for lower version numbers than the last. It also posts Errors and Warnings in VS's Errors panel.

enter image description here

Or

enter image description here

::Error Thrown by Build Script
@echo off
setlocal ENABLEDELAYEDEXPANSION

:: SETTINGS VARS
:: exceptionOnMatchingVersion can be Warning, Error or None. Be careful of trailing spaces
set exceptionOnMatchingVersion=Error

echo ==========================
echo PRE-BUILD EVENTS
echo ==========================
echo Build Type: $(ConfigurationName)
echo Project: $(ProjectName)

echo.
echo Checking Build Assembly Version for Wix Installer Build

:: PROCESS VARS
set /p lastversion=<lastbuildversion.txt

set version=0.0.0.0
FOR /F delims^=^"^ tokens^=2 %%i in ('findstr /b /c:"[assembly: AssemblyVersion(" $(ProjectDir)\Properties\AssemblyInfo.cs') do (
  set version=%%i
)

set concatVersion=0.0.0
    set /a presentMajor=0
    set /a presentMinor=0
    set /a presentPatch=0
    set /a lastMajor=0
    set /a lastMinor=0
    set /a lastPatch=0
    
for /F "tokens=1,2,3 delims=." %%a in ("%version%") do (
    set concatVersion=%%a.%%b.%%c
    set /a presentMajor=%%a
    set /a presentMinor=%%b
    set /a presentPatch=%%c
    )
    
for /F "tokens=1,2,3 delims=." %%a in ("%lastversion%") do (
    set /a lastMajor=%%a
    set /a lastMinor=%%b
    set /a lastPatch=%%c
    )

echo Build Full Assembly Version: %version%
echo Concatinated Assembly Version: %concatVersion%
echo Last Concatinated Assembly Version: %lastversion%

set lowerVersion=False

If "$(ConfigurationName)"=="Release" (
    If "%lastversion%" == "%concatVersion%" (
        If NOT %exceptionOnMatchingVersion% == None (
            echo.
            echo ======================================================================================================================================================
            echo = ALERT: The last Assembly Version is the same as this Build's Assembly Version. This causes issues with the installer. Please ammend the version number.
            echo = *** YOU ARE ADVISED NOT TO DEPLOY THIS VERSION ***
            echo ======================================================================================================================================================
            echo.
            If %exceptionOnMatchingVersion% == Warning (
                echo Warning: The last Assembly Version is the same as this Build's Assembly Version. This causes issues with the installer. Please ammend the version number.
                )
            If %exceptionOnMatchingVersion% == Error (
                echo Error: The last Assembly Version is the same as this Build's Assembly Version. This causes issues with the installer. Please ammend the version number. BUILD STOPPED.
                exit 1
                )
        )
        
    ) else (
    
        if %presentPatch% LSS %lastPatch% (
            if %presentMinor% LEQ %lastMinor% (
             set "lowerVersion=True"
            )
        ) else (
        
            if %presentMinor% LSS %lastMinor% (
                if %presentMajor% LEQ %lastMajor% (
                 set "lowerVersion=True"
                )
            ) else (
            
                if %presentMajor% LSS %lastMajor% (
                set "lowerVersion=True"
                )
            )           
        )
        
        if "!lowerVersion!"=="True" (
            echo.
            echo ======================================================================================================================================================
            echo = FATAL EXCEPTION: You are trying to build the assembly to a lower version numer. Please check your numbering. BUILD STOPPED..
            echo = *** YOU ARE ADVISED NOT TO DEPLOY THIS VERSION ***
            echo ======================================================================================================================================================
            echo.
            echo Error: You are trying to build the assembly to a lower version numer. Please check your numbering. BUILD STOPPED. This version: %concatVersion%. Last Version: %lastversion%
            exit 1
        ) else (
            echo ======================================================================================================================================================
            echo = VERSION NUMBER VALID: Proceeding to build. \o/
            echo ======================================================================================================================================================
            )
        
    )   
)

:: echo Lower Version: %lowerVersion%

echo %concatVersion%> lastbuildversion.txt
  • Related