2
votes

We're looking at running a private NuGet server. I know products like ProGet have functions to get packages from NuGet and put them into a private feed. We're more interested in a bare bones approach if possible. I built and deployed a NuGet server using NuGet.Server and Visual Studio. I can put nupkg files into my packages folder and install from my private server into a Visual Studio project. I've had to manually download the NuGet packages and then copy the nupkg files. Is this the only way? It seems some NuGet packages aren't fully downloadable as stand-alone packages. I was able to get one by creating a dummy app, installing the package from nuget.org and then copying the nupkg file but that seems hokey.

Anyone running a private NuGet server that has a cleaner way to "copy" packages from nuget.org into their private server?

TIA

1

1 Answers

0
votes

Running into the same issue in the past, i used this Blog Post by Jon Galloway

It contains a script that works "out of the box" (double checked it now on Win7)

Pay attention to the variables as the default script repository is Microsoft's and limit itself to 500 nuget packages.

Also, when downloading the packages ive noticed they are saved in ".Version.nupkg" and not containing the full package name (Happens in Nuget Offical Repository https://www.nuget.org/api/v2/)

EDIT:

Following your need to choose spesific nuget packages, ive added a few lines for filtering the packages

      param(
  [parameter(Mandatory=$TRUE,Position=0)]
  $PackageFilterBy
  )
# --- settings ---
$feedUrlBase = "http://go.microsoft.com/fwlink/?LinkID=206669"
# the rest will be params when converting to funclet
$latest = $true
$overwrite = $false
$top = 500 #use $top = $null to grab all
$destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "NuGetLocal"

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

# --- functions ---

# download entries on a page, recursively called for page continuations
function DownloadEntries {
 param ([string]$feedUrl) 
 $feed = [xml]$webClient.DownloadString($feedUrl)
 $entries =@()

$filter = $PackageFilterBy.Split(',')

 foreach($item in $filter ){
 $feed.feed.entry | %{if($_.id -match "$item"){$entries += $_}}
 }
 $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 = GetPackageUrl($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
}

Using the script will be something like this: .\Script.ps1 jquery,Microsoft