How to parameterize msi file from electron builder

↘锁芯ラ 提交于 2019-11-30 09:30:04

问题


I'm trying to create an .msi installer file with electron-builder (version 20.39.0), that can be parameterized during install time. The parameters (e.g. server endpoint) should be written in a file.

Example:
when MsiExec /i "MyProject.msi" SERVER_ENDPOINT=myapp.example.com
then myapp.example.com should appear in a file in the installation dir.

I tried to edit electron-builder's wix template file adding the following to write ${SERVER_ENDPOINT} to server.txt

File C:\...\MyProject\node_modules\electron-builder-lib\templates\msi\template.xml

...
<CustomAction Id="writeConfigFile" Directory="APPLICATIONFOLDER" Execute="commit" Impersonate="yes" ExeCommand="cmd.exe /c &quot;echo ${SERVER_ENDPOINT} > server.txt&quot;" Return="check" />
...
<InstallExecuteSequence>
  ...
  <Custom Action="writeConfigFile" After="InstallFinalize"/>
</InstallExecuteSequence>

Running with

MsiExec /i "MyProject.msi" /L*v Install.log SERVER_ENDPOINT=myapp.example.com

I does not work yet. It installs but does not show writeConfigFile in the log file.

Do you think this is the right approach to make the msi file parameterized?
Or would you recommend another solution?

I also found Orca.exe, to create an MST file, but I would prefer a simple solution, without manual steps.


回答1:


Setting MSI Properties

I am unfamiliar with Electron builder. However, in MSI terms you need to specify that the content in the file should be replaced by an MSI Property, and then you need to set the property either in a transform, by command line or in the property table (embedded in MSI).

In fact you can set all three at once, and I am not sure which one would apply :-). Command line certainly overrides the property table, but I am not sure what wins in a battle between a transform and a command line parameter:

Transform (applying transform on command line, actual settings inside the transform file - mst):

msiexec.exe /i "MySetup.msi" TRANSFORMS="MyTransform.mst"

Command Line (setting PUBLIC properties on the command line):

msiexec.exe /i "MySetup.msi" MYPROPERTY="My Value here"

Property Table (the built-in Property table in every MSI can also have a value set):


Using MSI Properties

Setting properties is obviously not enough, you have to define where the value goes during installation.

  • If the file is an INI file it is quite easy to set a parameter, since this is a built-in feature of MSI.
  • XML file updates and text file updates are worse because then you rely on third-party solutions or you do it yourself via custom actions (I would not do the latter).

Advanced Installer has very nice features to replace parameters in XML and text files. Installshield also has such features. The open source WiX toolkit also has features to support XML file updates, but it is much more involved than the commercial tools.

With regards to Electron I don't know how it works. But, in either case the central task is to get the MSI to contain a construct such as this:

This is from an MSI compiled with Advanced Installer. You see that I have a parameterized value [MYVALUE]. It can be set on the command line since it is an ALL UPPERCASE property - also known as a PUBLIC MSI property. During installation the property in braces will be replaced by the value passed in. Obviously.


Some Links:

  • Add a config file to an installer (msi)
  • How to make better use of MSI files



回答2:


With the help of Stein Åsmul, this is my current solution:

I took the current WiX template of electron-builder and added an option to write variables to an ini file.

<Property Id="MYSERVER" Value="notDefined"/>
<Property Id="MYSECONDPROPERTY" Value="notDefined"/>
...
<Directory Id="APPLICATIONFOLDER" Name="${installationDirectoryWixName}">
  <Component Id="AddLineTo_AppConfig.ini" Guid="{4171FB60-FDC5-46CF-A4D8-4AE9CADB4BE9}" KeyPath="yes" Feature="ProductFeature">
    <IniFile Id="AddLineTo_AppConfig.ini1" Name="AppConfig.ini" Directory="APPLICATIONFOLDER" Section="AppConfig" Key="Server" Value="&quot;[MYSERVER]&quot;" Action="addLine"/>
    <IniFile Id="AddLineTo_AppConfig.ini2" Name="AppConfig.ini" Directory="APPLICATIONFOLDER" Section="AppConfig" Key="SecondProp" Value="&quot;[MYSECONDPROPERTY]&quot;" Action="addLine"/>
  </Component>
</Directory>

The complete template looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  <!-- extended Template from https://github.com/electron-userland/electron-builder/blob/7f0ede7182ab6db8efb0cf4bf3cb183be712fb4e/packages/app-builder-lib/templates/msi/template.xml -->
  <!-- https://blogs.msdn.microsoft.com/gremlininthemachine/2006/12/05/msi-wix-and-unicode/ -->
  <Product Id="*" Name="${productName}" UpgradeCode="${upgradeCode}" Version="${version}" Language="1033" Codepage="65001" Manufacturer="${manufacturer}">
    <Package Compressed="yes" InstallerVersion="500"/>

    <Condition Message="Windows 7 and above is required"><![CDATA[Installed OR VersionNT >= 601]]></Condition>

    <!--
    AllowSameVersionUpgrades:
      When set to no (the default), installing a product with the same version and upgrade code (but different product code) is allowed and treated by MSI as two products.
      When set to yes, WiX sets the msidbUpgradeAttributesVersionMaxInclusive attribute, which tells MSI to treat a product with the same version as a major upgrade.

      So, AllowSameVersionUpgrades="yes" allows to build and test MSI with the same version, and previously installed app will be removed.
    -->
    <MajorUpgrade AllowSameVersionUpgrades="yes" DowngradeErrorMessage='A newer version of "[ProductName]" is already installed.'/>
    <MediaTemplate CompressionLevel="${compressionLevel}" EmbedCab="yes"/>

    <Property Id="ApplicationFolderName" Value="${installationDirectoryWixName}"/>
    <Property Id="WixAppFolder" Value="WixPerUserFolder"/>
    <Property Id="MYSERVER" Value="notDefined"/>
    <Property Id="MYSECONDPROPERTY" Value="notDefined"/>

    {{ if (iconPath) { }}
    <Icon Id="icon.ico" SourceFile="${iconPath}"/>
    <Property Id="ARPPRODUCTICON" Value="icon.ico"/>
    {{ } -}}

    {{ if (isAssisted || isRunAfterFinish) { }}
    <CustomAction Id="runAfterFinish" FileKey="mainExecutable" ExeCommand="" Execute="immediate" Impersonate="yes" Return="asyncNoWait"/>
    {{ } -}}

    <Property Id="ALLUSERS" Secure="yes" Value="2"/>
    {{ if (isPerMachine) { }}
    <Property Id="MSIINSTALLPERUSER" Secure="yes"/>
    {{ } else { }}
    <Property Id="MSIINSTALLPERUSER" Secure="yes" Value="1"/>
    {{ } -}}

    {{ if (isAssisted) { }}
    <!-- Check "Run after finish" checkbox by default -->
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOX" Value="1"/>
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Run ${productName}"/>

    <UIRef Id="WixUI_Assisted"/>
    {{ } else if (isRunAfterFinish) { }}
    <!-- https://stackoverflow.com/questions/1871531/launch-after-install-with-no-ui -->
    <InstallExecuteSequence>
      <Custom Action="runAfterFinish" After="InstallFinalize"/>
    </InstallExecuteSequence>
    {{ } -}}

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="${programFilesId}">
        <Directory Id="APPLICATIONFOLDER" Name="${installationDirectoryWixName}">
          <Component Id="AddLineTo_AppConfig.ini" Guid="{4171FB60-FDC5-46CF-A4D8-4AE9CADB4BE9}" KeyPath="yes" Feature="ProductFeature">
            <IniFile Id="AddLineTo_AppConfig.ini1" Name="AppConfig.ini" Directory="APPLICATIONFOLDER" Section="AppConfig" Key="Server" Value="&quot;[MYSERVER]&quot;" Action="addLine"/>
            <IniFile Id="AddLineTo_AppConfig.ini2" Name="AppConfig.ini" Directory="APPLICATIONFOLDER" Section="AppConfig" Key="SecondProp" Value="&quot;[MYSECONDPROPERTY]&quot;" Action="addLine"/>
          </Component>
        </Directory>
      </Directory>

      <!-- Desktop link -->
      {{ if (isCreateDesktopShortcut) { }}
      <Directory Id="DesktopFolder" Name="Desktop"/>
      {{ } -}}

      <!-- Start menu link -->
      {{ if (isCreateStartMenuShortcut) { }}
      <Directory Id="ProgramMenuFolder"/>
      {{ } }}
    </Directory>

    <!-- Files -->
    <Feature Id="ProductFeature" Absent="disallow">
      <ComponentGroupRef Id="ProductComponents"/>
    </Feature>

    {{-dirs}}

    <ComponentGroup Id="ProductComponents" Directory="APPLICATIONFOLDER">
      {{-files}}      
    </ComponentGroup>
  </Product>
</Wix>

Using electron-builder@20.39.0, I create the MSI with

set DEBUG=electron-builder:*
cp template.xml .\node_modules\app-builder-lib\templates\msi\template.xml
electron-builder

And then install the MSI with

MsiExec /i "myapp.msi" MYSERVER=myapp.example.com MYSECONDPROPERTY=helloworld /L*v Install.log

After installation finished, I got the AppConfig.ini in my installdir (%USERPROFILE%\AppData\Local\Programs\MyApp\AppConfig.ini)

[AppConfig]
Server="myapp.example.com"
SecondProp="helloworld"


来源:https://stackoverflow.com/questions/55460379/how-to-parameterize-msi-file-from-electron-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!