Increase your API usability by teaming up NuGet and Visual Studio

Tags van Technorati:

In my previous post I described a way to use NuGet for packaging libraries, often used in projects. This post is an addendum to my post ‘enter 2014 with NuGet’ look at the additional features possible. In this post i’m looking into Visual Studio features (project templates) and the NuGet package feature PowerShell.

I’ve been working on an API (see my next posts, Service Bus Series - ‘the case for service bus’), and during the development I wanted to make sure that explaining the API to another co-worker was a nicer experience than just going through the code. To ensure that the developers could easily connect and understand the usage of the API, I decided to create sample projects using templates. Templates are a great way to provide the user with a sample project from within Visual Studio. I wanted more… and with the features available in NuGet packages, i’ve found a way that worked for my scenario. I’ve been using the principles;

  1. Each project in Visual Studio can be exported as a template
  2. New Projects can be created based on a template, the user get the template in his overview of available project types
  3. Exporting templates results in a .Zip file in the <UserProfile> \ Documents \ Visual Studio <version> \ Templates \ Project Templates directory .
  4. NuGet allows by means of a 'Tools' content type to perform custom actions, which are executed when creating a reference of the NuGet package from PowerShell

By combining #1-3 with #4 we can create a NuGet package which adds a project template to the user of the NuGet package. This can be simply done by following the steps mentioned below;

 

image

 

  • Export Template

image

In Visual Studio select ‘File’ \ ‘Export Templates’ (in the solution which contains the project to be exported as template)

    1. Select the type ‘Project template’
    2. Select the project

image

3. Go Through the wizard steps

image

Note: This .zip file can be used in a different Visual Studio environment

 

Files can be added into a NuGet package, using the Content folder. In the NuGet package Explorer we can add a template;

1. Add a content folder

image

2. Add the content

image 

3. Browse to the exported template

 image

4. The endresult in the package explorer

image

 

  • NuGet and PowerShell

The final step is the configuration of a PowerShell script, which allows the Content to be installed in the folder of the user so that the project template is available. For this we need to perform the steps;

1. Add a tools folder

image

2. Add Install.ps1 (which is executed when you install the package

image

2. Add Install.ps1 (which is executed when you install the package) and add the following script

image

Note: This script will install the package into the user templates folder, and assumes (visual studio version, zipfile name)

Note 2: the complete script is copied below;

param($installPath, $toolsPath, $package, $project)
$documents = [System.IO.Path]::Combine((Get-Item env:USERPROFILE).Value, "Documents\Visual Studio 2013\Templates\ProjectTemplates")
$templateFile = "Contoso.ServiceBus.API.zip"
$template = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($project.FileName), $templateFile)
$templateDestination = [System.IO.Path]::Combine($documents, $templateFile)
Copy-Item $template $templateDestination

  • The endresult

Assuming the package is exported into the NuGet repository, the NuGet package source is configured, a new solution has been created, we can now use the package.

1. Manage NuGetPackages and click install

image

The PowerShell script is executed;

image

 

2. From now on we can create a sample project based on the template

image

image

 

If you are commited to re-using packages, and want to create an API or reusable library, this can be quite helpful. Although integrating NuGet in TFS is different, all the features presented are available, so improving the Dev-Ops integration using TFS is a nice way to improve this proces.

For me, this allowed me to provide more assistence to the end user of the API. I’ve currently devided the API into an package and created another package with the Templates. This post just gives an overview of the power that you have with NuGet.

 

HTH,

Sander.

Comments

Popular posts from this blog

Azure implementation guidelines

UK Connected Systems User Group – BizTalk Services questions

Setting up a build server with the BizTalk Deployment Framework