Peter
Peter

Reputation: 1687

Download all packages from private nuget feed

I want to download all packages in all version from my private nuget feed. Thats it. I have no problem in using powershell, bash, package manager what ever.

I can't use a placeholder project - that references all packages and copy my cache, because I need all versions.

Any idea?

I am working with a private nuget-feed. The feed is kind of broken, but its not in my hand to fix it. So I have to go this way...

Upvotes: 5

Views: 9735

Answers (4)

Kyle Burkett
Kyle Burkett

Reputation: 1443

These solutions did not work for me, so the following is what I did. I was working to migrate TFS 2017 feed to Azure DevOps hosted feed.

First, I downloaded all packages from the on-premise self hosted feed using this script in Powershell ISE:

# --- settings ---
# use nuget v2 feed for this script
# v2 feed is sometimes same url as v3, just change /v3/index.json to /v2/ 
$feedUrlBase = "http://servername:8080/tfs/MOMCollection/_packaging/bbd0bd78-205f-48af-ac25-6eef6470adb4/nuget/v2/"  # be sure to include trailing slash 
# the rest will be params when converting to funclet
$latest = $false
$overwrite = $true
$top = $null #use $top = $null to grab all , otherwise use number
$destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "NuGetLocal"

# --- locals ---
$webClient = New-Object System.Net.WebClient

# following is required if authenticating to TFS using integrated credentials
$webClient.UseDefaultCredentials=$true


# --- functions ---

# download entries on a page, recursively called for page continuations
function DownloadEntries {
 param ([string]$feedUrl) 
 $feed = [xml]$webClient.DownloadString($feedUrl)
 $entries = $feed.feed.entry 
 $progress = 0
            
 foreach ($entry in $entries) {
    $url = $entry.content.src
    $fileName = $entry.properties.id + "." + $entry.properties.version + ".nupkg"
    $saveFileName = join-path $destinationDirectory $fileName
    $pagepercent = ((++$progress)/$entries.Length*100)
    if ((-not $overwrite) -and (Test-Path -path $saveFileName)) 
    {
        write-progress -activity "$fileName already downloaded" `
                       -status "$pagepercent% of current page complete" `
                       -percentcomplete $pagepercent
        continue
    }
    write-progress -activity "Downloading $fileName" `
                   -status "$pagepercent% of current page complete" `
                   -percentcomplete $pagepercent

    [int]$trials = 0
    do {
        try {
            $trials +=1
            $webClient.DownloadFile($url, $saveFileName)
            break
        } catch [System.Net.WebException] {
            write-host "Problem downloading $url `tTrial $trials `
                       `n`tException: " $_.Exception.Message
        }
    }
    while ($trials -lt 3)
  }

  $link = $feed.feed.link | where { $_.rel.startsWith("next") } | select href
  if ($link -ne $null) {
    # if using a paged url with a $skiptoken like 
    # http:// ... /Packages?$skiptoken='EnyimMemcached-log4net','2.7'
    # remember that you need to escape the $ in powershell with `
    return $link.href
  }
  return $null
}  

# the NuGet feed uses a fwlink which redirects
# using this to follow the redirect
function GetPackageUrl {
 param ([string]$feedUrlBase) 
 $resp = [xml]$webClient.DownloadString($feedUrlBase)
 return $resp.service.GetAttribute("xml:base")
}

# --- do the actual work ---

# if dest dir doesn't exist, create it
if (!(Test-Path -path $destinationDirectory)) { 
    New-Item $destinationDirectory -type directory 
}

# set up feed URL
$serviceBase = $feedUrlBase
$feedUrl = $serviceBase + "Packages"
if($latest) {
    $feedUrl = $feedUrl + "?`$filter=IsLatestVersion eq true"
    if($top -ne $null) {
        $feedUrl = $feedUrl + "&`$orderby=DownloadCount desc&`$top=$top"
    }
}

while($feedUrl -ne $null) {
    $feedUrl = DownloadEntries $feedUrl
}

Next, I verified every version of every package was downloaded in .nupkg format, and they were. Next I uploaded to the new feed using powershell:

$destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "NuGetLocal"

Get-ChildItem $destinationDirectory -Recurse -Filter *.nupkg | 
Foreach-Object {
    nuget push -Source "YOUR NUGEFEED NAME HERE" -ApiKey az $_.FullName
}

I hope this helps someone who is working to migrate nuget feeds.

Upvotes: 4

Thomas Hallam
Thomas Hallam

Reputation: 21

If you just want to download, then save-package is more appropriate than install-package

This script saves all nuget packages from a nuget server (NuGet-Source) into the current directory

Find-Package -AllVersions -Source NuGet-Source | ForEach-Object {
  Save-Package -Name $_.Name -RequiredVersion $_.Version -Source NuGet-Source -Path '.'
}

Upvotes: 1

Fabito
Fabito

Reputation: 1224

I created a better PowerShell here that download all packages for all versions

Find-Package -AllVersions -Source NuGet-Source | ForEach-Object {
   Install-Package -Name $_.Name -MaximumVersion $_.Version -Destination 'C:\Temp\Nuget\' -Source NuGet-Source -SkipDependencies
}

Upvotes: 10

Andrew K
Andrew K

Reputation: 263

With PowerShell you can do this:

>Find-Package -Name='Package_Name'  -AllVersions -Source Local | Install-Package -Destination 'C:\SOME_PATH'

The command will find all the versions of packages with name like 'Package_Name' in the source Local (has to be specified in NuGet.config), and install them to 'C:\SOME_PATH' folder. To get all the packages from the source remove the -Name parameter.

Then you can get each .nupkg file from its own folder.

Upvotes: 4

Related Questions