Written by Irmak Tevfik on 19 - Oct - 2014

Creating and Publishing a NuGet Package

Previously we have gone through Creating a Repository project for Azure Storage services and I though it would be a good idea to create a NuGet Package in 5 simple steps. Before we begin, I have moved AzureStorageRepository project contents under WebRole project "Repository" folder which should look like:

Please remember to change the namespaces and references accordingly. So let's begin.

Step1: Download NuGet.exe and organise your folder.
Once you have created a folder for your NuGet package, go ahead and download the latest NuGet.exe from Nuget Releases and copy it to the created folder. In my case, latest version is NuGet 2.8.3

Step2: Creating and configuring nuspec file.
Open your command prompt and run:
nuget spec

Which will generate our core specification file for the NuGet package. Here properties will be used to display the main specification and configuration about your package. You need to becareful about some of the properties. 
Lets say if you are willing to publish your package as alpha or beta (Prerelease), there is a specific syntax you need to follow on versioning. Any sting followed after a dash is recognised as a prerelease version by NuGet.

But please aware not to use dot or a comma on the prerelease versioning such as

Also dependencies section needs to be studied well depending on your product. Lets say if we add:
<dependency id="SampleDependency" version="1.0" />

Means product will be depending on the package with Id SampleDependency with version "1.0"  or higher.
if we were to write version="(,1.0]" means dependency version should be lower or equal to 1.0. I have added the full chart below from http://docs.nuget.org/docs/reference/versioning

1.0  = 1.0 ≤ x
(,1.0]  = x ≤ 1.0
(,1.0)  = x < 1.0
[1.0] = x == 1.0
(1.0) = invalid
(1.0,) = 1.0 < x
(1.0,2.0) = 1.0 < x < 2.0
[1.0,2.0] = 1.0 ≤ x ≤ 2.0

So, lets go ahead and configure our nuspec as 

<?xml version="1.0"?>
<package >
    <description>Creates a basic repository classes for Azure Storage services</description>
    <tags>Azure Storage Repository</tags>
      <dependency id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.3" />
      <dependency id="WindowsAzure.Storage" version="4.2.1" />

Step3 : Adding files and configure transformations to NuGet
On NuGet packages, all content to be added needs to be inserted into a folder called "content" under the folder we have created. So lets go ahead and add our folder as:

We need to add our Repository folder from our project, but before we proceed we need to make some changes. As we have all namespaces related to our existing project, we need to change them as $rootnamespace$ like:

Next step is to configure our transformations. Once our NuGet gets installed, it needs to carryout these transformations and add our files to the project. We need to add ".pp" to the cs file extensions to tell our NuGet to carry our this process on the target.
So go ahead and change the extensions as shown below:

If you are willing to transform a configuration file (Web.config, App.config etc..) please refer to the following LINK for more information.
Step4 : Creating package and Uploading NuGet
Now lets go back to our command prompt and run:
NuGet pack

it will be creating our .nupkg file which we will be uploading. 

Step5 : Publishing NuGet Package
Now, you can simply login to your NuGet account (or create a new one) and upload your generated package file from upload section shown below.

Once you followed the steps you will be having your first NuGet package uploaded.

Enjoy Coding:)
comments powered by Disqus