How to set up a NuGet project

I’ve been exploring the NuGet package concept lately. I think it is a pretty slick way to keep functionality separated as well as dependencies.

So, I thought I try to make a canvas javascript game. However, I haven’t decided where it will “live” (MVC4 applicaton? that might be much to much. NancyFx? That could do it). In the future I might want to opt in some SignalR features.

So my idea is to create the core of the game as one NuGet package, then create a second package with the SignalR implementation. That package then has a dependency to the core package. My web application then reference to the signalr package and that way get everything it need right away. All updates will be handled with NuGet.

To make things smoother, I decided that every build with Release configuration should produce a new, uniquely versioned package. In order to get that working, I had to take a few steps:

Auto increment Build Version

How this should be done properly is a topic in itself. try google auto increase assemblyversion c# to take part of the discussion. I did not want to give this too much thought, so I followed the comment in the AssemblyInfo.cs:

// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

Worth noting is that you want to remove [assembly: AssemblyFileVersion("0.0.0.0")] from your file. This makes the Assemby File Version the same as the Assembly Version, which I think is ok.

Write powershell script

Powershell is pretty powerful. It is build on top of .NET, so there is all reasons to get familiar with it. Besides, if I where to chose between powershell and, say, MsBuild… well, that’s a pretty easy thing. For the time being, the my ps script is a oneliner, but who knows – it might grow over time:

param(
    $Configuration = "Release")
& echo "Starting to package the nuget package" 
& nuget pack ../Pitfall.Nuget/Pitfall.Nuget.csproj -Prop Configuration=$Configuration -OutputDirectory ../Pitfall.Nuget/nuget-packages

A few things are worth noting:

  • param is the set of input parameters. When in the powershell terminal you get intelisence of available flags. Release is the default value if you don’t specify
  • I’m keepin my build script outside the project folder, in a sub folder to the solution, therefore I want to specify OutputDirectory, since it will otherwise build to wherever the script is executed (e.g. my script directory).
  • Before the first time running this script, I executed nuget spec, to create an manifesto file used in the packaging process.

Hook the script to project build

I don’t know why Microsoft decided to make this so cumbersome. So I need to edit the .csproj file, which in itself is an XML file that describes the project, what files to include if release, build targets etc. In order to edit the file, right click and Unload project and then right click and select Edit project file.

Where close to the end, there is a comment with to targets, BeforeBuild and AfterBuild. What I did was uncomment the AfterBuild target an added this line:

<Exec Command="powershell.exe -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; {../scripts/create.nuget.ps1}&quot;" Condition="$(Configuration) == 'Release'" />

Again, there are a few gottchas:

  • quotes (“) and ampersand (&) is not allowed, therefore we need to use &quot; and &amp;.
  • By default, the script environment has a execution policy that are Restricted, meaning you are not allowed to run script. Hence the -executionpolicy flag
  • The Condition attribute tells MsBuild that only do this if we’re using Release configuration

Setting up a local NuGet repositoiry

Under Tools > Library Package Manager > Package Manager Settings, go to Package Sources and click the plus. You probably want one directory to which you build all your nuget packages — add that directory and you should be done.

Now, when you open the package manager, you will see at the menu to the left that there is a new source, “Local” (or whatever you have named it). And there is your package. Click and install.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>