I can’t say enough about it, NuGet is great!
NuGet is a .NET package management system integrated into VS2010. It is available as part of the ASP.NET MVC 3 download or separately from Codeplex. It’s free and open source and makes adding third party (or in house) libraries a snap.
Out of the box, the NuGet packages are retrieved from the internet, however, with very little configuration NuGet can retrieve packages locally or from a shared drive. Phil Haack as excellent post describing this configuration.
Steve Michelotti has an excellent post on how to use PowerShell to automatically pull down the NuGet packages from the internet an store them locally. The script is based on an early version of NuGet. Since then the location for the packages has changed, furthermore, the new feed will only list 100 packages at a time.
I have expanded on Steve’s PowerShell script allowing all the packages to be downloaded:
1: $webClient = New-Object System.Net.WebClient
2:
3: $feed = [xml]$webClient.DownloadString("http://packages.nuget.org/v1/FeedService.svc/Packages()")
4: $destinationDirectory = "C:\dev\nuget-lib"
5: $records = $feed | select -ExpandProperty feed | select -ExpandProperty entry | select -ExpandProperty content
6:
7: while($records.Length -gt 0)
8: {
9: for ($i=0; $i -lt $records.Length; $i++) {
10: $url = $records[$i].src
11: $start = $url.IndexOf("/Download/") + 10
12: $packageAndVersion = $url.Substring($start, $url.Length - $start)
13: $package = $packageAndVersion.Substring(0, $packageAndVersion.IndexOf("/"))
14: $start = $packageAndVersion.IndexOf("/") + 1
15: $len = $packageAndVersion.Length - $packageAndVersion.IndexOf("/") -1
16: $ver = $packageAndVersion.Substring($start, $len)
17:
18: $fileName = $url.Substring($startOfQuery, $url.Length - $startOfQuery)
19: $fullPath = ($destinationDirectory + "\" + $fileName)
20: write-host "Created package: $package.$ver.nupkg"
21: $webClient.DownloadFile($records[$i].src, ($destinationDirectory + "\" + $package + "." + $ver + ".nupkg"))
22: }
23: $feed = [xml]$webClient.DownloadString("http://packages.nuget.org/v1/FeedService.svc/Packages()?`$skiptoken='$package','$ver'")
24: $records = $feed | select -ExpandProperty feed | select -ExpandProperty entry | select -ExpandProperty content
25: }
The trick is to continue querying the feed until all the packages are downloaded. The feed will only list 100 packages at a time. I could not find any way to override this behaviour. Fortunately, the feed contains the URL to fetch the next set of packages (Fiddler to the rescue). This is done on line 23 of the Powershell script, which adds the $skiptoken to the end of the URL.
The script will deposit all the NuGet packages to a local directory c:\dev\nuget-lib (line 4). Go ahead and change the location to meet your needs.
At the time of writing this post the feed had a total of 393 packages. That ‘s right 393 packages!