NuGet packages with code snippets

by Geert 18. February 2011 11:55

I always try to create NuGet packages for Catel to create an “as easy as possible” installation experience. Catel uses lots of code snippets to increase the speed at which a developer can create view models and more. So, how do we deploy these code snippets using NuGet?

Good question, I couldn’t find the answer so I decided to create the solution myself. The cool thing about NuGet is that it supports Powershell scripts. Keep in mind that this is not an introduction to NuGet, I expect you to already know how to create basic NuGet packages.

Files preparation

First,  create a “tools” folder if you haven’t already created that for your NuGet package. Then, copy all your code snippets into that directory so you have something like the image below:

image

As you can see, both the code snippets as the scripts are located in the “tools” folder of the package. For more information about what kind of scripts can be included, please read the NuGet documentation.

Install script

Let’s take a look at the content of the scripts. I created a Powershell script (install.ps1) with the following content:

   1: # Install script for code snippets with NuGet
   2: # =============================================
   3: # Written by Geert van Horrik (see http://blog.catenalogic.com)
   4: #
   5: # Version 1.0 / 2011-02-18
   6: #
   7: # Required call to get environment variables
   8:  
   9: param($installPath, $toolsPath, $package, $project)
  10:  
  11: # You only have to customize the $snippetFolder name below,
  12: # don't forget to rename the $snippetFolder of the other file
  13: # ("uninstall.ps1") as well
  14:  
  15: $snippetFolder = "Catel"
  16:  
  17: # Actual script start
  18: $source = "$toolsPath\*.snippet"
  19: $vsVersions = @("2005", "2008", "2010")
  20:  
  21: Foreach ($vsVersion in $vsVersions)
  22: {
  23:     $myCodeSnippetsFolder = "$HOME\My Documents\Visual Studio $vsVersion\Code Snippets\Visual C#\My Code Snippets\"
  24:     if (Test-Path $myCodeSnippetsFolder)
  25:     {
  26:         $destination = "$myCodeSnippetsFolder$snippetFolder"
  27:         if (!($myCodeSnippetsFolder -eq $destination))
  28:         {
  29:             if (!(Test-Path $destination))
  30:             {
  31:                 New-Item $destination -itemType "directory"
  32:             }
  33:         
  34:             "Installing code snippets for Visual Studio $vsVersion"
  35:             Copy-Item $source $destination
  36:         }
  37:         else
  38:         {
  39:             "Define a value for snippetFolder variable, cannot be empty"
  40:         }
  41:     }
  42: }

The code should fairly speak for itself. On my test-machine (Windows 7, 32-bit, only VS 2010 installed), this is the output:

“Installing code snippets for Visual Studio 2010”

Uninstall script

A good installer should also uninstall the contents if required. Therefore, there is also an uninstall script (uninstall.ps1):

   1: # Uninstall script for code snippets with NuGet
   2: # =============================================
   3: # Written by Geert van Horrik (see http://blog.catenalogic.com)
   4: #
   5: # Version 1.0 / 2011-02-18
   6: #
   7: # Required call to get environment variables
   8:  
   9: param($installPath, $toolsPath, $package, $project)
  10:  
  11: # You only have to customize the $snippetFolder name below,
  12: # don't forget to rename the $snippetFolder of the other file
  13: # ("install.ps1") as well
  14:  
  15: $snippetFolder = "Catel"
  16:  
  17: # Actual script start
  18: $source = "$toolsPath\*.snippet"
  19: $vsVersions = @("2005", "2008", "2010")
  20:  
  21: Foreach ($vsVersion in $vsVersions)
  22: {
  23:     $myCodeSnippetsFolder = "$HOME\My Documents\Visual Studio $vsVersion\Code Snippets\Visual C#\My Code Snippets\"
  24:     if (Test-Path $myCodeSnippetsFolder)
  25:     {
  26:         $destination = "$myCodeSnippetsFolder$snippetFolder"
  27:         if (!($myCodeSnippetsFolder -eq $destination))
  28:         {        
  29:             if (Test-Path $destination)
  30:             {
  31:                 "Uninstalling code snippets for Visual Studio $vsVersion"
  32:                 Remove-Item $destination -recurse -force
  33:             }
  34:         }
  35:         else
  36:         {
  37:             "Define a value for snippetFolder variable, cannot be empty"
  38:         }        
  39:     }
  40: }

How should I use the scripts for my own packages?

I developed the scripts in such a way that they are compatible with all known versions of Visual Studio (2005, 2008 and 2010).  The cool thing is that you only have to modify the following line in both scripts:

   1: $snippetFolder = "Catel"

Just give the folder your own name. Don’t think to be smart by renaming it to “”. You will then delete all the code snippets of the user, and I can tell you one thing: they won’t be happy… I added a protection for this in the script, but just make sure you don’t do that.

Downsides

The downside of this script is that if a package is used in multiple projects, and it is uninstalled in one project, the code snippets are uninstalled and thus not available for other projects. You might want to choose to keep the files on the system as garbage, it’s up to you.

Downloads

NuGet Code Snippets scripts.zip (1.32 kb) [Downloads: 373]

Disclaimer:
Only tested on Windows 7 32-bit with Visual Studio 2010 and NuGet 1.1.

Tags:

NuGet

Comments (1) -

David Alpert
David Alpert Canada
2/19/2011 7:00:25 PM #

As an alternative to bundling the code snippets with the core nuget package, you might consider wrapping up the snippets into a separate nuget package such that I could choose to install or uninstall the snippets independently from adding/removing the core catel package from any given project.

Pingbacks and trackbacks (2)+

Comments are closed

About the Author

Geert van Horrik is an independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, Silverlight, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!