Create a FULL Local NuGet Repository with PowerShell

Create a FULL Local NuGet Repository with PowerShell

UPDATE: with the release of NuGet 1.0 RTM, this script has some breaking changes. For an updated version, see this post by Jon Galloway:.

is simply awesome. Despite its relative infancy, it has already established itself as the standard for.NET package management. You can easily add packages from the or even from a local directory on your machine. Phil Haack already has an excellent post describing how you set up a local feed. His post does a great job of explaining *how* to set up the feed, but how do you get packages (that you didn’t create locally) to put in there in the first place? One way is to simply install a package and then grab the *.nupkg file locally and then copy/paste it into whatever folder you’ve designated as your local NuGet feed. For example, if I’ve added the AutoMapper package, I can just grab it from here:

local nuget automapper

But what if I want to grab ALL of the packages from the local feed and store them all locally in a single directory? Certainly, this copy/paste process is way to manual of a process. Fortunately, the OData feed for NuGet tells you all you need to know.

nugetfeed

Notice how the tag shows me the URI that I need to hit in order to download the file? That makes life easy. Now all we need is a few lines of PowerShell to put everything in a single local directory:

1: $webClient = New-Object System.Net.WebClient

2: $feed = [xml]$webClient.DownloadString("http://feed.nuget.org/ctp2/odata/v1/Packages")

3: $destinationDirectory = "C:\development\LocalNuGetTest"

4:  

5: $records = $feed | select -ExpandProperty feed | select -ExpandProperty entry | select -ExpandProperty content

6:

7: for ($i=0; $i -lt $records.Length; $i++) {

8: $url = $records[$i].src

9: $startOfQuery = $url.IndexOf("?p=") + 3

10: $fileName = $url.Substring($startOfQuery, $url.Length - $startOfQuery)

11: $fullPath = ($destinationDirectory + "\" + $fileName)

12: $webClient.DownloadFile($records[$i].src, ($destinationDirectory + "\" + $fileName))

13: }

I specify my destination directory on line 3. Then I expand the XML elements into an object that PowerShell understands. After that it’s a simple matter of string parsing and downloading the files with the WebClient.

Now that you have all the packages locally, you can work offline even when you don’t have access to the public feed.

posted on Wednesday, November 10, 2010 3:34 AM Print

This article is part of the GWB Archives. Original Author: Steve Michelotti

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.