-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
restore-toolset.ps1
164 lines (137 loc) · 5.89 KB
/
restore-toolset.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function InitializeCustomSDKToolset {
if ($env:TestFullMSBuild -eq "true") {
$env:DOTNET_SDK_TEST_MSBUILD_PATH = InitializeVisualStudioMSBuild -install:$true -vsRequirements:$GlobalJson.tools.'vs-opt'
Write-Host "INFO: Tests will run against full MSBuild in $env:DOTNET_SDK_TEST_MSBUILD_PATH"
}
if (-not $restore) {
return
}
# The following frameworks and tools are used only for testing.
# Do not attempt to install them in source build.
if ($productBuild -or $properties -like "*DotNetBuildRepo=true*") {
return
}
$cli = InitializeDotnetCli -install:$true
InstallDotNetSharedFramework "6.0.0"
InstallDotNetSharedFramework "7.0.0"
InstallDotNetSharedFramework "8.0.0"
InstallDotNetSharedFramework "9.0.0"
CreateBuildEnvScripts
CreateVSShortcut
InstallNuget
}
function InstallNuGet {
$NugetInstallDir = Join-Path $ArtifactsDir ".nuget"
$NugetExe = Join-Path $NugetInstallDir "nuget.exe"
if (!(Test-Path -Path $NugetExe)) {
Create-Directory $NugetInstallDir
Invoke-WebRequest "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -UseBasicParsing -OutFile $NugetExe
}
}
function CreateBuildEnvScripts()
{
Create-Directory $ArtifactsDir
$scriptPath = Join-Path $ArtifactsDir "sdk-build-env.bat"
$scriptContents = @"
@echo off
title SDK Build ($RepoRoot)
set DOTNET_MULTILEVEL_LOOKUP=0
set DOTNET_ROOT=$env:DOTNET_INSTALL_DIR
set DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR=$env:DOTNET_INSTALL_DIR
set PATH=$env:DOTNET_INSTALL_DIR;%PATH%
set NUGET_PACKAGES=$env:NUGET_PACKAGES
DOSKEY killdotnet=taskkill /F /IM dotnet.exe /T ^& taskkill /F /IM VSTest.Console.exe /T ^& taskkill /F /IM msbuild.exe /T
"@
Out-File -FilePath $scriptPath -InputObject $scriptContents -Encoding ASCII
Create-Directory $ArtifactsDir
$scriptPath = Join-Path $ArtifactsDir "sdk-build-env.ps1"
$scriptContents = @"
`$host.ui.RawUI.WindowTitle = "SDK Build ($RepoRoot)"
`$env:DOTNET_MULTILEVEL_LOOKUP=0
`$env:DOTNET_ROOT="$env:DOTNET_INSTALL_DIR"
`$env:DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR="$env:DOTNET_INSTALL_DIR"
`$env:PATH="$env:DOTNET_INSTALL_DIR;" + `$env:PATH
`$env:NUGET_PACKAGES="$env:NUGET_PACKAGES"
function killdotnet {
taskkill /F /IM dotnet.exe /T
taskkill /F /IM VSTest.Console.exe /T
taskkill /F /IM msbuild.exe /T
}
"@
Out-File -FilePath $scriptPath -InputObject $scriptContents -Encoding ASCII
}
function CreateVSShortcut()
{
# https://github.com/microsoft/vswhere/wiki/Installing
$installerPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer"
if(-Not (Test-Path -Path $installerPath))
{
return
}
$versionFilePath = Join-Path $RepoRoot 'src\Layout\redist\minimumMSBuildVersion'
# Gets the first digit (ex. 17) and appends '.0' to it.
$vsMajorVersion = "$(((Get-Content $versionFilePath).Split('.'))[0]).0"
$devenvPath = (& "$installerPath\vswhere.exe" -all -prerelease -latest -version $vsMajorVersion -find Common7\IDE\devenv.exe) | Select-Object -First 1
if(-Not $devenvPath)
{
return
}
$scriptPath = Join-Path $ArtifactsDir 'sdk-build-env.ps1'
$slnPath = Join-Path $RepoRoot 'sdk.sln'
$commandToLaunch = "& '$scriptPath'; & '$devenvPath' '$slnPath'"
$powershellPath = '%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe'
$shortcutPath = Join-Path $ArtifactsDir 'VS with sdk.sln.lnk'
# https://stackoverflow.com/a/9701907/294804
# https://learn.microsoft.com/en-us/troubleshoot/windows-client/admin-development/create-desktop-shortcut-with-wsh
$wsShell = New-Object -ComObject WScript.Shell
$shortcut = $wsShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $powershellPath
$shortcut.Arguments = "-WindowStyle Hidden -Command ""$commandToLaunch"""
$shortcut.IconLocation = $devenvPath
$shortcut.WindowStyle = 7 # Minimized
$shortcut.Save()
}
function InstallDotNetSharedFramework([string]$version) {
$dotnetRoot = $env:DOTNET_INSTALL_DIR
$fxDir = Join-Path $dotnetRoot "shared\Microsoft.NETCore.App\$version"
if (!(Test-Path $fxDir)) {
$installScript = GetDotNetInstallScript $dotnetRoot
& $installScript -Version $version -InstallDir $dotnetRoot -Runtime "dotnet" -SkipNonVersionedFiles
if($lastExitCode -ne 0) {
throw "Failed to install shared Framework $version to '$dotnetRoot' (exit code '$lastExitCode')."
}
}
}
# Let's clear out the stage-zero folders that map to the current runtime to keep stage 2 clean
function CleanOutStage0ToolsetsAndRuntimes {
$GlobalJson = Get-Content -Raw -Path (Join-Path $RepoRoot 'global.json') | ConvertFrom-Json
$dotnetSdkVersion = $GlobalJson.tools.dotnet
$dotnetRoot = $env:DOTNET_INSTALL_DIR
$versionPath = Join-Path $dotnetRoot '.version'
$aspnetRuntimePath = [IO.Path]::Combine( $dotnetRoot, 'shared' ,'Microsoft.AspNetCore.App')
$coreRuntimePath = [IO.Path]::Combine( $dotnetRoot, 'shared' ,'Microsoft.NETCore.App')
$wdRuntimePath = [IO.Path]::Combine( $dotnetRoot, 'shared', 'Microsoft.WindowsDesktop.App')
$sdkPath = Join-Path $dotnetRoot 'sdk'
$majorVersion = $dotnetSdkVersion.Substring(0,1)
if (Test-Path($versionPath)) {
$lastInstalledSDK = Get-Content -Raw -Path ($versionPath)
if ($lastInstalledSDK -ne $dotnetSdkVersion)
{
$dotnetSdkVersion | Out-File -FilePath $versionPath -NoNewline
Remove-Item (Join-Path $aspnetRuntimePath "$majorVersion.*") -Recurse
Remove-Item (Join-Path $coreRuntimePath "$majorVersion.*") -Recurse
Remove-Item (Join-Path $wdRuntimePath "$majorVersion.*") -Recurse
Remove-Item (Join-Path $sdkPath "*") -Recurse
Remove-Item (Join-Path $dotnetRoot "packs") -Recurse
Remove-Item (Join-Path $dotnetRoot "sdk-manifests") -Recurse
Remove-Item (Join-Path $dotnetRoot "templates") -Recurse
throw "Installed a new SDK, deleting existing shared frameworks and sdk folders. Please rerun build"
}
}
else
{
$dotnetSdkVersion | Out-File -FilePath $versionPath -NoNewline
}
}
InitializeCustomSDKToolset
CleanOutStage0ToolsetsAndRuntimes