Monthly Archives: September 2015

My First Nuget Package. Written. Packaged. Published.

I had to get my hands dirty building a powershell extension for .NET Visual Studios. As a java developer, .NET, powershell, nuget, are all terms unfamiliar to me. So best way to learn, is to just jump right into it.

Write a PowerShell script

Open Visual Studio and create a new project. Then add a new file (Add > New Item > Text File) and call it MyFirstScript.ps1

Let’s create a “Hello World” inside MyFirstScript.ps1

Write-Host "hello world"

Then in the “Package Manager Console” window, type

PM> .\MyFirstScript.ps1

You should get a ‘hello world’ output

Check out tutorials on the web to go beyond “hello world” but we’re moving on now…

Create a Nuget Package

Now, I’m going to have you modify MyFirstScript in a few ways.

First, change its extension to .psm1.

Next wrap the code in a function and add an Export-ModuleMember line to tell it what function to expose.

You should now have something like the following.

MyFirstScript.psm1:

function Write-HelloWorld {
  Write-Host "hello world"
}

Export-ModuleMember Write-HelloWorld

Your exported function should follow appropriate naming scheme, otherwise it will yell at you later. For a list of proper verbs to use in your function name, go here:
Approved Verbs for Windows PowerShell Commands

Now create another file, that will import your powershell script as a module. Call this init.ps1

init.ps1:

param($installPath, $toolsPath, $package, $project)

Import-Module (Join-Path $toolsPath MyFirstScript.psm1)

Just make sure the last parameter of Import-Module matches the name of your script.

Now I need you to put both MyFirstScript.psm1 and init.ps1 into a folder called tools.

Open a terminal to the folder where the tools folder resides.
Type the following

c:\> nuget spec MyFirstScript

This will create a MyFirstScript.nuspec.
Open this file and modify the values. Now it might look something like this

MyFirstScript.nuspec

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyFirstScript</id>
    <version>1.0</version>
    <authors>Me</authors>
    <owners>Me</owners>
    <licenseUrl>http://example.com</licenseUrl>
    <projectUrl>http://example.com</projectUrl>
    <iconUrl>http://example.com</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>prints hello world</description>
    <releaseNotes>alpha release</releaseNotes>
    <copyright>Copyright 2015</copyright>
    <tags>testscript</tags>
  </metadata>
</package>

On the terminal again, type

c:\> nuget pack MyFirstScript.nuspec

This should create a binary file called MyFirstScript.1.0.nupkg

You’ve just created a nupkg (nuget package).

Import your Nuget Package

You can import this local nupkg to your Visual Studio project.
From the “Package Manager Console” just type

PM> Install-Package MyFirstScript -Source C:\pathToMyFirstScriptFolder

Publish your Nuget Package for Others to Import

If you wanted to share this more widely with others, you’ll want to upload it to nuget.org.

To do that, register an account and go here to upload your MyFirstScript.nupkg

Nuget Upload Page
Screen Shot 2015-09-18 at 3.58.55 PM

After the upload, your nuget package will be hosted and be available for all to install

Screen Shot 2015-09-18 at 4.01.20 PM

To install it, go to the “Package Manager Console” in visual studios. (Make sure you have a project opened). And type

PM> Install-Package MyFirstScript

It’s that easy!

Tagged , , ,