Skip to content

Commit 06be900

Browse files
committed
Added missing PS script
Added install-dotnet.ps1 script.
1 parent 72d93ab commit 06be900

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

install-dotnet.ps1

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# ----------------------------------------------------------
2+
# Install script to check and download the correct .NET SDK
3+
# ----------------------------------------------------------
4+
5+
function Test-IsWindows
6+
{
7+
[environment]::OSVersion.Platform -ne "Unix"
8+
}
9+
10+
function Invoke-Cmd ($cmd)
11+
{
12+
Write-Host $cmd -ForegroundColor DarkCyan
13+
if (Test-IsWindows) { $cmd = "cmd.exe /C $cmd" }
14+
Invoke-Expression -Command $cmd
15+
if ($LastExitCode -ne 0) { Write-Error "An error occured when executing '$cmd'."; return }
16+
}
17+
18+
function dotnet-version { Invoke-Cmd "dotnet --version" }
19+
20+
function Get-DesiredSdk
21+
{
22+
Get-Content "global.json" | ConvertFrom-Json | % { $_.sdk.version.ToString() }
23+
}
24+
25+
function Get-NetCoreSdk ($version)
26+
{
27+
$os = if (Test-IsWindows) { "windows" } else { "linux" }
28+
29+
$response = Invoke-WebRequest `
30+
-Uri "https://www.microsoft.com/net/download/thank-you/dotnet-sdk-$version-$os-x64-binaries" `
31+
-Method Get `
32+
-MaximumRedirection 0 `
33+
34+
$downloadLink =
35+
$response.Links `
36+
| Where-Object { $_.onclick -eq "recordManualDownload()" } `
37+
| Select-Object -Expand href
38+
39+
$tempFile = [System.IO.Path]::GetTempFileName()
40+
$webClient = New-Object System.Net.WebClient
41+
$webClient.DownloadFile($downloadLink, $tempFile)
42+
return $tempFile
43+
}
44+
45+
function Install-NetCoreSdk ($sdkZipPath)
46+
{
47+
$env:DOTNET_INSTALL_DIR = "$pwd\.dotnetsdk"
48+
New-Item $env:DOTNET_INSTALL_DIR -ItemType Directory -Force
49+
50+
Add-Type -AssemblyName System.IO.Compression.FileSystem;
51+
[System.IO.Compression.ZipFile]::ExtractToDirectory($sdkZipPath, $env:DOTNET_INSTALL_DIR)
52+
$env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
53+
}
54+
55+
# ----------------------------------------------
56+
# Install .NET Core SDK
57+
# ----------------------------------------------
58+
59+
$ErrorActionPreference = "Stop"
60+
61+
# Rename the global.json before making the dotnet --version call
62+
# This will prevent AppVeyor to fail because it might not find
63+
# the desired SDK specified in the global.json
64+
$globalJson = Get-Item "global.json"
65+
Rename-Item -Path $globalJson.FullName -NewName "global.json.bak" -Force
66+
67+
# Get the current .NET Core SDK version
68+
$currentSdk = dotnet-version
69+
70+
# After we established the current installed .NET SDK we can put the global.json back
71+
Rename-Item -Path ($globalJson.FullName + ".bak") -NewName "global.json" -Force
72+
73+
$desiredSdk = Get-DesiredSdk
74+
75+
if ($desiredSdk -eq $currentSdk)
76+
{
77+
Write-Host "The current .NET SDK matches the project's desired .NET SDK: $desiredSDK" -ForegroundColor Green
78+
return
79+
}
80+
81+
Write-Host "The current .NET SDK ($currentSdk) doesn't match the project's desired .NET SDK ($desiredSdk)." -ForegroundColor Yellow
82+
Write-Host "Attempting to download and install the correct .NET SDK..."
83+
84+
$sdkZipPath = Get-NetCoreSdk $desiredSdk
85+
Install-NetCoreSdk $sdkZipPath
86+
87+
Write-Host ".NET SDK installation complete." -ForegroundColor Green
88+
dotnet-version

0 commit comments

Comments
 (0)