NuGet Package To Automatically Create Your NuGet Packages

I've also created a NuGet package called "Create New NuGet Package From Project After Each Build" that you can include in your Visual Studio project that will call the New-NuGetPackage.ps1 script from your project's Post-Build events to automatically create a NuGet package every time your project builds. So just add that NuGet package to your project and instantly you will have a NuGet package (.nupkg) created for your project that is always up-to-date with your last successful build. The .nupkg file is placed in the project's output directory (i.e. in the same directory as the project's .dll/.exe).

Depending on the size of your project and it's dependencies, the time this adds to your build should not be noticeable (only a second or two during my tests). Obviously the NuGet package does not automatically push the package to the gallery on every build though, as that would be very wasteful and would require more time each build.

Add The NuGet Package To Your Visual Studio Project

To add the "Create New NuGet Package From Project After Each Build" package to your project:

1. From Solution Explorer in Visual Studio, right-click on your project's References node and choose Manage NuGet Packages...
NavigateToManageNugetPackages.png

2. In the NuGet Package Manager, select Online from the menu on the left to search for packages online, and then in the search box on the top-right type in "New NuGet Package" (include the double quotes). When the "Create New NuGet Package From Project After Each Build" package appears, click its Install button.
InstallNuGetPackageFromPackageManager.png

3. That's it. You should now see a PostBuildScripts folder added to your project that contains the New-NuGetPackage.ps1 script and NuGet.exe.
FilesAddedToProject.png

The NuGet.exe is included so that you are not required to have NuGet.exe added to your PATH in order for the script to run. If you look in the project's properties you will also notice that a few lines were added to the Post-Build event section to call the New-NuGetPackage.ps1 script.

Now if you build your project the NuGet Package file should get created in your project's output directory.
NuGetPackageInOutputDirectory.png

Push Your Package To The NuGet Gallery

Since pushing your package to the NuGet Gallery is not automated, you still have to do that manually. Luckily you can simply use the New-NuGetPackage.ps1 script in the PostBuildScripts folder to pack and push the new NuGet Package to the Gallery:

1. Right-click on the New-NuGetPackage.ps1 file in Visual Studio and choose "Run with PowerShell".
Cannot resolve image macro, invalid image name or id.

2. It will then prompt you for the file to pack, so navigate up one directory and choose the project file (e.g. the .csproj, .vbproj, or .fsproj file).

3. When it prompts you if you want to push your package to the NuGet Gallery, choose "Yes".

This last step assumes you have setup your NuGet API key on your PC at some point. I plan on updating the script soon for it to prompt you for your API key if one cannot be found automatically.

Troubleshooting Errors

If you want to see output from the New-NuGetPackage.ps1 script and NuGet.exe, take a look at Visual Studio's Build Output window. This can be very helpful for identifying and troubleshooting any problems that you may have.

BuildOutputWindow.png

Last edited Jun 22, 2013 at 4:14 PM by deadlydog, version 26

Comments

jhauser May 19 at 9:31 AM 
Hello,

Thanks for this package!
Since we have different languages on dev pcs and build server and we need a transparent solution i replaced a part of the skript:

```
# Get the path the NuGet Package was created to, and write it to the output stream.
$start = $packOutput.LastIndexOf(':\') - 1
$end = $packOutput.IndexOf('.nupkg') + '.nupkg'.Length
$path = $packOutput.Substring($start,$end-$start)
if (Test-Path $path)
{
$nuGetPackageFilePath = $path
Write-Output $nuGetPackageFilePath
}
else
{
throw "nuget package not found"
}
```

A bit hacky, but it seems to work for german and english version.

Heslacher Nov 19, 2013 at 7:41 AM 
Thanks a lot. Just a side note for users of german VS:
Inside the New-NuGetPackage.ps1 file you need to change the line 1017 from
$rxNugetPackagePath = [regex] "(?i)(Successfully created package '(?<FilePath>.*?)'.)"
to
$rxNugetPackagePath = [regex] "(?i)(Das Paket \`"(?<FilePath>.*?)\`".)"
otherwise it will throw this exception: Could not determine where NuGet Package was created to.

Togakangaroo Oct 13, 2013 at 7:07 PM 
Very very nice, way easier for simple stuff than my standard psake scripts. Maybe this will finally get my coworkers publishing their stuff.

NuGetter Aug 20, 2013 at 4:59 PM 
Great work. You made my day!