Skip to content

Instantly share code, notes, and snippets.

@MartinBodocky
Last active September 29, 2015 09:54
Show Gist options
  • Save MartinBodocky/2f76877e0f45fcc3cc11 to your computer and use it in GitHub Desktop.
Save MartinBodocky/2f76877e0f45fcc3cc11 to your computer and use it in GitHub Desktop.
The script will be download most used Nuget packages, or just those which are in packages.config file.
# --- settings ---
$feedUrlBase = "http://go.microsoft.com/fwlink/?LinkID=206669"
# the rest will be params when converting to funclet
$forceLatest = $false
$latest = $true
$overwrite = $false
$top = 500 #use $top = $null to grab all
$destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "NuGetLocalNew"
$packageFile =(Resolve-Path ".\").Path + "\packages.config"
# --- 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 = $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
}
# download entry one by one
function DownloadPackage {
param ([string]$feedUrl)
$feed = [xml]$webClient.DownloadString($feedUrl)
if($feed -ne $null -and $feed.feed.entry -ne $null)
{
$entry = $feed.feed.entry
$url = $entry.content.src
$fileName = $entry.properties.id + "." + $entry.properties.version + ".nupkg"
$saveFileName = join-path $destinationDirectory $fileName
if ((-not $overwrite) -and (Test-Path -path $saveFileName))
{
#write-progress -activity "$fileName already downloaded" `
# -status "$pagepercent% of current page complete" `
# -percentcomplete $pagepercent
write-host $fileName + " already donwloaded"
continue
}
#write-progress -activity "Downloading $fileName" `
# -status "$pagepercent% of current page complete" `
# -percentcomplete $pagepercent
write-host $fileName + " is going to be donwloaded"
[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)
}
}
# 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"
# check if the package config is available
if(Test-Path -Path $packageFile)
{
# get only packages mentioned inside packages config
# $xdoc = new-object System.Xml.XmlDocument
[xml]$xmlContent = Get-Content $packageFile
# packages
$packages = $xmlContent.SelectNodes("//packages//package")
foreach ($package in $packages)
{
# in case there is no version filled we need to get latest on
if($package.version -eq $null -or $forceLatest)
{
$packageFeedUrl = $feedUrl + "?`$filter=Id eq '"+ $package.id + "' and IsLatestVersion eq true"
}
else
{
$packageFeedUrl = $feedUrl + "?`$filter=Id eq '"+ $package.id + "' and Version eq '" + $package.version + "'"
}
Write-Host "feed url: " + $packageFeedUrl
if($packageFeedUrl -ne $null)
{
DownloadPackage $packageFeedUrl
}
}
}
else
{
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment