.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2013

Number of NuGet Packages Increases Dramatically

Last week I posted a PowerShell script that downloads all the NuGet packages from the NuGet Feed.

At that time the number of packages was an astounding 393. Five days later that number has increased by 100.

Adding third party libraries to your .NET projects has never been easier!

Currently rated 1.3 by 369 people

  • Currently 1.330623/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by CynotWhyNot on Saturday, January 22, 2011 12:34 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Creating a Complete Local NuGet Repository

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!

Currently rated 1.4 by 112 people

  • Currently 1.446429/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: .NET | ASP.NET MVC | VS2010
Posted by CynotWhyNot on Saturday, January 15, 2011 6:34 PM
Permalink | Comments (0) | Post RSSRSS comment feed