NuSpec - 如何将 trim $version$ 降低到 Major.Minor.Build (SemVersion)?

NuSpec - how to trim $version$ down to Major.Minor.Build (SemVersion)?

我正在创建一个 .NuSpec 文件

<metadata>
  <version>$version$</version>
  <!-- ... -->
</metadata>

不幸的是,returns 程序集的版本采用 Major.Minor.Build.Revision 格式。为了与 Squirrel 使用的 SemVersion 兼容,我只需要 trim 将其缩减为 3 个部分 (Major.Minor.Build)。

有什么办法吗?我希望不必为每个版本手动输入版本号。

Is there any way to do this? I'd prefer not having to put in the version number by hand for each release

如果您不想为每个版本手动修改版本号,您可能不需要 .nuspec。你可以运行 nuget pack直接用项目文件:

nuget pack MyProject.csproj

根据Replacement tokens,当我们直接打包.csproj时,$version$的值源来自文件AssemblyInfo.csAssemblyInfo.vb

所以 nuget 将使用 AssemblyInformationalVersion 的值(如果存在),否则使用 AssemblyVersion。它默认被删除,因为它们不适用于语义版本控制。当我们将它添加到 AssemblyInfo.cs 文件时:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]

Nuget 实际上会应用该字符串中的任何内容作为包版本。 请注意,信息版本仅包含三个数字。因此它与 Squirrel 使用的 SemVersion 兼容。

来源:How to Version Assemblies Destined for Nuget

希望这对您有所帮助。