MSBuild and 7Zip SFX

The following allows you to package up your build output into a SFX file with custom properties that appear when someone right clicks the SFX and chooses properties.

Useful if you need to create a zip that can be signed but want your company/product information to appear on the SFX.EXE.

<Target Name="AfterCompileSolution" Condition="'$(Solution)'=='$(examplesol_Root)\examplesol.sln' And'$(BuildBreak'!='true'">
    <PropertyGroup>
      <rcfile>7za.rc</rcfile>
      <resfile>resource.res</resfile>
      <rccontent>
        1 VERSIONINFO
        FILEVERSION 4,65,0,0
        PRODUCTVERSION 4,65,0,0
        FILEOS 0x40004
        FILETYPE 0x1
        {
        BLOCK &quot;StringFileInfo&quot;
        {
        BLOCK &quot;040904B0&quot;
        {
        VALUE &quot;CompanyName&quot;, &quot;Example Company&quot;
        VALUE &quot;FileDescription&quot;, &quot;Example Update&quot;
        VALUE &quot;FileVersion&quot;, &quot;1.0&quot;
        VALUE &quot;InternalName&quot;, &quot;Example.exe&quot;
        VALUE &quot;LegalCopyright&quot;, &quot;Copyright (c) 2013 Example Company&quot;
        VALUE &quot;OriginalFilename&quot;, &quot;Example.exe&quot;
        VALUE &quot;ProductName&quot;, &quot;Example Update&quot;
        VALUE &quot;ProductVersion&quot;, &quot;$(ExampleVersion)&quot;
        }
        }

        BLOCK &quot;VarFileInfo&quot;
        {
        VALUE &quot;Translation&quot;, 0x0409 0x04B0
        }
        }
      </rccontent>
      <installerexepath>$(examplesol_Root)\Example\Product Configuration 1\Release\DiskImages\DISK1</installerexepath>
      <installerexe>Example.exe</installerexe>
    </PropertyGroup>

    <Message Text="Generating RC Script" />
    <WriteLinesToFile File="$(examplesol_Root)\$(rcfile)" Lines="$(rccontent)" Overwrite="true" />
    
    <Message Text="Compile Resource file from Resource Script" />
    <Exec ContinueOnError="false" WorkingDirectory="$(examplesol_Root)" 
          Command="&quot;$(winSDKPath)\bin\rc.exe&quot; /fo &quot;.\$(resfile)&quot; &quot;.\$(rcfile)&quot;" />

    <Message Text="Move update file" />
    <Move SourceFiles="$(installerexepath)\$(installerexe)" DestinationFolder="$(examplesol_Root)\Example\" />
    
    <Message Text="Zipping installer for Update" />
    <!-- Use full paths for files so dir struct is not preserved-->
    <Exec ContinueOnError="false" WorkingDirectory="$(examplesol_Root)" 
          Command="&quot;$(Root)\BuildTools\7zip\7za.exe&quot; a -sfx  &quot;.\$(installerexe)&quot; &quot;.\Manifest.xml&quot; &quot;.\Example\$(installerexe)&quot;" />

    <Message Text="Applying Resource changes to sfx file" />
    <Exec ContinueOnError="false" WorkingDirectory="$(examplesol_Root)" 
          Command="$(Root)\BuildTools\reshacker\ResHacker.exe -delete $(installerexe),$(installerexe),versioninfo,1," />
    <Exec ContinueOnError="false" WorkingDirectory="$(examplesol_Root)" 
          Command="$(Root)\BuildTools\reshacker\ResHacker.exe -add $(installerexe),$(installerexe),$(resfile),,," />

    <Message Text="Move log file to dir" />
    <Move SourceFiles="$(Root)\BuildTools\reshacker\reshacker.log" DestinationFolder="$(examplesol_Root)\" />
    
    <OnError ExecuteTargets="UndoCheckout"/>
  </Target>

Leave a Reply

Your email address will not be published. Required fields are marked *