NuSpec - how to trim $version$ down to Major.Minor

2019-08-19 00:44发布

I'm creating a .NuSpec file with

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

Unfortunately, that returns the assembly's version in Major.Minor.Build.Revision format. I need to trim this down to 3 parts only (Major.Minor.Build) for compatibility with SemVersion as used by Squirrel.

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

1条回答
做自己的国王
2楼-- · 2019-08-19 01:07

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

If you do not want to modify the version number by hand for each release, you probably don't need .nuspec. You can run nuget pack with the project file directly:

nuget pack MyProject.csproj

According to the Replacement tokens, when we pack the .csprojdirectly, the value source of $version$ comes from file AssemblyInfo.cs or AssemblyInfo.vb:

enter image description here

So nuget will use the value of AssemblyInformationalVersion if present, otherwise AssemblyVersion. It is removed by default, because they don’t apply to semantic versioning. when we add it to the AssemblyInfo.cs file:

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

Nuget will actually apply whatever is in that string as the package version. Notice that the informational version only contains three numbers. So it compatibility with SemVersion as used by Squirrel.

enter image description here

Source:How to Version Assemblies Destined for Nuget

Hope this helps.

查看更多
登录 后发表回答