creating an nuget package for episerver

18
Turning my Reports into a NuGet Package. In my previous post on creating an EPiServer Usage Report I had some additional tasks to complete. These tasks involved turning the reports into a nugget package to allow anybody to use them on their site. NuGet is a free and open-source package manager for the Microsoft development platform (previously known as NuPack). Since its introduction in 2010, NuGet has evolved into a large ecosystem of tools and services.

Upload: paul-graham

Post on 23-Feb-2017

318 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Creating an nuget package for EPiServer

Turning my Reports into a NuGet Package.In my previous post on creating an EPiServer Usage Report I had some additional tasks to complete. These tasks involved turning the reports into a nugget package to allow anybody to use them on their site.

NuGet is a free and open-source package manager for the Microsoft development platform (previously known as NuPack). Since its introduction in 2010, NuGet has evolved into a large ecosystem of tools and services.

Page 2: Creating an nuget package for EPiServer

RefactoringBefore I can create the package I need to I need to perform some simple refactoring and by tackling this first I reduce error that will find in my finial NuGet Package.

Firstly, I Renamed the UsageReports folder to CSharpBrew.UsageReports and updated the namespaces to match. This will ensure we don’t deploy on top of existing.

Underneath I create a new folder called ‘Pages’ and moved all the ASPX files into it. Next comes the ‘ViewModels’ folder to hold the presentation models. Finally, I create a simple class ‘UsageReportsService.cs’ to hold all the reporting logic.

By moving this logic into a separate class I aid future development and gain the ability to write unit tests against this logic.

Page 3: Creating an nuget package for EPiServer
Page 4: Creating an nuget package for EPiServer

Create a new Project.As well as creating a new class library project in visual studio I also chose to create it in a different location from the website. This was done to allow me to version control it separately.

Now I simple moved the folder contents of CSharpBrew.UsageReports in our new project. Then I checked the namespace to remove any prefix, i.e. AllowDemoKit.

Now, If try to rebuild this project, I receive a large number of errors about missing assembly references.

Page 5: Creating an nuget package for EPiServer

This is expected and easily fixed and simply involves installing the CMS UI Core package, from the EPiServer Nuget Feed.

However, as I to target as broadest range of web sites as possible I need to install a lower version.

This can be done via the nugget package manager, or by searching the EPiServer Nuget repository and running the correct Package Manager Command.

Page 6: Creating an nuget package for EPiServer

PM> Install-Package EPiServer.CMS.Core -Version 7.6.0

Uploading to GitHubNow that I’ve a working project I wish to upload it to GitHub , so it’s available to everybody. This is done via a new public repository that also includes the Apache License . To ensure that I will still get some credit for my work and prevent some misuse.

Page 7: Creating an nuget package for EPiServer

Once I committed my work it’s available for anyone to download. http://bit.ly/1PiwIIi

Adding Packager

Page 8: Creating an nuget package for EPiServer

Now I need to package my project. To do this I’m using a Visual Studio extension called NuGet Packager . A simple project template for creating packages within Visual Studio. To install Packager we can search for in inside Tools -> Extensions and Updates dialog or through the Visual Studio Gallery .

This allows me to create a new project with the postfix of Packager, which will contains the package.nuspec. This is where my project is declared and the majority of the content is self-explanatory; however, two section are importance; the dependencies and files .

Dependencies are there to ensure the correct DLL’s are installed and prevent installation on incompatible projects i.e. we need EPiServer CMS 7.6 or greater. The version can be a singular of range. For instance, "1.2.10" works with one version, whereas version="[7.6.0,9)" is a range from 7.6 up to and including version 9.

<dependencies> <group targetFramework="net40"> <dependency id="log4net" version="1.2.10" /> <dependency id="EPiServer.CMS.Core" version="[7.6.0,9)" /> </group> </dependencies>

The files section contains all the content, we need to deploy and elements used to configure the deployment.

Page 9: Creating an nuget package for EPiServer

The first two sections copyies the (aspx files and web.config) into the content folder which will be deployed into the modules folder. The next I copy the Dll into lib\net45, to allows them to be referenced by Visual Studio.

Also as Packager is a separate project I will need use a relative path to the original project. I.e. by prefixing “..\CSharpBrew.UsageReports”.

<files> <file src="..\CSharpBrew.UsageReports\Pages\*.aspx" target="content\modules\CSharpBrew.UsageReports\Pages" /> <file src="..\CSharpBrew.UsageReports\web.config" target="content\modules\CSharpBrew.UsageReports\" /> <filesrc="..\CSharpBrew.UsageReports\Pages\binRelease\CSharpBrew.UsageReports.dll" target="lib\net45" /> <file src="web.config.*.xdt" target="Content\" /></files>

The finial node copies two xdt files, which are used to transform the web.config, via a Transformation Syntax . This Syntax consists of two attributes that can be attached to an Xml element. xdt:Locator is an locate the correct node which will be used with xdt:Transform to insert, update or delete an element or attribute.

Page 10: Creating an nuget package for EPiServer

web.config.install.xdt will transform the web.config when it’s added to project.

<episerver.shell> <publicModules> <add name="CSharpBrew.UsageReports" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"> <assemblies> <add assembly="CSharpBrew.UsageReports" /> </assemblies> </add> </publicModules> </episerver.shell>

web.config.uninstall.xdt will transform the web.config when it’s removed.

<episerver.shell> <publicModules> <add name="CSharpBrew.UsageReports" xdt:Transform="Remove" xdt:Locator="Match(name)"/> </publicModules> </episerver.shell>

Now when the Packager project is build it also produce the package; which is just a zip file with the extension of nupkg.

Page 11: Creating an nuget package for EPiServer

Installing the PackageNow I have a package I need to it into my website project. As I wish to test my package before its released I will install it from a local folder before it’s released.

Usefully the nuget.config supports multiple package sources, so I can deploy locally and to www.nuget.org as well.

<configuration> <packageSources>

<add key="NuGet official package source" value="https://www.nuget.org" /> <add key="Local package Source" value="C:\Projects\Packages" /> </packageSources><configuration>

To add additional package sources I open the Tools menu, clicked Options to go to Visual Studio settings. Next I expanded the NuGet Package Manger section and clicked Package Sources.

Finally, I clicked the green plus symbol and entered the name as Local package Source and location as C:\Projects\Packages

Page 12: Creating an nuget package for EPiServer

When I’m happy and have completed my testing I can update the nuget.config to now include an apikey to allow me to deploy to www.nuget.org.

<configuration> <apikeys> <add key="https://www.nuget.org" value="" /> </apikeys> <packageSources>

Page 13: Creating an nuget package for EPiServer

To Locate the apikey I first logged into NuGet and then clicked on account to see my key.

And that’s it all I now need to do is rebuild and the script will automatically publish to NuGet.

Page 14: Creating an nuget package for EPiServer

Finial ThoughtsNow I have a usable NuGet packages my next steps is to turn these reports into something more useful.

NuGet is a incredibly useful tool for integrating third library’s into your projects, but it’s also extremely useful for creating internal packages for private consummation and I have in several company creating nugget packages to allow reuse of work.

Finally, i’ve also made some change to NuGetPackage.ps1, for versioning and configuration support, please fill free to checkout my source.