forked from dotnet/winforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-local.ps1
318 lines (237 loc) · 9.73 KB
/
build-local.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
[CmdletBinding(PositionalBinding=$false)]
Param(
[switch] $FullClean,
[Parameter(ValueFromRemainingArguments=$true)][String[]]$properties
)
$LASTEXITCODE=0;
function Check-SdkExists {
<#
.SYNOPSIS
Checks whether a system-wise NETCore SDK and a matching local NETCore SDK installations exist
.DESCRIPTION
Checks whether a system-wise NETCore SDK and a matching local NETCore SDK installations exist
.PARAMETER LocalSdkLocation
Specifies the expected location of local NETCore SDK (e.g. D:\winforms\.dotnet\sdk\3.0.100-preview5-011568).
.PARAMETER SystemSdkLocation
Specifies the expected location of local NETCore SDK (e.g. C:\Program Files\dotnet\sdk\3.0.100-preview5-011568).
.INPUTS
None.
.OUTPUTS
None.
#>
Param(
[Parameter(Mandatory=$true, Position=0)]
[string] $LocalSdkLocation,
[Parameter(Mandatory=$true, Position=1)]
[string] $SystemSdkLocation
)
# check that we have the right version of SDK installed correctly
# if not, we will need to run in an elevated mode
$isSdkPresent = $false;
if (![string]::IsNullOrWhiteSpace($LocalSdkLocation) -and ![string]::IsNullOrWhiteSpace($SystemSdkLocation)) {
$isLocalSdkPresent = [System.IO.Directory]::Exists($LocalSdkLocation);
$isSystemSdkPresent = [System.IO.Directory]::Exists($SystemSdkLocation);
if ($isLocalSdkPresent -eq $true -and $isSystemSdkPresent -eq $true) {
# the required SDK exists both locally and system-wide, all good
$isSdkPresent = $true;
}
}
return $isSdkPresent;
}
function Create-SymLink {
<#
.SYNOPSIS
Creates a symbolic link from a system-wise NETCore SDK to a local NETCore SDK installation
.DESCRIPTION
Creates a symbolic link from a system-wise NETCore SDK to a local NETCore SDK installation
.PARAMETER LocalSdkLocation
Specifies the expected location of local NETCore SDK (e.g. D:\winforms\.dotnet\sdk\3.0.100-preview5-011568).
.PARAMETER SystemSdkLocation
Specifies the expected location of local NETCore SDK (e.g. C:\Program Files\dotnet\sdk\3.0.100-preview5-011568).
.INPUTS
None.
.OUTPUTS
None.
#>
param (
[Parameter(Mandatory=$true, Position=0)]
[string] $LocalSdkLocation,
[Parameter(Mandatory=$true, Position=1)]
[string] $SystemSdkLocation
)
if ([System.IO.Directory]::Exists($SystemSdkLocation)) {
return;
}
Write-Host "Creating a symlink..."
try {
# Create a symbolic link, see MKLINK command
$dummy = New-Item -ItemType SymbolicLink -Path $SystemSdkLocation -Value "$LocalSdkLocation" -ErrorAction Stop;
Write-Host "√ Symlink created: $LocalSdkLocation --> $SystemSdkLocation" -ForegroundColor Green;
}
catch [System.IO.IOException] {
if ($_.Exception.Message -eq 'NewItemIOError') {
Write-Warning "! Symlink to '$SystemSdkLocation' already exists, ignore"
}
else {
throw
}
}
}
function Get-SystemDotnetPath {
return [string][System.IO.Path]::Combine($env:ProgramFiles, 'dotnet');
}
function Invoke-FullCleanup {
param (
[Parameter(Mandatory=$true, Position=0)]
[string] $SdkVersion,
[Parameter(Mandatory=$true, Position=1)]
[string] $NETCoreAppVersion
)
Stop-Process -Name 'dotnet' -Force -ErrorAction Ignore
Stop-Process -Name 'MSBuild' -Force -ErrorAction Ignore
$systenDotnetLocation = Get-SystemDotnetPath
$locations = @()
$locations += Get-ChildItem -Path $systenDotnetLocation -Filter sdk/$SdkVersion;
$locations += Get-ChildItem -Path $systenDotnetLocation -Filter shared/Microsoft.NETCore.App/$NETCoreAppVersion;
$locations | `
ForEach-Object {
$path = $_.FullName
if ($_.LinkType -eq 'SymbolicLink') {
(Get-Item $path).Delete()
}
else {
Remove-Item -Path $path -Force -Recurse
}
Write-Host "√ Path removed: $path" -ForegroundColor Green;
}
Write-Host "Cleaning local folder...";
git clean -xfd
Write-Host "√ Local folder cleaned" -ForegroundColor Green;
}
function Remove-Artifacts {
param (
[Parameter(Mandatory=$true, Position=0)]
[string] $Path
)
if (!(Test-Path $Path)) {
return 0;
}
$errorCount = 0;
Get-ChildItem -Path $Path -Include @( 'bin', 'obj' ) -Recurse | ForEach-Object {
$fullPath = $_.FullName;
Write-Host "Removing $fullPath" -NoNewline;
Remove-Item -Path $fullPath -Recurse -Force
$LastCode = $LASTEXITCODE;
if ($LastCode -ne 0) {
$errorCount++;
Write-Host " failed" -ForegroundColor Red;
}
else {
Write-Host " success" -ForegroundColor Green;
}
}
return $errorCount;
}
function Start-ElevatedModeIfRequired {
<#
.SYNOPSIS
Checks whether the script needs to be restarted in the elevated mode
.DESCRIPTION
The elevated mode is required in the following cases:
- FullClean is requested, or
- LocalSdkLocation or SystemSdkLocation does not exist, or
- LocalNETCoreAppLocation or SystemNETCoreAppLocation does not exist.
.PARAMETER IsFullCleanRequested
Specifies whether a full clean up is required.
.PARAMETER LocalSdkLocation
Specifies the expected location of local NETCore SDK (e.g. D:\winforms\.dotnet\sdk\3.0.100-preview5-011568).
.PARAMETER SystemSdkLocation
Specifies the expected location of local NETCore SDK (e.g. C:\Program Files\dotnet\sdk\3.0.100-preview5-011568).
.PARAMETER LocalNETCoreAppLocation
Specifies the expected location of local NETCore SDK (e.g. D:\winforms\.dotnet\shared\Microsoft.NETCore.App\3.0.0-preview7-27819-07).
.PARAMETER SystemNETCoreAppLocation
Specifies the expected location of local NETCore SDK (e.g. C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.0.0-preview7-27819-07).
.INPUTS
None.
.OUTPUTS
None.
#>
Param(
[Parameter(Mandatory=$true, Position=0)]
[bool] $IsFullCleanRequested,
[Parameter(Mandatory=$true, Position=1)]
[string] $LocalSdkLocation,
[Parameter(Mandatory=$true, Position=2)]
[string] $SystemSdkLocation,
[Parameter(Mandatory=$true, Position=2)]
[string] $LocalNETCoreAppLocation,
[Parameter(Mandatory=$true, Position=3)]
[string] $SystemNETCoreAppLocation
)
$isSdkPresent = Check-SdkExists -LocalSdkLocation $LocalSdkLocation -SystemSdkLocation $SystemSdkLocation;
$isNETCoreAppPresent = Check-SdkExists -LocalSdkLocation $LocalNETCoreAppLocation -SystemSdkLocation $SystemNETCoreAppLocation;
# if the SDK is found and not doing a full clean - we're done here
if ($isSdkPresent -eq $true -and `
$isNETCoreAppPresent -eq $true -and `
$IsFullCleanRequested -ne $true) {
return;
}
# To perform full cleanup we must run the script in an elevated mode
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$args = if ($IsFullCleanRequested -eq $true) { "-FullClean" } else { '' }
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" $args" -Verb RunAs;
exit 0
}
}
# break on errors
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
try {
$repoPath = $PSScriptRoot;
Push-Location $repoPath;
$localSdkPath = (Join-Path -Path $repoPath -ChildPath '.dotnet\sdk')
$localSharedPath = (Join-Path -Path $repoPath -ChildPath '.dotnet\shared')
# Detect the require version of SDK and see if we have it already
$globalJson = Get-Content -Raw -Path global.json | ConvertFrom-Json
$sdkVersion = [string]$globalJson.sdk.version
$localSdkLocation = [System.IO.Path]::Combine($localSdkPath, $sdkVersion);
$systemSdkLocation = [string][System.IO.Path]::Combine($(Get-SystemDotnetPath), 'sdk', $sdkVersion);
# Detect the require version of NETCoreApp and see if we have it already
[xml]$versionsProps = Get-Content -Raw -Path ./eng/Versions.props
$value = $versionsProps.Project.PropertyGroup | Where-Object { $_['MicrosoftNETCoreAppPackageVersion'] -ne $null }
$netCoreAppVersion = $value.InnerText
$localNETCoreAppLocation = [System.IO.Path]::Combine($localSharedPath, 'Microsoft.NETCore.App', $netCoreAppVersion);
$systemNETCoreAppLocation = [string][System.IO.Path]::Combine($(Get-SystemDotnetPath), 'shared\Microsoft.NETCore.App', $netCoreAppVersion);
Start-ElevatedModeIfRequired -IsFullCleanRequested $FullClean.ToBool() `
-LocalSdkLocation $localSdkLocation -SystemSdkLocation $systemSdkLocation `
-LocalNETCoreAppLocation $localNETCoreAppLocation -SystemNETCoreAppLocation $systemNETCoreAppLocation
if ($FullClean -eq $true) {
Invoke-FullCleanup -SdkVersion $sdkVersion -NETCoreAppVersion $netCoreAppVersion
}
# remove any rouge bin/obj folders that intermitently get created
$errorCount = Remove-Artifacts -Path ./src;
$errorCount += Remove-Artifacts -Path ./pkg;
if ($errorCount -gt 0) {
throw "Failed to remove bin/obj folders";
}
Write-Host "Building the solution...";
.\build.cmd
$LastCode = $LASTEXITCODE;
Write-Host "√ Solution built" -ForegroundColor Green;
Create-SymLink -LocalSdkLocation $localSdkLocation -SystemSdkLocation $systemSdkLocation
Create-SymLink -LocalSdkLocation $localNETCoreAppLocation -SystemSdkLocation $systemNETCoreAppLocation
if ($LastCode -ne 0) {
Write-Host 'Χ Build failed....' -ForegroundColor Red
pause
exit -1;
}
Start-Process .\Winforms.sln
}
catch {
Write-Host $_.Exception -ForegroundColor Red
pause
}
finally {
Pop-Location
}