NuGet:如何仅在 $(ConfigurationName) == Debug 时将自动递增的内部版本号附加到我的 NuGet 包版本?

NuGet: How can I append an auto-incrememted build number to my NuGet package version ONLY when $(ConfigurationName) == Debug?

我正在尝试使用 Visual Studio post-构建事件来完成此操作。

我想要发生的事情:

当我在 debug 中构建时,我想创建一个类似于 MyProject.1.0.0.xxxx-ci.nupkg.nupkg,其中 xxxx 是一些自动递增的构建编号,并且 1.0.0 在我的 .nuspec 文件中定义。

然而,当我构建 release 时,我只想要一个简单的 MyProject.1.0.0.nupkg 使用我的 nuspec。


我有一个开发 NuGet 提要和一个生产 NuGet 提要,我想在构建后将它们推送到它们,这就是为什么我只希望这个额外的内部版本号显示在调试中。

我几乎已经在 AssemblyInfo.cs 中使用 [assembly: AssemblyVersion("1.0.0.*")] 解决了这个问题,但是我的发布版本也添加了该版本号,我不希望这样。


这是我在 post-build 中的代码,现在只使用 nuspec 和 not AssemblyInfo.cs:

if "$(ConfigurationName)" == "Debug" (
nuget pack "$(ProjectDir)$(ProjectName).nuspec" -Suffix ci
nuget push -Source http://myfeed.com/nuget/NuGet-Development "*.nupkg" )

if "$(ConfigurationName)" == "Release" (
nuget pack "$(ProjectDir)$(ProjectName).nuspec" 
nuget push -Source http://myfeed.com/nuget/NuGet-Production "*.nupkg" )

有什么方法可以使用 -Version 来做到这一点吗?

我对这一切都非常陌生,所以很有可能我只是遗漏了一些简单的东西,或者所有的命令都是错误的:)

如果我的问题不清楚,请提前道歉!

Is there some sort of way I can use -Version to do this?

答案是肯定的。使用.nuspec文件时,可以使用[assembly: AssemblyVersion("1.0.0.*")]中的版本。只需将版本添加到宏中,然后您就可以在构建事件中使用此宏,如:@(VersionNumber).

为此,请卸载您的项目。然后在项目的最后,就在结束标记 </Project> 之前,将脚本放在下面:

<PropertyGroup>
   <PostBuildEventDependsOn>
     $(PostBuildEventDependsOn);
     PostBuildMacros;
   </PostBuildEventDependsOn>    
</PropertyGroup>

<Target Name="PostBuildMacros">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="Targets" />
  </GetAssemblyIdentity>
  <ItemGroup>
    <VersionNumber Include="@(Targets->'%(Version)')"/>
  </ItemGroup>
</Target>

现在我们可以在构建事件中使用这个宏:@(VersionNumber),如下所示

if "$(ConfigurationName)" == "Debug" (
nuget.exe pack "$(ProjectDir)$(ProjectName).nuspec" -Suffix @(VersionNumber)-ci)
nuget push -Source http://myfeed.com/nuget/NuGet-Development "*.nupkg" )

if "$(ConfigurationName)" == "Release" (
nuget pack "$(ProjectDir)$(ProjectName).nuspec" 
nuget push -Source http://myfeed.com/nuget/NuGet-Production "*.nupkg" )

使用 AssemblyVersion("2.0.0.0") 在 Debug 中构建时的结果:

当您在 Release 中构建时: