Tip: Install missing NuGet packages using Albacorebuild and Rake

UPDATED!

See comments below, and you will learn that Albacore uses deferred execution, hence not all packages.config will be treated. The corrected code looks like this:

task :installNuGetPackages do
  FileList["Solution/**/packages.config"].each { |filepath|
    sh "NuGet.exe i #{filepath} -o Solution/packages"
  }
end

Which also removes the dependency on Albacore.

End update

As I’m using Albacorebuild and Rake together with TeamCity and GitHub to setup a continuous integration (CI) flow I’m about to stop checking in NuGet packages in my source code repository at GitHub. What I do want, is a simple way for the developer and the CI server to use rake to install any missing NuGet packages. The NuGet.exe tool allready supports this, but per project: read more at NuGet.org. If you have a look at the link you will also see that they give an example of using a Pre-build event for each project. Not fun. Lets have a look at another solution.

The solution using Albacorebuild and Rake

Create a rakefile and define a task that uses NuGet.exe and use the command parameters defined at the link above, but do it for each packages.config file located under the solution directory.

setup.rb

require 'albacore'
#--------------------------------------
task :default => [:installNuGetPackages]
#--------------------------------------
desc "Install missing NuGet packages."
exec :installNuGetPackages do |cmd|
  FileList["Solution/**/packages.config"].each { |filepath|
    cmd.command = "NuGet.exe"
    cmd.parameters = "i #{filepath} -o Solution/packages"
  }
end

So given this structure:

One can simply use rake to install any missing packages:

Summary

Even if it was a simplified rakefile I showed here, you could easily incorporate the task in your normal rakefile and let the CI server execute it, as well as perhaps just have a simple bat-file for devs to setup their local environment.

//Daniel

4 thoughts on “Tip: Install missing NuGet packages using Albacorebuild and Rake

  1. Pingback: DotNetShoutout

  2. Pingback: Tip: Install missing NuGet packages using... | .NET | Syngu

  3. good tip but the Albacore ExecTask uses a deferred execution so it only runs on the last package config that it finds. To run NuGet on each, I used a standard rake task and called the command with sh instead.

    sh “NuGet.exe i #{filepath} -o /…”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s