Skip to content

Commit

Permalink
Add option to install SDKs machine-wide
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Jul 28, 2019
1 parent 5ee62bf commit a7959d7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 24 deletions.
7 changes: 5 additions & 2 deletions init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
.SYNOPSIS
Installs dependencies required to build and test the projects in this repository.
.DESCRIPTION
This does not require elevation, as the dependencies are installed in per-user locations.
This MAY not require elevation, as the SDK and runtimes are installed locally to this repo location,
unless `-InstallLocality machine` is specified.
.PARAMETER InstallLocality
A value indicating whether dependencies should be installed locally to the repo or at a per-user location.
Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache.
Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script.
Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build.
When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used.
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`.
Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it.
.PARAMETER NoPrerequisites
Skips the installation of prerequisite software (e.g. SDKs, tools).
.PARAMETER NoRestore
Expand All @@ -18,7 +21,7 @@ An optional access token for authenticating to Azure Artifacts authenticated fee
#>
[CmdletBinding(SupportsShouldProcess=$true)]
Param (
[ValidateSet('repo','user')]
[ValidateSet('repo','user','machine')]
[string]$InstallLocality='user',
[Parameter()]
[switch]$NoPrerequisites,
Expand Down
106 changes: 84 additions & 22 deletions tools/Install-DotNetSdk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,102 @@
Installs the .NET SDK specified in the global.json file at the root of this repository,
along with supporting .NET Core runtimes used for testing.
.DESCRIPTION
This does not require elevation, as the SDK and runtimes are installed locally to this repo location.
This MAY not require elevation, as the SDK and runtimes are installed locally to this repo location,
unless `-InstallLocality machine` is specified.
.PARAMETER InstallLocality
A value indicating whether dependencies should be installed locally to the repo or at a per-user location.
Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache.
Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script.
Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build.
When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used.
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`.
Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it.
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
Param (
[ValidateSet('repo','user')]
[ValidateSet('repo','user','machine')]
[string]$InstallLocality='user'
)

$DotNetInstallScriptRoot = "$PSScriptRoot/../obj"
if (!(Test-Path $DotNetInstallScriptRoot)) { mkdir $DotNetInstallScriptRoot | Out-Null }
$DotNetInstallScriptRoot = "$PSScriptRoot/../obj/tools"
if (!(Test-Path $DotNetInstallScriptRoot)) { New-Item -ItemType Directory -Path $DotNetInstallScriptRoot | Out-Null }
$DotNetInstallScriptRoot = Resolve-Path $DotNetInstallScriptRoot

# Look up actual required .NET Core SDK version from global.json
$sdkVersion = & "$PSScriptRoot/../azure-pipelines/variables/DotNetSdkVersion.ps1"

# Search for all .NET Core runtime versions referenced from MSBuild projects and arrange to install them.
$runtimeVersions = @()
Get-ChildItem "$PSScriptRoot\..\src\*.*proj" -Recurse |% {
$projXml = [xml](Get-Content -Path $_)
$targetFrameworks = $projXml.Project.PropertyGroup.TargetFramework
if (!$targetFrameworks) {
$targetFrameworks = $projXml.Project.PropertyGroup.TargetFrameworks
if ($targetFrameworks) {
$targetFrameworks = $targetFrameworks -Split ';'
}
}
$targetFrameworks |? { $_ -match 'netcoreapp(\d+\.\d+)' } |% {
$runtimeVersions += $Matches[1]
}
}

Function Get-FileFromWeb([Uri]$Uri, $OutDir) {
$OutFile = Join-Path $OutDir $Uri.Segments[-1]
if (!(Test-Path $OutFile)) {
Write-Verbose "Downloading $Uri..."
try {
(New-Object System.Net.WebClient).DownloadFile($Uri, $OutFile)
} finally {
# This try/finally causes the script to abort
}
}

$OutFile
}

Function Get-InstallerExe($Version, [switch]$Runtime) {
$sdkOrRuntime = 'Sdk'
if ($Runtime) { $sdkOrRuntime = 'Runtime' }

# Get the latest/actual version for the specified one
if (([Version]$Version).Build -eq -1) {
$versionInfo = -Split (Invoke-WebRequest -Uri "https://dotnetcli.blob.core.windows.net/dotnet/$sdkOrRuntime/$Version/latest.version")
$Version = $versionInfo[-1]
}

Get-FileFromWeb -Uri "https://dotnetcli.blob.core.windows.net/dotnet/$sdkOrRuntime/$Version/dotnet-$($sdkOrRuntime.ToLowerInvariant())-$Version-win-x64.exe" -OutDir "$DotNetInstallScriptRoot"
}

Function Install-DotNet($Version, [switch]$Runtime) {
if ($Runtime) { $sdkSubstring = '' } else { $sdkSubstring = 'SDK ' }
Write-Host "Downloading .NET Core $sdkSubstring$Version..."
$Installer = Get-InstallerExe -Version $Version -Runtime:$Runtime
Write-Host "Installing .NET Core $sdkSubstring$Version..."
cmd /c start /wait $Installer /install /quiet
if ($LASTEXITCODE -ne 0) {
throw "Failure to install .NET Core SDK"
}
}

if ($InstallLocality -eq 'machine') {
if ($IsMacOS -or $IsLinux) {
Write-Error "Installing the .NET Core SDK or runtime at a machine-wide location is only supported by this script on Windows."
exit 1
}

if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) {
Install-DotNet -Version $sdkVersion
}

$runtimeVersions |% {
if ($PSCmdlet.ShouldProcess(".NET Core runtime $_", "Install")) {
Install-DotNet -Version $_ -Runtime
}
}

return
}

$switches = @(
'-Architecture','x64'
Expand All @@ -28,7 +108,6 @@ $envVars = @{
'DOTNET_SKIP_FIRST_TIME_EXPERIENCE' = 'true';
}

$DotNetInstallScriptRoot = Resolve-Path $DotNetInstallScriptRoot
if ($InstallLocality -eq 'repo') {
$DotNetInstallDir = "$DotNetInstallScriptRoot/.dotnet"
} elseif ($env:AGENT_TOOLSDIRECTORY) {
Expand All @@ -44,7 +123,6 @@ if ($DotNetInstallDir) {
$envVars['DOTNET_MULTILEVEL_LOOKUP'] = '0'
$envVars['DOTNET_ROOT'] = $DotNetInstallDir
}
$sdkVersion = & "$PSScriptRoot/../azure-pipelines/variables/DotNetSdkVersion.ps1"

if ($IsMacOS -or $IsLinux) {
$DownloadUri = "https://dot.net/v1/dotnet-install.sh"
Expand All @@ -67,22 +145,6 @@ if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) {
Invoke-Expression -Command "$DotNetInstallScriptPath -Version $sdkVersion $switches -DryRun"
}

# Search for all .NET Core runtime versions referenced from MSBuild projects and arrange to install them.
$runtimeVersions = @()
Get-ChildItem "$PSScriptRoot\..\src\*.*proj" -Recurse |% {
$projXml = [xml](Get-Content -Path $_)
$targetFrameworks = $projXml.Project.PropertyGroup.TargetFramework
if (!$targetFrameworks) {
$targetFrameworks = $projXml.Project.PropertyGroup.TargetFrameworks
if ($targetFrameworks) {
$targetFrameworks = $targetFrameworks -Split ';'
}
}
$targetFrameworks |? { $_ -match 'netcoreapp(\d+\.\d+)' } |% {
$runtimeVersions += $Matches[1]
}
}

$switches += '-Runtime','dotnet'

$runtimeVersions | Get-Unique |% {
Expand Down

0 comments on commit a7959d7

Please sign in to comment.