Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting version in post-build for Nuget

Tags:

c#

nuget

Code:

if $(ConfigurationName) == Release (
    cd $(ProjectDir)
    nuget spec Entities -f
    nuget pack DeusPak.Entities.csproj -Prop Configuration=Release
    nuget push DeusPak.Entities.$(version).nupkg  $(MYGET_API_KEY) -Source     http://www.myget.org/F/lojaali/api/v2/package
)

I have just started to play around with NuGet and want to know how to include the version number in my NuGet package. I am currently hard coding it into the post-build event which is obviously not what I want to keep doing. Can anybody help?

This is my current post-build event :

if $(ConfigurationName) == Release (
    cd $(ProjectDir)
    nuget spec Dev-f
    nuget pack Dev.csproj -Prop Configuration=Release
    nuget push Dev.1.0.0.0.nupkg  $(MYGET_API_KEY) -Source     http://www.myget.org/F/api/v2/package
)

Update:

OK, I have managed to build the DLL with the correct auto incremented version number :

if $(ConfigurationName) == Release (
    cd $(ProjectDir)
    nuget spec Dev -f
    nuget pack Dev.csproj -Prop Configuration=Release
    nuget push Dev.$(version).nupkg  $(MYGET_API_KEY) -Source     http://www.myget.org/F/api/v2/package
)

But this version does not show on my list of MyGet packages. How do I get it to show there so it can be downloaded? Or can this only be done manually by clicking 'Add a package'?

like image 525
user517406 Avatar asked Jul 30 '12 13:07

user517406


2 Answers

Just extending a bit the solution provided by Carlos J López, i used the AfterBuild script to actually call NuGet and provide the version parameter.

In my case, I also added "cd $(ProjectDir)" to the post build event, which actually happens before the AfterBuild script.

Cheers,

    <Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'">
      <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
          <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
      </GetAssemblyIdentity>
      <Exec Command="$(SolutionDir)\.nuget\nuget pack $(ProjectName).nuspec -Version %(AssemblyVersion.Version)" />
      <Message Text="$(SolutionDir)\.nuget\nuget pack $(ProjectName).nuspec -Version %(AssemblyVersion.Version)" Importance="high" />
    </Target>  
like image 71
Feu Avatar answered Sep 30 '22 17:09

Feu


It is not clear in your question, but assuming you want to sync the version of your package with the version of your assembly, you can simply manage the AssemblyVersion attribute in the project's AssemblyInfo.cs file.

[assembly: AssemblyVersion("1.0.0")]

or if you want to use auto-generated build numbers

[assembly: AssemblyVersion("1.0.0.*")]

If you want to deviate from the assembly's version, and only specify the package version, you can use the AssemblyInformationalVersion attribute in the AssemblyInfo.cs file.

[assembly: AssemblyInformationalVersion("1.0.0")]

It's also not clear from the question what versioning strategy you use, but I'll assume you want to apply Semantic Versioning (where the first 3 version numbers are most relevant). In general when auto-creating NuGet packages, I'd recommend you to create a tokenized nuspec file in your csproj directory, so you can more easily manipulate the package metadata. FYI, there's even a NuGet package to assist you with that:

Install-Package NuSpec 

NuGet will look for this nuspec (make sure it's called MyProject.nuspec) when targeting MyProject.csproj.

<package>
  <version>$version$</version>
  ...
</package>

I also explained this on the MyGet blog in this post: http://blog.myget.org/post/2012/04/27/NuGet-version-token-explained.aspx

A post build that calls nuget pack should be good enough then, assuming you simply change the assembly version before building.

nuget pack MyProject.csproj
like image 27
Xavier Decoster Avatar answered Sep 30 '22 17:09

Xavier Decoster