Skip to content

Instantly share code, notes, and snippets.

@bjh1977
Created October 3, 2018 11:27
Show Gist options
  • Save bjh1977/af3e8dad2f9b266a8a9a40a6844b2637 to your computer and use it in GitHub Desktop.
Save bjh1977/af3e8dad2f9b266a8a9a40a6844b2637 to your computer and use it in GitHub Desktop.
Gets nuget from nuget.org. Either the latest or a sprecific version
function Install-NugetFromNugetOrg {
<#
.Synopsis
Gets nuget from nuget.org
.Description
Gets nuget from nuget.org. Either the latest or a sprecific version
.Parameter RequiredVersion
The version as seen on www.nuget.org
.Parameter RootFolder
The root folder where nuget will be copied to
.Example
Install-NugetFromNugetOrg -RequiredVersion 'latest' -RootFolder 'c:\nuget'
.Example
Install-NugetFromNugetOrg -RequiredVersion '4.7.1' -RootFolder 'c:\nuget'
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
[string]
$RequiredVersion,
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
[string]
$RootFolder
)
if ($RequiredVersion -ne "latest" -and -not ($RequiredVersion.StartsWith('v'))) {
$RequiredVersion = "v$RequiredVersion"
}
$NugetPath = "$RootFolder\$RequiredVersion"
$NugetFullPath = Join-Path $NugetPath "nuget.exe"
New-Item -ItemType Directory -Path $NugetPath -Force | Out-Null
$WebRequestURL = "https://dist.nuget.org/win-x86-commandline/$RequiredVersion/nuget.exe"
if (!(Test-Path $NugetFullPath) -or $RequiredVersion -eq 'latest') {
Write-Host "Downloading nuget.exe from $WebRequestURL"
Invoke-WebRequest -Uri $WebRequestURL -OutFile $NugetFullPath -ErrorAction Stop
}
if ($RequiredVersion -eq "latest") {
$LatestVersion = ((Get-Item $NugetFullPath).VersionInfo.FileVersion) -split "\."
$LatestVersion3Part = "$($LatestVersion[0]).$($LatestVersion[1]).$($LatestVersion[2])"
$VersionSpecificPath = "$RootFolder\$LatestVersion3Part"
Write-Host "You asked for the latest version, which is $LatestVersion3Part."
$VersionSpecificFullPath = Join-Path $VersionSpecificPath "nuget.exe"
if (Test-Path $VersionSpecificFullPath) {
Write-Verbose "Found nuget.exe already at $VersionSpecificPath"
}
else {
Write-Verbose "Copying latest version to $VersionSpecificPath"
New-Item -ItemType Directory -Path $VersionSpecificPath -Force | Out-Null
Copy-Item $NugetFullPath $VersionSpecificFullPath
}
$NugetFullPath = $VersionSpecificFullPath
}
Write-Host "Nuget is at $NugetFullPath - enjoy!"
return $NugetFullPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment