The fundamentals of development: NuGet Packages

Rachel Drake
By Rachel DrakeDeveloper
5 minutes to read

NuGet is the default tool that allows .NET developers to easily share and use code.

 

It can be used on loads of projects including websites that use Json.NET, connecting databases with Entity Framework, and even code from the development community like jQuery and Angular. NuGet is fundamental to projects and can help encourage collaboration and skills development.

 

The practical uses of NuGet packages

 

There are two practical uses for the packages we generate:

 

  1. When used in combination with TeamCity, deployment packages are automatically generated so your site’s code is continuously up-to-date.
  2. NuGet packages contain common tools that we use as a base for our Umbraco sites. This is useful for countless reasons so we try and keep it as up-to-date with Umbraco versions as we possibly can and feedback improvements on the many projects we work on.

 

So, without further ado, here’s how to set up your own NuGet package, stored on a local file system so it’s not publically available. If you do want to make a public package, visit the host site (like NuGet.org) for upload instructions.

 

We’re working on the assumption that you already have a basic library of code. It could be a DLL or a website but the rules are the same for either.

 

How to set up a NuGet package

  1. Download NuGet.exe from: https://www.NuGet.org/downloads

 

  1. Create a manifest file with the .nuspec extension. This describes the contents of the package, as well as any other packages it depends on.

 

This file can be automatically generated using the ‘NuGet spec’ command in the project’s root directory. The structure of the file will depend on several things like the structure of the project or if there’s a .csproj or .vbproj file present. Here’s a simplified example:

 






  

    Unique.Id

    1.0.0

    Enjoy Digital

    Enjoy Digital

    false

    Example NuGet Package

    http://example.com/example.png

    Copyright © 2018

    

      ...

    

  

  

    ...

  



 

To determine which files should be included in the project, specify them in the ‘files’ node:
...



  

  ...



...

 

  1. Finally, call the NuGet pack ‘<package-name>.nuspec’ command to build the package.

 

Modifying your package

These steps generate a .nupkg file that can be placed wherever you want to reference it. However, there are a few modifications we may still want to make. If we’re using this as a base for a website build, we may want to replace part of the namespace in the .cs files to match the name of the project we’re installing the package into.

 

To do this we need to create a .pp file. For example, you may name a .pp file Utility.cs.pp to correspond with your utility.cs file. The .pp would have a simple substitution in the namespace line:

 

Utility.cs

namespace BaseSite.Utility

 

Utility.cs.pp

namespace $rootnamespace$.Utility

 

The $rootnamespace$ token can be replaced when installed in the target project with the corresponding value in that project’s property file.

 

You can see how we might have to create a .pp file for every .cs file in the project. Thankfully, there’s a sanity-saving NuGet package – NuGet Content Generator – which automatically generates on build simply by including appropriate headings for each file.

 

We can then include these .pp files in the manifest, removing any references to the corresponding .cs files, and regenerate the .nupkg file.

 

Consuming the package

So, our package is all set but how do we consume it? The easy way is to upload it to NuGet.org or another host service. But that’s not an option if we want to keep it private or in-house.

 

That means we need to host it on a local file system or on one of your network drives. All you need to do is copy the .nupkg file to your source of choice. You can then add this to a list, either in Visual Studio’s NuGet Package Manager settings (found in the options window and is added by specifying a name and source url/folder) or with the command line: ‘NuGet sources Add – Name ‘local packages’ – Source <filepath>’.

 

 

And that’s it! You can both create your own package and consume it from a private feed. All that’s left to do is ensure you update the version number whenever you make changes to ensure your projects pick up the update.

Happy coding!

<?xml version="1.0"?>

<package>

  <metadata>

    <id>Unique.Id</id>

    <version>1.0.0</version>

    <authors>Enjoy Digital</authors>

    <owners>Enjoy Digital</owners>

    <requireLicenseAcceptance>false</requireLicenseAcceptance>

    <description>Example NuGet Package</description>

    <iconUrl>http://example.com/example.png</iconUrl>

    <copyright>Copyright © 2018</copyright>

    <dependencies>

      ...

    </dependencies>

  </metadata>

  <files>

    ...

  </files>

</package>

 

To determine which files should be included in the project, specify them in the ‘files’ node:
...

<files>

  <file src="readme.txt" target="" />

  ...

</files>

...

comments powered by Disqus

Articles by Rachel Drake

I am a backend developer here at Enjoy Digital specialising in Sitecore.