gpt4 book ai didi

wix - 配置 WiX 以自动设置产品版本属性?

转载 作者:行者123 更新时间:2023-12-05 01:18:07 26 4
gpt4 key购买 nike

目前,每当我构建我的包时,我都必须手动增加 P​​roduct.wxs 文件中的 Version 属性,如下所示:

<Product 
Id = "*"
Version="4.1.3"

我想将其自动化,以简化构建过程。我们对 exe/dll 文件使用以下版本控制方案:

major.minor.special.build

特殊的几乎从不使用,设置为 0,约定是将打包的 MSI 版本化如下,因为您只能使用三个数字:

major.minor.build

我见过的唯一解决方案是让您获取另一个项目的 4 位数版本,然后截断构建版本,所以您最终会得到这样的结果:

major.minor.special

显然这不适用于我们的方案,因为我们丢失了内部版本号。我如何获取 major.minor.build,忽略特殊?

最佳答案

我使用一个包含文件中的 WiX 变量,我在每次构建时都会重新生成该文件。

由于我的项目是 .wixproj (MSBuild/Visual Studio),我只是在其中将版本提取和格式设置编码为自定义的内联 MSBuild 任务,并在 BeforeBuild 目标中调用它。

在下面的示例中,我获取了产品主程序集的程序集版本。您可以为任何您想要的版本编写代码。

使用 WiX 包含和变量

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<?include ProductVersion.wxi?>
<Product Version="$(var.ProductVersion)" …>

</Wix>

示例 ProductVersion.wxi

<Include>
<?define ProductVersion=1.0.38549?>
</Include>

我建议在项目中包含 .wxi 文件,以便它在解决方案 View 中可见。而且,由于它已生成,我建议将其从源代码管理中排除。

编辑 WixProj

.wixproj 既是 Visual Studio 项目文件又是 MSBuild 项目文件。要在 Visual Studio 中编辑 Visual Studio 项目文件,请选择 tutorial or extension .

BeforeBuild 目标

MSBuild 系统(包括 WiX 的)提供 BeforeBuild 和 AfterBuild 目标,如 .wixproj 注释中所述。

只需从评论中拉出目标并添加任务调用即可。

<Target Name="BeforeBuild">
<GenerateProductVersion AssemblyPath='../wherever/whatever.exe' />
</Target>

MSBuild 内联任务

任务代码可以放在它自己的 MSBuild 文件中,甚至可以放在 DLL 中以供重用。或者,对于脚本方法,它可以是内联的。

此任务分为 3 个部分:

  • 文件路径参数(因为它会因项目而异)
  • 提取(带日志记录)
  • 包括生成(到项目文件夹中的硬编码名称,因为它不需要变化)

.

<UsingTask TaskName="GenerateProductVersion" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<AssemblyPath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Using Namespace="System" />
<Using Namespace="System.Xml.Linq" />
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs"><![CDATA[
var assemblyVersion = AssemblyName.GetAssemblyName(AssemblyPath).Version;
var productVersion = String.Format("{0}.{1}.{2}", assemblyVersion.Major, assemblyVersion.Minor, assemblyVersion.Revision);
Log.LogMessage(MessageImportance.High, "ProductVersion=" + productVersion + " extracted from assembly version of " + AssemblyPath);
new XDocument(
new XElement("Include",
new XProcessingInstruction("define", "ProductVersion=" + productVersion)))
.Save("ProductVersion.wxi");
]]></Code>
</Task>
</UsingTask>

让EXE路径更显眼

所有这些都隐藏在项目文件中。许多项目设计者都有一个构建选项卡,允许在构建中输入名称-值对。这提供了一种从 XML 中向上提升路径的机制。

<Target Name="BeforeBuild">
<GenerateProductVersion AssemblyPath='$([System.Text.RegularExpressions.Regex]::Match(";$(DefineConstants);", ";VersionExtractionPath=(?&lt;path&gt;.*?);").Groups["path"].Value)' />
</Target>

enter image description here

关于wix - 配置 WiX 以自动设置产品版本属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46835233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com