How to extend NuPack to complement its behavior

0
101

Introduction

Installing NuPack nuget package on a project allows you to automated creation of nuget package. Indeed, it manage common library template and console application as build action.

General description can be found in this tips : how to create a nuget package on each Visual Studio build?

NuPack comes with an extension system based on nugets package thanks to the package NuPack Extension 4.0+

NuPack Extension 4.0+

NuPack extension 4.0+ is a nuget package to install if you want to improve NuPack behavior. Indeed, after installation of this nuget package, you will be able to implements an interface named NuPack.Extension.IPlugin.

We can see interface is very simple and required to implements only Update and Dispose methods :

  • Update method have 2 arguments : Setting thats represents arguments passed to NuPack (solution, project, configuration, plateform & assembly) and PackageBuilder that is a object come with NuGet.Core nuget package.

  • Dispose method come with System.IDisposable interface. This method will be called only after full NuPack process. By this way you will be able to do something more after nuget package creation. (publish to a nuget server for example)

How to : step by step

Let’s create an extension to automatically set license URL to MIT license.

  1. Create a library project


     

  2. Install NuPack Extension 2.1.0+


     

  3. Create a class and implements NuPack.Extension.IPlugin 


     

  4. Install NuPack to create your nuget package containing your extension


     

  5. Build the project to generate the nuget package.

note : the generated nuget package has a custom structure defined by NuPack for extension recognization. 

Test extension

  1. Create a library project


     

  2. Install NuPack 3.4.2+ and Install your extension (you can refer to this tips to create a local store to test you nuget package)


     

  3. Build project and open it with nuget package explore


     

It sound good, extension work!

Conclusion

NuPack can help you to save time by producing your nuget package automatically at build time with Visual Studio / MSBuild. You can create an extension for NuPack materialized as a nuget package. As the extension is a nuget package it can easily be versioned, deployed and updated. This technique offers the possibility of performing a very advanced processing and simply controlling the behavior of NuPack by composing with the extensions. 

LEAVE A REPLY