|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Installs the .NET SDK specified in the global.json file at the root of this repository, |
| 4 | +along with supporting .NET Core runtimes used for testing. |
| 5 | +.DESCRIPTION |
| 6 | +This MAY not require elevation, as the SDK and runtimes are installed locally to this repo location, |
| 7 | +unless `-InstallLocality machine` is specified. |
| 8 | +.PARAMETER InstallLocality |
| 9 | +A value indicating whether dependencies should be installed locally to the repo or at a per-user location. |
| 10 | +Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache. |
| 11 | +Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script. |
| 12 | +Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build. |
| 13 | +When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used. |
| 14 | +Per-repo can lead to file locking issues when dotnet.exe is left running as a build server and can be mitigated by running `dotnet build-server shutdown`. |
| 15 | +Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it. |
| 16 | +#> |
| 17 | +[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')] |
| 18 | +Param ( |
| 19 | + [ValidateSet('repo','user','machine')] |
| 20 | + [string]$InstallLocality='user' |
| 21 | +) |
| 22 | + |
| 23 | +$DotNetInstallScriptRoot = "$PSScriptRoot/../obj/tools" |
| 24 | +if (!(Test-Path $DotNetInstallScriptRoot)) { New-Item -ItemType Directory -Path $DotNetInstallScriptRoot | Out-Null } |
| 25 | +$DotNetInstallScriptRoot = Resolve-Path $DotNetInstallScriptRoot |
| 26 | + |
| 27 | +# Look up actual required .NET Core SDK version from global.json |
| 28 | +$sdkVersion = & "$PSScriptRoot/DotNetSdkVersion.ps1" |
| 29 | + |
| 30 | +# Search for all .NET Core runtime versions referenced from MSBuild projects and arrange to install them. |
| 31 | +$runtimeVersions = @() |
| 32 | +Get-ChildItem "$PSScriptRoot\..\*.*proj" -Recurse |% { |
| 33 | + $projXml = [xml](Get-Content -Path $_) |
| 34 | + $targetFrameworks = $projXml.Project.PropertyGroup.TargetFramework |
| 35 | + if (!$targetFrameworks) { |
| 36 | + $targetFrameworks = $projXml.Project.PropertyGroup.TargetFrameworks |
| 37 | + if ($targetFrameworks) { |
| 38 | + $targetFrameworks = $targetFrameworks -Split ';' |
| 39 | + } |
| 40 | + } |
| 41 | + $targetFrameworks |? { $_ -match 'netcoreapp(\d+\.\d+)' } |% { |
| 42 | + $runtimeVersions += $Matches[1] |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +Function Get-FileFromWeb([Uri]$Uri, $OutDir) { |
| 47 | + $OutFile = Join-Path $OutDir $Uri.Segments[-1] |
| 48 | + if (!(Test-Path $OutFile)) { |
| 49 | + Write-Verbose "Downloading $Uri..." |
| 50 | + try { |
| 51 | + (New-Object System.Net.WebClient).DownloadFile($Uri, $OutFile) |
| 52 | + } finally { |
| 53 | + # This try/finally causes the script to abort |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + $OutFile |
| 58 | +} |
| 59 | + |
| 60 | +Function Get-InstallerExe($Version, [switch]$Runtime) { |
| 61 | + $sdkOrRuntime = 'Sdk' |
| 62 | + if ($Runtime) { $sdkOrRuntime = 'Runtime' } |
| 63 | + |
| 64 | + # Get the latest/actual version for the specified one |
| 65 | + if (([Version]$Version).Build -eq -1) { |
| 66 | + $versionInfo = -Split (Invoke-WebRequest -Uri "https://dotnetcli.blob.core.windows.net/dotnet/$sdkOrRuntime/$Version/latest.version" -UseBasicParsing) |
| 67 | + $Version = $versionInfo[-1] |
| 68 | + } |
| 69 | + |
| 70 | + Get-FileFromWeb -Uri "https://dotnetcli.blob.core.windows.net/dotnet/$sdkOrRuntime/$Version/dotnet-$($sdkOrRuntime.ToLowerInvariant())-$Version-win-x64.exe" -OutDir "$DotNetInstallScriptRoot" |
| 71 | +} |
| 72 | + |
| 73 | +Function Install-DotNet($Version, [switch]$Runtime) { |
| 74 | + if ($Runtime) { $sdkSubstring = '' } else { $sdkSubstring = 'SDK ' } |
| 75 | + Write-Host "Downloading .NET Core $sdkSubstring$Version..." |
| 76 | + $Installer = Get-InstallerExe -Version $Version -Runtime:$Runtime |
| 77 | + Write-Host "Installing .NET Core $sdkSubstring$Version..." |
| 78 | + cmd /c start /wait $Installer /install /quiet |
| 79 | + if ($LASTEXITCODE -ne 0) { |
| 80 | + throw "Failure to install .NET Core SDK" |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +if ($InstallLocality -eq 'machine') { |
| 85 | + if ($IsMacOS -or $IsLinux) { |
| 86 | + Write-Error "Installing the .NET Core SDK or runtime at a machine-wide location is only supported by this script on Windows." |
| 87 | + exit 1 |
| 88 | + } |
| 89 | + |
| 90 | + if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) { |
| 91 | + Install-DotNet -Version $sdkVersion |
| 92 | + } |
| 93 | + |
| 94 | + $runtimeVersions |% { |
| 95 | + if ($PSCmdlet.ShouldProcess(".NET Core runtime $_", "Install")) { |
| 96 | + Install-DotNet -Version $_ -Runtime |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return |
| 101 | +} |
| 102 | + |
| 103 | +$switches = @( |
| 104 | + '-Architecture','x64' |
| 105 | +) |
| 106 | +$envVars = @{ |
| 107 | + # For locally installed dotnet, skip first time experience which takes a long time |
| 108 | + 'DOTNET_SKIP_FIRST_TIME_EXPERIENCE' = 'true'; |
| 109 | +} |
| 110 | + |
| 111 | +if ($InstallLocality -eq 'repo') { |
| 112 | + $DotNetInstallDir = "$DotNetInstallScriptRoot/.dotnet" |
| 113 | +} elseif ($env:AGENT_TOOLSDIRECTORY) { |
| 114 | + $DotNetInstallDir = "$env:AGENT_TOOLSDIRECTORY/dotnet" |
| 115 | +} else { |
| 116 | + $DotNetInstallDir = Join-Path $HOME .dotnet |
| 117 | +} |
| 118 | + |
| 119 | +Write-Host "Installing .NET Core SDK and runtimes to $DotNetInstallDir" -ForegroundColor Blue |
| 120 | + |
| 121 | +if ($DotNetInstallDir) { |
| 122 | + $switches += '-InstallDir',$DotNetInstallDir |
| 123 | + $envVars['DOTNET_MULTILEVEL_LOOKUP'] = '0' |
| 124 | + $envVars['DOTNET_ROOT'] = $DotNetInstallDir |
| 125 | +} |
| 126 | + |
| 127 | +if ($IsMacOS -or $IsLinux) { |
| 128 | + $DownloadUri = "https://dot.net/v1/dotnet-install.sh" |
| 129 | + $DotNetInstallScriptPath = "$DotNetInstallScriptRoot/dotnet-install.sh" |
| 130 | +} else { |
| 131 | + $DownloadUri = "https://dot.net/v1/dotnet-install.ps1" |
| 132 | + $DotNetInstallScriptPath = "$DotNetInstallScriptRoot/dotnet-install.ps1" |
| 133 | +} |
| 134 | + |
| 135 | +if (-not (Test-Path $DotNetInstallScriptPath)) { |
| 136 | + Invoke-WebRequest -Uri $DownloadUri -OutFile $DotNetInstallScriptPath -UseBasicParsing |
| 137 | + if ($IsMacOS -or $IsLinux) { |
| 138 | + chmod +x $DotNetInstallScriptPath |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) { |
| 143 | + Invoke-Expression -Command "$DotNetInstallScriptPath -Version $sdkVersion $switches" |
| 144 | +} else { |
| 145 | + Invoke-Expression -Command "$DotNetInstallScriptPath -Version $sdkVersion $switches -DryRun" |
| 146 | +} |
| 147 | + |
| 148 | +$switches += '-Runtime','dotnet' |
| 149 | + |
| 150 | +$runtimeVersions | Get-Unique |% { |
| 151 | + if ($PSCmdlet.ShouldProcess(".NET Core runtime $_", "Install")) { |
| 152 | + Invoke-Expression -Command "$DotNetInstallScriptPath -Channel $_ $switches" |
| 153 | + } else { |
| 154 | + Invoke-Expression -Command "$DotNetInstallScriptPath -Channel $_ $switches -DryRun" |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +if ($PSCmdlet.ShouldProcess("Set DOTNET environment variables to discover these installed runtimes?")) { |
| 159 | + & "$PSScriptRoot/Set-EnvVars.ps1" -Variables $envVars -PrependPath $DotNetInstallDir | Out-Null |
| 160 | +} |
0 commit comments