1

I'm trying to accomplish this using Visual Studio post-build events.

What I want to happen:

When I build in debug, I want to create a .nupkg like MyProject.1.0.0.xxxx-ci.nupkg, where xxxx is some auto-incrementing build number, and 1.0.0 is defined in my .nuspec file.

However when I build release, I want just a simple MyProject.1.0.0.nupkg using my nuspec.


I have a Development NuGet feed, and a Production NuGet feed that I'd like to push these to after build, which is why I ONLY want this extra build number to show up in Debug.

I've almost figured this out using [assembly: AssemblyVersion("1.0.0.*")] in AssemblyInfo.cs, but then my release build also gets that build number tacked on, and I don't want that.


Here's the code I have in post-build right now using just the nuspec and 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" )

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

I'm very very new to this all, so there's a good chance I'm just missing simple things, or have the wrong commands all together :)

Sorry in advance if my question isn't clear!

Shaun Z.
  • 379
  • 1
  • 3
  • 11
  • And putting the assembly attribute into an `#if DEBUG` block isn't an option? – Martin Ullrich Nov 14 '17 at 17:59
  • Have you looked at migrating to the new csproj format in vs 2017 that has integrated NuGet support? That's the format used for .NET Standard projects which can also be used for .NET Framework NuGet packages. It has integrated MSBuild support that is easier to use for this szenario – Martin Ullrich Nov 14 '17 at 18:00
  • @MartinUllrich It may be an option, I can't say either way though, as I'm not very knowledgeable in this area yet. Can you explain a bit more how that works? Also, I haven't looked into that but I will! – Shaun Z. Nov 14 '17 at 18:02

1 Answers1

2

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

The answer is yes. You can use the version in the [assembly: AssemblyVersion("1.0.0.*")] when you using the .nuspec file. Just add the version to the macro, then you can use this macro in the build event like:@(VersionNumber).

To accomplish this, unload your project. Then at the very end of the project, just before the end-tag </Project>, place below scripts:

<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>

Now we could use this macro: @(VersionNumber) in the build event as following:

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" )

The result when you build in Debug with AssemblyVersion("2.0.0.0"):

enter image description here

When you build in Release:

enter image description here

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • This is what I needed, thank you so much!! A couple changes to your answer to help others.. You still used `-Suffix` in your answer when `-Version` should be used, and then you have an extra `)` at the end of that line. – Shaun Z. Nov 15 '17 at 20:02
  • Oh seems like that's just cause I'm not SemVer 2.0.0 compliant.. but I got it working how I need it to, so that's good! – Shaun Z. Nov 15 '17 at 20:55