Nuget.Downloader package - Download a local NuGet repository using Nuget - Jon Galloway

Nuget.Downloader package - Download a local NuGet repository using Nuget

NuGet supports multiple feeds, running against either a server or a simple folder / fileshare. See Phil Haack's post explaining both options for more information. I'm a big believer in running your own local NuGet feed for a lot of reasons - offline access, control over updates, and as an absolute must-have for demonstrations and training. I previously wrote up a NuGet Powershell script which pages through the NuGet OData feed and downloads local copies of the packages. It's a little more complex than you'd guess, since the script needs to follow redirections and page links, plus I added in some options to skip downloads for features you've got, only grab the most popular X packages, etc.

I'd always assumed this would end up as a NuGet package, although the use case is a little out of the ordinary. The primary use case of NuGet is to add things (libraries, code, and content) to an existing application, and this NuGet package doesn't work at the project level at all. But, it's just so handy - and there had been enough requests for it - that I couldn't help myself.

I'd actually started on this when Eric Hexter mentioned that he was working on it as well, so I merged what he'd done, removed all the affiliate ad codes he'd snuck in, and uploaded it yesterday. So, here's NuGet.Downloader.

2011-03-10 15h46_44

How to use it

  1. Open a project in Visual Studio (it doesn't matter which - if you try to install a package outside of a solution, you'll get slapped down with "The current environment doesn't have a solution open").
  2. Open the Package Manager console (View / Other Windows / Package Manager Console).
  3. Type "install-package NuGet.Downloader" - you should see "Successfully installed 'Nuget.Downloader 1.0.0.5'." (with whatever the current version number is).
  4. This adds a new commend to the Package Manager console, Download-Packages. You can run it without any parameters, which will grab the top 500 most popular NuGet packages and drop them in a new LocalNuGet folder inside your My Documents folder.
  5. If desired, uninstall the package using Uninstall-Package.

Parameter options:

  • feedBaseUrl (default: official NuGet package feed, following redirections) - you can point this at a corporate NuGet server or some other location
  • latest (default: $true) - when true, only grabs the latest version of each package
  • overwrite (default: $false) - when true, downloads and overwrites all packages regardless of whether you already have a copy
  • top (default: 500) - number of packages to download, ordered by package popularity. If $null, downloads all packages; if 1 it would download the most popular package
  • destinationDirectory - (default: %HOMEPATH%\Documents\NuGetLocal) full path to the directory you'd like the packages downloaded to. Note: I changed this from LocalNuGet in the Powershell to NuGetLocal in the package, as I thought it made more sense

Example - downloading the top 10 most popular packages with overwrite option, then removing the package:

PM> Install-Package NuGet.Downloader
Successfully installed 'Nuget.Downloader 1.0.0.5'.   PM> Download-Packages -top 10 -overwrite $true
Downloading latest 10 packages (by download count)
downloading from http://packages.nuget.org/v1/FeedService.svc/Packages?$filter=IsLatestVersion eq true&$orderby=DownloadCount desc&$top=10
Complete. Local packages saved to C:\Users\Jon\Documents\NuGetLocal
PM> Uninstall-Package NuGet.Downloader
Successfully uninstalled 'Nuget.Downloader 1.0.0.5'.
 
PM> 

Hope it helps you out, and please let me know how I can make it better.

Published Thursday, March 10, 2011 4:54 PM by Jon Galloway
Filed under:

Comments

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

Working great. Thanks for this. But this also begs for being able to run NuGet outside of Visual Studio. It not only avoid having to use a temp solution, but also opens up new possibilities...

Saturday, March 12, 2011 2:08 AM by Hadi Hariri

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

@Hadi - The NuGet core API's are available outside of Visual Studio - see the NuGet FAQ's here: nuget.codeplex.com/wikipage

For this specific scenario, you can use the PowerShell script without any NuGet dependency: weblogs.asp.net/.../downloading-a-local-nuget-repository-with-powershell.aspx

But I generally agree. It would also be nice to be able to run some NuGet packages without having a solution open, although I don't know how that would work.

Saturday, March 12, 2011 1:58 PM by Jon Galloway

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

would be nice to have an option that checked the folder specified for NuGetLocal and downloaded, if necessary, newer versions of all packages found in that folder.

Monday, March 21, 2011 6:57 AM by leparduk

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

Can this download specific package names and using wildcards for parameters?

e.g. I like to download all the EnterpriseLibrary.* packages.

Is this possible?

Friday, November 25, 2011 11:44 AM by Eric

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

Really nice script Jon! Thanks.

A really handy feature would be if the tool could download the latest version of only those packages already present in the destinationDirectory. Then you'd end up with super simple tool for maintaining a selective nuget.org cache.

Tuesday, April 3, 2012 2:59 AM by Daniel

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

this is a great idea. however, if you want to create a local repository for a 'real reason', like your server is cut off from the internet and your workstation does not have install permissions, having to get the nuget-downloader from nuget is a chicken-and-egg dilemma.

Wouldn't it be reasonable to post the actual nuget package on your site so that we can get nuget packages with your tool without having to figure out how to get your nuget package from nuget when that is the problem to begin with?

Thanks,

Kimball

Wednesday, May 23, 2012 1:26 PM by kimball johnson

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

Also, when I try to use the nuget command line to install nuget-downloader, I get an error:

C:\Active\Install>nuget install nuget.downloader -version 1.0.0.6

Could not load file or assembly '0 bytes loaded from System, Version=4.0.0.0, Cu

lture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. An a

ttempt was made to load a program with an incorrect format.

Wednesday, May 23, 2012 1:31 PM by kimball johnson

# re: Nuget.Downloader package - Download a local NuGet repository using Nuget

Great Tool Jon!

But I agree with Daniel, It would be great if this tool could download the latest version of packages already presents in the destination directory.

Thursday, August 30, 2012 8:17 AM by Steph