【问题标题】:(Can I / How do I) access the assembly information from WiX(我可以/我如何)从 WiX 访问程序集信息
【发布时间】:2021-05-14 03:43:21
【问题描述】:

我正在学习如何使用 WiX 为我的项目创建安装程序,并且希望能够引用我的 VS2019 C# 项目的程序集信息,这些信息是从我的 .wxs 文件中的项目属性 > 应用程序 > 程序集信息按钮填充的.

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

谢谢

SEO 术语:wix 汇编版本。 wix 程序集信息

【问题讨论】:

  • 我已经阅读了一堆具有不同程度洞察力的人。我得到的最接近的是我的 .wixproj 文件中的内联任务,它使用 C# 提取数据并将其放入任务输出参数中。我相信我只需要将输出变量作为预处理器变量从 .wxs 文件中访问即可。
  • 如果一个解决方案有多个OutputType为WinExe的项目怎么办?
  • 能够根据选定的 UI 选项指定多个程序集及其数据,并将其特定数据显示为 +1。但是现在只要让一个工作就可以让我根据需要扩展能力。

标签: c# visual-studio wix preprocessor


【解决方案1】:

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。

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

Rob Mensching:WiX 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"


链接:

【讨论】:

  • 我使用的是 Visual Studio,而不是直接使用命令行,我根本无法使用任何绑定组合来构建安装程序。无论我输入什么,系统都无法解析绑定时变量。
  • 所以经过几个小时的战斗,我只能让bind.assemblyName..ABC、bind.fileVersion.ABC 和bind.property.ProductVersion.ABC 工作。无论我尝试什么,“包”属性都不会解析。
  • 很抱歉听到这个消息。我刚刚添加了一个 Visual Studio 示例。我不确定那些“包”属性 - 它们可能仅适用于 Bundle 项目而不是 WiX 项目吗? (捆绑项目创建 setup.exe 启动器,常规 WiX 项目创建 MSI 文件)。
  • 在我看来,这些属性是用于 Bundle 引用 MSI 文件中的值。 See this random example found on github.com.
  • 我还没有开始 wix bundle 项目,我打算利用一个(如果我理解正确的话)来创建一个设计精美的自定义 UI,但还没有开始。我已经确定我很可能需要向运行一些 C# 的 wix 项目添加一个 BeforeBuild 操作,以从指定文件中提取 FileVersionInfo 类并将其放置在一些自定义预处理器变量中,但我没有那里也很幸运。 (谁能想到不必对所有应用程序信息进行硬编码会是一项看似不可能完成的任务……)
【解决方案2】:

对于那些将来遇到这种情况的人,这里是从 wix 访问程序集信息的方法。

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

为了便于定义目标程序集文件,请通过 .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>

参考文献

【讨论】:

    猜你喜欢
    • 2010-11-02
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    相关资源
    最近更新 更多