Using NuGet with TFS

Amongst other things, I use TFS and NuGet. When I move to a new machine, TFS will give me the code but the packages will be missing.

I tried the solution context menu command to “Enable NuGet Package Restore” and checked it in but when I updated the solution files on the other machine it made a complete pig’s ear of things and half of the projects in the solution wouldn’t load because of missing files. So I backed that out…

ASIDE: why is it so hard to back out a big check-in, in TFS? Surely, one of the major reasons for source control is to save your bacon? So, why is it so hard to back out a change?  Why can’t I simply point at a check-in and go “Revert to this point”.  Yes, it should ask me if I’m sure.  Possibly, it should point out how dangerous and possibly stupid this is.  But it should be that easy, not like this. [Not that I’m complaining about that post – saved me lots of fun.]

… and had a poke around t’Internet. As shown here, it’s easy to get the packages.

First, to download the packages, you need to get nuget.exe, which you can find here.

Now, my solutions are often quite large and I wanted an easier way that running that for every project folder. A bit of fiddling with PowerShell (which is also always harder than I think it should be) and I arrived at this one-liner which iterates through your solution folder and downloads missing packages:

PS C:\Users\Public\...{SolutionDir}\.nuget> gci -include "packages.config" -recurse ..\ | % { .\nuget install $_.FullName -o Packages }

nuget.exe is in the .nuget folder where Enable NuGet Package Restore put it.

If you try it, you should see something like this:

All packages listed in packages.config are already installed.
Successfully installed ‘CommonServiceLocator 1.0’.
Successfully installed ‘Prism.UnityExtensions 4.1.0.0’.
Successfully installed ‘Unity 2.1.505.0’.
Successfully installed ‘Prism 4.1.0.0’.
Successfully installed ‘SharpSerializer 2.18’.
All packages listed in packages.config are already installed.
Successfully installed ‘NUnit 2.6.0.12054’.
Successfully installed ‘NSubstitute 1.4.2.0’.
All packages listed in packages.config are already installed.
All packages listed in packages.config are already installed.
All packages listed in packages.config are already installed.

I haven’t tested it extensively so, as with everything here, use it advisedly.