-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathInstall-DotnetSDK.ps1
133 lines (112 loc) · 4.62 KB
/
Install-DotnetSDK.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
################################################################################
## File: Install-DotnetSDK.ps1
## Desc: Install all released versions of the dotnet sdk and populate package
## cache. Should run after VS and Node
################################################################################
# ensure temp
New-Item -Path C:\Temp -Force -ItemType Directory
# Set environment variables
Set-SystemVariable -SystemVariable DOTNET_MULTILEVEL_LOOKUP -Value "0"
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor "Tls12"
function Get-SDKVersionsToInstall (
$DotnetVersion
) {
$releaseJson = "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/${DotnetVersion}/releases.json"
$releasesJsonPath = Start-DownloadWithRetry -Url $releaseJson -Name "releases-${DotnetVersion}.json"
$currentReleases = Get-Content -Path $releasesJsonPath | ConvertFrom-Json
# filtering out the preview/rc releases
$currentReleases = $currentReleases.'releases' | Where-Object { !$_.'release-version'.Contains('-') }
$sdks = @()
ForEach ($release in $currentReleases)
{
$sdks += $release.'sdk'
$sdks += $release.'sdks'
}
return $sdks.version | Sort-Object { [Version] $_ } -Unique `
| Group-Object { $_.Substring(0, $_.LastIndexOf('.') + 2) } `
| Foreach-Object { $_.Group[-1] }
}
function Invoke-Warmup (
$SdkVersion
) {
# warm up dotnet for first time experience
$projectTypes = @('console', 'mstest', 'web', 'mvc', 'webapi')
$projectTypes | ForEach-Object {
$template = $_
$projectPath = Join-Path -Path C:\temp -ChildPath $template
New-Item -Path $projectPath -Force -ItemType Directory
Push-Location -Path $projectPath
& $env:ProgramFiles\dotnet\dotnet.exe new globaljson --sdk-version "$sdkVersion"
& $env:ProgramFiles\dotnet\dotnet.exe new $template
Pop-Location
Remove-Item $projectPath -Force -Recurse
}
}
function Fix-ImportPublishProfile (
$SdkVersion
) {
if ((Test-IsWin16) -or (Test-IsWin19)) {
# Fix for issue https://github.com/dotnet/sdk/issues/1276. This will be fixed in 3.1.
$sdkTargetsName = "Microsoft.NET.Sdk.ImportPublishProfile.targets"
$sdkTargetsUrl = "https://raw.githubusercontent.com/dotnet/sdk/82bc30c99f1325dfaa7ad450be96857a4fca2845/src/Tasks/Microsoft.NET.Build.Tasks/targets/${sdkTargetsName}"
$sdkTargetsPath = "C:\Program Files\dotnet\sdk\$sdkVersion\Sdks\Microsoft.NET.Sdk\targets"
Start-DownloadWithRetry -Url $sdkTargetsUrl -DownloadPath $sdkTargetsPath -Name $sdkTargetsName
}
}
function InstallSDKVersion (
$SdkVersion,
$Warmup
)
{
if (!(Test-Path -Path "C:\Program Files\dotnet\sdk\$sdkVersion"))
{
Write-Host "Installing dotnet $sdkVersion"
.\dotnet-install.ps1 -Architecture x64 -Version $sdkVersion -InstallDir $(Join-Path -Path $env:ProgramFiles -ChildPath 'dotnet')
}
else
{
Write-Host "Sdk version $sdkVersion already installed"
}
Fix-ImportPublishProfile -SdkVersion $SdkVersion
if ($Warmup) {
Invoke-Warmup -SdkVersion $SdkVersion
}
}
function InstallAllValidSdks()
{
# Consider all channels except preview/eol channels.
# Sort the channels in ascending order
$dotnetToolset = (Get-ToolsetContent).dotnet
$dotnetVersions = $dotnetToolset.versions
$warmup = $dotnetToolset.warmup
# Download installation script.
$installationName = "dotnet-install.ps1"
$installationUrl = "https://dot.net/v1/${installationName}"
Start-DownloadWithRetry -Url $installationUrl -Name $installationName -DownloadPath ".\"
ForEach ($dotnetVersion in $dotnetVersions)
{
$sdkVersionsToInstall = Get-SDKVersionsToInstall -DotnetVersion $dotnetVersion
ForEach ($sdkVersion in $sdkVersionsToInstall)
{
InstallSDKVersion -SdkVersion $sdkVersion -Warmup $warmup
}
}
}
function RunPostInstallationSteps()
{
# Add dotnet to PATH
Add-MachinePathItem "C:\Program Files\dotnet"
# Remove NuGet Folder
$nugetPath = "$env:APPDATA\NuGet"
if (Test-Path $nugetPath) {
Remove-Item -Path $nugetPath -Force -Recurse
}
# Generate and copy new NuGet.Config config
dotnet nuget list source | Out-Null
Copy-Item -Path $nugetPath -Destination C:\Users\Default\AppData\Roaming -Force -Recurse
# Add %USERPROFILE%\.dotnet\tools to USER PATH
Add-DefaultPathItem "%USERPROFILE%\.dotnet\tools"
}
InstallAllValidSdks
RunPostInstallationSteps
Invoke-PesterTests -TestFile "DotnetSDK"