Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfroThundr3007730/d92e07b5b082d33b889334d36cebdd45 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/d92e07b5b082d33b889334d36cebdd45 to your computer and use it in GitHub Desktop.
Update a local Nuget repo from a remote repo
Set-StrictMode -Version Latest
# Inspired by: https://weblogs.asp.net/jongalloway/downloading-a-local-nuget-repository-with-powershell
function Update-LocalNugetRepository {
<# .SYNOPSIS
Update a local Nuget repo from a remote repo #>
Param(
# Remote repository feed URL
[Uri]$FeedURLBase = 'https://aka.ms/sme-extension-feed',
# Local repository directory
[IO.DirectoryInfo]$Destination = [IO.Path]::Join([Environment]::GetFolderPath('MyDocuments'), 'NuGetLocal'),
# Overwrite local file if it exists
[switch]$Overwrite,
# Number of times to retry a download
[int]$Retries = 2
)
function _DownloadEntries {
Param([Uri]$feedURL)
[xml]$feed = [Net.WebClient]::new().DownloadString($feedUrl)
$entries = $feed.feed.entry.where{ $_.properties.IsLatestVersion.'#text' -eq 'true' }
[int]$progress = 0
foreach ($entry in $entries) {
$fileName = $entry.properties.id + '.' + $entry.properties.version + '.nupkg'
[IO.FileInfo]$filePath = [IO.Path]::Join($Destination, $fileName)
[int]$pagePercent = (++$progress) / $entries.Count * 100
if (!$Overwrite -and $filePath.Exists) {
Write-Progress -Activity ('{0} already downloaded' -f $fileName) `
-Status ('{0}% of current page complete' -f $pagePercent) `
-PercentComplete $pagePercent
continue
}
Write-Progress -Activity ('Downloading {0}' -f $fileName) `
-Status ('{0}% of current page complete' -f $pagePercent) `
-PercentComplete $pagePercent
[int]$tries = 0
while ($tries -le $Retries) {
try {
$tries++
[Net.WebClient]::new().DownloadFile($entry.content.src, $filePath)
break
} catch [System.Net.WebException] {
Write-Host ("Problem downloading URL: {0}`tTry: {1}`n`tException: {2}" -f
$entry.content.src, $tries, $_.Exception.Message)
}
}
}
$nextLink = ([object[]]$feed.feed.link).Where{ $_.rel.startsWith('next') }
if ($nextLink) { return [Uri]$nextLink.href }
}
$downloadURL = ([xml][Net.WebClient]::new().DownloadString($FeedURLBase)).service.GetAttribute('xml:base') + '/Packages'
if (!$Destination.Exists) { [void]$Destination.Create() }
while ($downloadURL) { $downloadURL = _DownloadEntries -feedURL $downloadURL }
}
@AfroThundr3007730
Copy link
Author

Updated version available in my HelperFunctions module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment