Monthly Archives: July 2011
Installing all NuGet Packages in the Solution

The benefits of using NuGet to manage your project dependencies should be well-known by now.

If not, read this.

There’s one area where NuGet is still somewhat deficient: Team Workflow.

Ideally, when you begin work on a new solution, you should follow these steps:

1) Get the latest version of the source code.

2) Run a command to install any dependent packages.

3) Build!

It’s step 2 in this process that is causing some trouble. NuGet does offer a command to install packages for a single project, but the developer is required to run this command for each project that has package references. It would be nice if NuGet would install all packages for all projects using packages/repositories.config, but at the time of this writing it will not. However, it is fairly easy to add a powershell function to the NuGet package manager console that will do this for you.

First, you should download the NuGet executable and add its directory to your PATH Environment variable. I placed mine in C:devutilsNuGet<version>. You’ll need to be able to access the executable from the command-line.

Second, you need to identify the expected location of the NuGet Powershell profile. You can do this be launching Visual Studio, opening the Package Manager Console, type $profile, then press enter. The console will show you the expected profile path. In Windows 7 it will probably be: “C:Users<user>DocumentsWindowsPowerShellNuGet_profile.ps1”

Next you need to create a file with that name in that directory.

Next, paste the following powershell code into the file:

function Install-NuGetPackagesForSolution()
{
    write-host "Installing packages for the following projects:"
    $projects = get-project -all
    $projects
   
    foreach($project in $projects)
    {
        Install-NuGetPackagesForProject($project)
    }

}

function Install-NuGetPackagesForProject($project)
{
    if ($project -eq $null)
    {
        write-host "Project is required."
        return
    }

    $projectName = $project.ProjectName;
    write-host "Checking packages for project $projectName"
    write-host ""
   
    $projectPath = $project.FullName
    $projectPath
   
    $dir = [System.IO.Path]::GetDirectoryName($projectPath)
    $packagesConfigFileName = [System.IO.Path]::Combine($dir, "packages.config")
    $hasPackagesConfig = [System.IO.File]::Exists($packagesConfigFileName)
   
    if ($hasPackagesConfig -eq $true)
    {
        write-host "Installing packages for $projectName using $packagesConfigFileName."
        nuget i $packagesConfigFileName -o ./packages
    }

}

Restart Visual Studio 2010. After the package manager console loads, you should be able to run the Install-NuGetPackagesForSolution command. The command will iterate over each of your projects, check to see if the project contains a packages.config file, and if so run NuGet install against the packages.config.

You may also wish to do this from the PowerShell console outside of visual studio. If you are in the solution root directoy you can run the following two commands:

$files = gci -Filter packages.config -Recurse
$files | ForEach-Object {nuget i $_.FullName -o .packages}