Friday, March 20, 2015

NuGet for Visual Studio

Lately I've been doing a lot of Visual Studio development at work and at home. I don't like rewriting or copying common code from project to project. I strongly believe in the "DRY" principle of programming: "Don't repeat yourself." On the other hand, package management can be a pain.

Installing third party software using NuGet is now so very easy. But I was not the only developer that thought it would be complicated to create and manage our own Nuget packages. How wrong I was!

Creating and managing NuGet packages could not be easier, and NuGet takes so much of the pain out of managing shared code. It comes installed by default in the latest versions of Visual Studio, and if  you don't have it, you can install it as an ad-on.

Here are the simple steps to creating a NuGet package and making it available to your Visual Studio projects (or other software that accesses NuGet like MonoDevelop). These steps assume a C# project only because that's what I've been working with.
  1. Create, write, and build a library project (this is where you compile to a ".dll" file). Your shared code will be in the .dll
  2. Download nuget.exe from https://nuget.codeplex.com/releases/view/58939 and put it in your "PATH."
  3. From an MS command shell, CD down into your project directory (typically one level lower than your solution). This is the directory where your ".csproj" file exists. (Assuming your doing C# development.) 
  4. Edit your project assembly information (either through project properties or by editing AssemblyInfo.cs directly to fill in program description, company name, etc.
  5. Run "nuget spec" to create a new ".nuspec" file.
  6. Edit the nuspec file as you desire. Once you run the next step, you'll know better how you want to edit it based on the warnings you get form NuGet.
  7. Run "nuget pack" to generate the ".nupkg" file, like: "CommonCode.1.0.0.0.nupkg"
  8. Copy this and all other such package files to a directory of your choice. If you're sharing this with others, make sure they can access that directory. (This is the easiest way to create a NuGet repository, and the focus of this example.)
  9. One time only: go into Visual Studio Tools->NuGet package manager->Package Manager Settings and under "NuGet Package Manager->Package Sources" add your package directory as a package source, naming your new "repository." Be descriptive and concise.
Now when you have a solution open in Visual Studio, go to Tools->NuGet package manager->Manage NuGet Packages for Solution, look under "Online" and you will see the repository you added and whatever packages happen to be there.


No comments:

Post a Comment