Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Last active September 25, 2019 14:28
Show Gist options
  • Save JeremySkinner/5bbbfacceace6133a546067256fb88f2 to your computer and use it in GitHub Desktop.
Save JeremySkinner/5bbbfacceace6133a546067256fb88f2 to your computer and use it in GitHub Desktop.
function install {
$build_dir = Join-Path $PSScriptRoot ".build"
$json = ConvertFrom-Json (Get-Content "$path/global.json" -Raw)
$required_version = $json.sdk.version
# If there's a version mismatch with what's defined in global.json then a
# call to dotnet --version will generate an error.
try { dotnet --version 2>&1>$null } catch { $install_sdk = $true }
# Different PS versions may throw an exception vs set LASTEXITCODE
if ($global:LASTEXITCODE) {
$install_sdk = $true;
$global:LASTEXITCODE = 0;
}
if ($install_sdk) {
$installer = $null;
if ($IsWindows) {
$installer = "$build_dir/dotnet-installer.ps1"
(New-Object System.Net.WebClient).DownloadFile("https://dot.net/v1/dotnet-install.ps1", $installer);
}
else {
$installer = "$build_dir/dotnet-installer"
write-host Downloading installer to $installer
# use curl on linux as WebClient.DownloadFile always throws an exception
curl https://dot.net/v1/dotnet-install.sh --output $installer
chmod +x $installer
}
$dotnet_path = "$path/.dotnetsdk"
# If running in azure pipelines, use that as the dotnet install path.
if ($env:AGENT_TOOLSDIRECTORY) {
$dotnet_path = Join-Path $env:AGENT_TOOLSDIRECTORY dotnet
}
Write-Host Installing $json.sdk.version to $dotnet_path
. $installer -i $dotnet_path -v $json.sdk.version
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment