Files
runner-images-sangeeth/images/win/scripts/Installers/Install-Go.ps1
2020-03-17 09:47:22 +03:00

76 lines
2.7 KiB
PowerShell

################################################################################
## File: Install-Go.ps1
## Desc: Install Go
################################################################################
Import-Module -Name ImageHelpers -Force
$refsJson = Invoke-RestMethod "https://api.github.com/repos/golang/go/git/refs/tags"
function Install-GoVersion
{
Param
(
[String]$goVersion,
[Switch]$addToDefaultPath
)
$latestVersionObject = $refsJson | Where-Object { $_.ref -Match "refs/tags/go$goVersion[./d]*" } | Select-Object -Last 1
$latestVersion = $latestVersionObject.ref.replace('refs/tags/go','')
# Download the Go zip archive.
Write-Host "Downloading Go $latestVersion..."
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -UseBasicParsing -Uri "https://dl.google.com/go/go$latestVersion.windows-amd64.zip" -OutFile go$latestVersion.windows-amd64.zip
# Extract the zip archive. It contains a single directory named "go".
Write-Host "Extracting Go $latestVersion..."
Expand-Archive -Path go$latestVersion.windows-amd64.zip -DestinationPath "C:\" -Force
# Delete unnecessary files to conserve space
Write-Host "Cleaning directories of Go $latestVersion..."
if (Test-Path "C:\go\doc")
{
Remove-Item -Recurse -Force "C:\go\doc"
}
if (Test-Path "C:\go\blog")
{
Remove-Item -Recurse -Force "C:\go\blog"
}
# Rename the extracted "go" directory to include the Go version number (to support side-by-side versions of Go).
$newDirName = "Go$latestVersion"
Rename-Item -path "C:\go" -newName $newDirName
# Delete the Go zip archive.
Write-Host "Deleting downloaded archive of Go $latestVersion..."
Remove-Item go$latestVersion.windows-amd64.zip
# Make this the default version of Go?
if ($addToDefaultPath)
{
Write-Host "Adding Go $latestVersion to the path..."
# Add the Go binaries to the path.
Add-MachinePathItem "C:\$newDirName\bin" | Out-Null
# Set the GOROOT environment variable.
setx GOROOT "C:\$newDirName" /M | Out-Null
}
# Done
Write-Host "Done installing Go $latestVersion."
return "C:\$newDirName"
}
# Install Go
$goVersionsToInstall = $env:GO_VERSIONS.split(", ", [System.StringSplitOptions]::RemoveEmptyEntries)
foreach($go in $goVersionsToInstall) {
Write-Host "Installing Go ${go}"
if($go -eq $env:GO_DEFAULT) {
$installDirectory = Install-GoVersion -goVersion $go -addToDefaultPath
} else {
$installDirectory = Install-GoVersion -goVersion $go
}
$envName = "GOROOT_{0}_{1}_X64" -f $go.split(".")
setx $envName "$installDirectory" /M
}