(我可以/我如何)从 WiX 访问程序集信息

(Can I / How do I) access the assembly information from WiX

我正在学习如何使用 WiX 为我的项目创建安装程序,并希望能够引用我的 VS2019 C# 项目的程序集信息,这些信息是从我的 . wxs 文件。

我知道我可以使用添加项目作为参考,然后使用 $(var.MyProject.???) 声明,但我不知道在哪里可以找到项目的可行点符号属性列表。或者,我知道我可以将 '' 与 <Package Manufacturer="$(var.CompanyName)" .../> 一起使用,以节省多次输入数据的麻烦,但我仍然更愿意将其从项目中提取出来并放在一个地方。

谢谢

SEO 术语:wix 程序集版本。 wix 装配信息

Light.exe:请在此处查看 WiX light.exe 文档:https://wixtoolset.org/documentation/manual/v3/overview/light.html#standard-binder-variables

如果您想 运行 将主程序集的版本作为安装程序的 ProductVersion,您应该可以这样做:

<Product Id="*" Name="MyProject" Version="!(bind.fileVersion.MyMain.exe)"
         Manufacturer="MyCorp" Language="1033" UpgradeCode="PUT-GUID-HERE">

 <..>

  <Component Feature="Main">
    <File Id="MyMain.exe" Source="MyMain.exe"></File>
  </Component>

WiX 样本Complete Github.com sample here.

fileLanguagefileVersion 可用于所有版本化的二进制文件。 Dot NET 程序集支持许多其他变量 - see documentation(与答案顶部相同 link)。

Rob MenschingWiX creator Mensching has an answer here精华"You can drive your product version off of an assembly's version using "!(bind.assemblyVersion.FileId)". ... You can only specify binder variables in .wxs files. It's a binder (in light.exe) concept not an MSBuild (in MSBuild.exe reading .wixproj files) concept"

Heath Stewart:请查看此博客以获取有关 .NET 程序集值的一些信息: https://devblogs.microsoft.com/setup/get-binder-variables-for-assemblies-without-installing-into-the-gac/ - 精华: "...to get binder variables for assemblies without installing into the GAC set File/@Assembly to “.net” or “win32”, then for the same file set File/@AssemblyApplication to the value for File/@Id"


链接:

对于那些将来 运行 参与其中的人来说,这里是从 wix 获取程序集信息的方法。

请注意,在发帖时我没有设法return将整个数据对象作为一个结构,但至少可以将值输出为字符串。

为了便于定义目标程序集文件,请通过 .wixproj 文件中的节点或通过 wixProj 的 Properties > Build > Define Preprocessor variables 字段定义常量: AssetPath=../MyReferencedProject/bin/$(Configuration)/MyAsset.exe;

接下来,将以下代码放入您的 .wixProj 文件中。 (用 NotePad++ 编辑,然后在 VS 中重新加载项目)

    <!--To modify your build process, add your task inside one of the targets below and uncomment it.
    Other similar extension points exist, see Wix.targets.-->

    <Target Name="BeforeBuild">
        <ExtractAsmInfo AsmPath="$([System.Text.RegularExpressions.Regex]::Match(&quot;;$(DefineConstants);&quot;, &quot;;AssetPath=(?&lt;path&gt;.*?);&quot;).Groups[&quot;path&quot;].Value)">
            <Output PropertyName="asmCompName" TaskParameter="AsmCompName" />
            <Output PropertyName="asmProdName" TaskParameter="AsmProdName" />
            <Output PropertyName="asmDesc" TaskParameter="AsmDesc" />
            <Output PropertyName="asmCopyright" TaskParameter="AsmCopyright" />
            <Output PropertyName="asmTrademarks" TaskParameter="AsmTrademarks" />
            <Output PropertyName="asmFileVersion" TaskParameter="AsmFileVersion" />
        </ExtractAsmInfo>

        <!--This is needed for the output to be accessible from your .wxs file as "!(wix.asmCompName)"-->
        <CreateProperty Value="asmCompName=$(asmCompName);$(WixVariables)">
            <Output TaskParameter="Value" PropertyName="WixVariables" />
        </CreateProperty>
        <CreateProperty Value="asmProdName=$(asmProdName);$(WixVariables)">
            <Output TaskParameter="Value" PropertyName="WixVariables" />
        </CreateProperty>
        <CreateProperty Value="asmDesc=$(asmDesc);$(WixVariables)">
            <Output TaskParameter="Value" PropertyName="WixVariables" />
        </CreateProperty>
        <CreateProperty Value="asmCopyright=$(asmCopyright);$(WixVariables)">
            <Output TaskParameter="Value" PropertyName="WixVariables" />
        </CreateProperty>
        <CreateProperty Value="asmTrademarks=$(asmTrademarks);$(WixVariables)">
            <Output TaskParameter="Value" PropertyName="WixVariables" />
        </CreateProperty>
        <CreateProperty Value="asmFileVersion=$(asmFileVersion);$(WixVariables)">
            <Output TaskParameter="Value" PropertyName="WixVariables" />
        </CreateProperty>
    </Target>

    <!--<Target Name="AfterBuild"></Target>-->

    <!--Extracts data from the assembly-->
    <UsingTask TaskName="ExtractAsmInfo" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
        <ParameterGroup>
            <!--The assembly path-->
            <AsmPath ParameterType="System.String" Required="true" />
            <!--The return value. Note the return types are extremely limited-->
            <AsmCompName ParameterType="System.String" Output="true" />
            <AsmProdName ParameterType="System.String" Output="true" />
            <AsmDesc ParameterType="System.String" Output="true" />
            <AsmCopyright ParameterType="System.String" Output="true" />
            <AsmTrademarks ParameterType="System.String" Output="true" />
            <AsmFileVersion ParameterType="System.String" Output="true" />
        </ParameterGroup>
        <Task>
            <Reference Include="System.Xml" />
            <Reference Include="System.Xml.Linq" />
            <Using Namespace="System" />
            <Using Namespace="System.Xml.Linq" />
            <Using Namespace="System.Reflection" />
            <Using Namespace="System.Diagnostics" />
            <Code Type="Fragment" Language="cs">
                <![CDATA[
            FileVersionInfo fileInfo = FileVersionInfo.GetVersionInfo(AsmPath);
            AsmCompName = fileInfo.CompanyName;
            AsmProdName = fileInfo.ProductName;
            AsmDesc = fileInfo.FileDescription;
            AsmCopyright = fileInfo.LegalCopyright;
            AsmTrademarks = fileInfo.LegalTrademarks;
            AsmFileVersion = fileInfo.FileVersion;
        ]]>
            </Code>
        </Task>
    </UsingTask>

最后,在您的 .wxs 文件中,您可以使用:

<!--Note the use of an EXCLAMATION MARK and not Dollar Sign as well as the wix. instead of var.-->
!(wix.asmProdName)
!(wix.asmCompName)
!(wix.asmDesc)
!(wix.asmCopyright)
!(wix.asmTrademarks)
!(wix.asmFileVersion)

<!--Example-->
<Product Id="*" UpgradeCode="1129c4e2-e288-48d5-84dd-587aec927f26"
             Name="!(wix.asmProdName) Installer" Manufacturer="!(wix.asmCompName)"
             Language="1033" Version="!(wix.asmFileVersion)">

        <Package Id="*" Compressed="yes"
                 InstallerVersion="200" InstallPrivileges="elevated" InstallScope="perMachine"
                 Keywords="Installer" Languages="1033" Platform="x64" ReadOnly="no" ShortNames="no" SummaryCodepage="1252"
                 Description="The !(wix.asmDesc) and it's protocol installer."
                 Comments="!(wix.asmCopyright) | !(wix.asmTrademarks)" />
...
</Product>

参考资料