I have just added my first NuGet package, and you may no know what NuGet is? so what is NuGet it’s a Package management add on for Visual Studio 2010 and above (maybe more as it is integrated with Windows PowerShell).
Figure 1 – Install NuGet
See Installing NuGet and then you will need NuGet.exe Command Line and or NuGet Package Explorer v1.1 this is the GUI version (and unlike Scott Hanselman I like the GUI and try to avoid the DOS as much as possible I’m with Phil Haack on that see NuGet In Depth: Empowering Open Source on the .NET Platform on Channel9) and can do a lot of what the command line tool does.
You can get loads on information on NuGet Docs.
OK so I’m going to assume your all setup and ready to go.
Creating the Package
NuGet has some Package Conventions and we should follow them (I’ll not explain them just go and have a read as documentation can be updated from time to time) for out package lets first of all look at what we want to package.
For this Package I want to package up Scott Hunter’s old Sample for Displaying Images from the Database using Dynamic Data (DBImage) and it’s update Sample for Displaying Images Updated + Screencast note the latest version of this is in the old Dynamic Data Futures VS2008 SP1 RTM for DD 3.51 SP1.
Figure 2 – Embedded image field template
This consists of several parts;
- Read-Only field template
- Edit field template
- Image Format attribute
- Linq extension methods
- Image http handler.
- Web.Config settings
I have decided to keep things simple for out first NuGet Package and all the above will go into one package some of the above could be shared and as such we could spilt it between several packages and have dependencies but we will keep it simple for now.
so to build our package there are some things we need to know like where do we put our files;
Figure 3 – Simple Package folder structure
As you can see I have broken the folder structure into three parts, the first part is quite simple, it’s the readme file which will give basic information about the package.
Content (files to be copied on install)
Here you can have three types of file;
- Copied as is with no changes.
- Transform files (see Configuration File and Source Code Transformations for more info)
- .transform or Configuration File Transformations
- .pp or Source Code Transformations
The “.transform” files transform web.config or app.config files by adding/merging their content with the project’s configuration file.
While the “.pp” files are files that are transformed as they are copied to the project in the same folder structure they have in the package. These files are a little more complex than the configuration files, as they have Visual Studio project properties in the code and these are used to transform the code files during install ((see Configuration File and Source Code Transformations and the Specifying Source Code Transformations section for more info).
This folder contains any DLLs that are required, not that the “net40” subfolder this indicates that the DLL supports .Net 4 you could have several version that support different versions of the .Net framework here we will just support .Net 4.
Note this is just the basic of where you need to put files for your package to work.
now we have out files ready in a folder structure we need to create our “nuspec” file, this is an XML file containing details (the manifest) about the package you can manually create this or you can use NuGet Package Explorer to create this see Using A GUI (Package Explorer) to build packages once created you can manually edit the “nuspec” file manually if you like.
You then place your “nuspec” file in the root of the package folder structure or if you follow Using A GUI (Package Explorer) to build packages you can add them directly.
Once done you just publish and it works!
The real power come with PowerShell scripts see Scaffolding – ASP.NET, NuGet, Entity Framework Code First and More with Steve Sanderson he shows what you can really do.
I am going to add more complex post here as I discover new and interesting feature of NuGet.