forked from OData/WebApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
442 lines (373 loc) · 12.7 KB
/
build.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# Default to Debug
$Configuration = 'Debug'
# Color
$Success = 'Green'
$Warning = 'Yellow'
$Err = 'Red'
Function Error ($msg)
{
Write-Host "[Error:]" $msg -ForegroundColor $Err
}
Function Warning ($msg)
{
Write-Host "[Warning:]" $msg -ForegroundColor $Warning
}
Function Success ($msg)
{
Write-Host "[Success:]" $msg -ForegroundColor $Success
}
Function Info ($msg)
{
Write-Host "[Info:]" $msg
}
if ($args.Count -eq 0)
{
$TestType = 'Nightly'
$Configuration = 'Release'
}
elseif ($args[0] -match 'quick' -or ($args[0] -match '-q'))
{
$TestType = "Quick"
}
elseif ($args[0] -match 'DisableSkipStrongName')
{
$TestType = "DisableSkipStrongName"
}
elseif ($args[0] -match 'EnableSkipStrongName')
{
$TestType = "EnableSkipStrongName"
}
else
{
Error("Unknown input ""$args"". It can be empty or ""quick|DisableSkipStrongName|EnableSkipStrongName"".")
exit
}
$Build = 'build'
if ($args -contains 'rebuild')
{
$Build = 'rebuild'
}
$PROGRAMFILESX86 = [Environment]::GetFolderPath("ProgramFilesX86")
$env:ENLISTMENT_ROOT = Split-Path -Parent $MyInvocation.MyCommand.Definition
$ENLISTMENT_ROOT = Split-Path -Parent $MyInvocation.MyCommand.Definition
$LOGDIR = $ENLISTMENT_ROOT + "\bin"
# Default to use Visual Studio 2015
$VS14MSBUILD=$PROGRAMFILESX86 + "\MSBuild\14.0\Bin\MSBuild.exe"
$VSTEST = $PROGRAMFILESX86 + "\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
$SN = $PROGRAMFILESX86 + "\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\sn.exe"
$SNx64 = $PROGRAMFILESX86 + "\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\sn.exe"
# Use Visual Studio 2017 compiler for .NET Core and .NET Standard. Because VS2017 has different paths for different
# versions, we have to check for each version. Meanwhile, the dotnet CLI is required to run the .NET Core unit tests in this script.
$VS15VERSIONS = "Enterprise", "Professional", "Community"
$VS15MSBUILD = $null
ForEach ($version in $VS15VERSIONS)
{
$tempMSBuildPath = ($PROGRAMFILESX86 + "\Microsoft Visual Studio\2017\{0}\MSBuild\15.0\Bin\MSBuild.exe") -f $version
if([System.IO.File]::Exists($tempMSBuildPath))
{
$VS15MSBUILD = $tempMSBuildPath
break
}
}
$DOTNETDIR = "C:\Program Files\dotnet\"
$DOTNETEXE = $null
if ([System.IO.File]::Exists($DOTNETDIR + "dotnet.exe"))
{
$DOTNETEXE = $DOTNETDIR + "dotnet.exe"
}
else
{
Error("The dotnet CLI must be installed to run any .NET Core tests.")
exit
}
# Other variables
$BUILDLOG = $LOGDIR + "\msbuild.log"
$TESTLOG = $LOGDIR + "\mstest.log"
$NUGETEXE = $ENLISTMENT_ROOT + "\sln\.nuget\NuGet.exe"
$NUGETPACK = $ENLISTMENT_ROOT + "\sln\packages"
$XUNITADAPTER = "/TestAdapterPath:" + $NUGETPACK + "\xunit.runner.visualstudio.2.3.1\build\_common"
$ClassicUnitTestSLN = "WebApiOData.AspNet.sln"
$ClassicE2ETestSLN = "WebApiOData.E2E.AspNet.sln"
$NetCoreUnitTesSLN = "WebApiOData.AspNetCore.sln"
$NetCoreE2ETestSLN = "WebApiOData.E2E.AspNetCore.sln"
$NugetRestoreSolutions = $ClassicUnitTestSLN, $ClassicE2ETestSLN, $NetCoreUnitTesSLN, $NetCoreE2ETestSLN
$NetCoreProductPROJ = "\src\Microsoft.AspNetCore.OData\Microsoft.AspNetCore.OData.csproj "
$NetCoreUnitTestPROJ = "\test\UnitTest\Microsoft.AspNetCore.OData.Test\Microsoft.AspNetCore.OData.Test.csproj"
$NetCoreE2ETestPROJ = "\test\E2ETest\Microsoft.Test.E2E.AspNet.OData\Build.AspNetCore\Microsoft.Test.E2E.AspNetCore.OData.csproj"
$NugetRestoreDotNetProjects = $NetCoreProductPROJ, $NetCoreUnitTestPROJ, $NetCoreE2ETestPROJ
$ClassicProductDIR = $ENLISTMENT_ROOT + "\bin\$Configuration"
$ClassicProductDlls = "Microsoft.AspNet.OData.dll"
$ClassicUnitTestDIR = $ENLISTMENT_ROOT + "\bin\$Configuration\UnitTest\AspNet"
$ClassicUnitTestDlls = "Microsoft.AspNet.OData.Test.dll"
$ClassicE2ETestDIR = $ENLISTMENT_ROOT + "\bin\$Configuration\E2ETest\AspNet"
$ClassicE2ETestDlls = "Microsoft.Test.E2E.AspNet.OData.dll"
$NetCoreProductDIR = $ENLISTMENT_ROOT + "\bin\$Configuration\netstandard2.0"
$NetCoreProductDlls = "Microsoft.AspNetCore.OData.dll"
$NetCoreUnitTestDIR = $ENLISTMENT_ROOT + "\bin\$Configuration\UnitTest\AspNetCore\netcoreapp2.0"
$NetCoreUnitTestDlls = "Microsoft.AspNetCore.OData.Test.dll"
$NetCoreE2ETestDIR = $ENLISTMENT_ROOT + "\bin\$Configuration\E2ETest\AspNetCore"
$NetCoreE2ETestDlls = "Microsoft.Test.E2E.AspNetCore.OData.dll"
# .NET Core tests are different and require the dotnet tool. The tool references the .csproj (VS2017) files instead of dlls
# However, the AspNetCore E2E tests still use EF6 and are not compiled to a .NetCore binary.
$ClassicUnitTestFramework = "netfx"
$ClassicUnitTestSuite = @()
ForEach($dll in $ClassicUnitTestDlls)
{
$ClassicUnitTestSuite += $ClassicUnitTestDIR + "\" + $dll
}
$NetCoreUnitTestFramework = "dotnet"
$NetCoreUnitTestSuite = @()
$NetCoreUnitTestProjs = $ENLISTMENT_ROOT + "\test\UnitTest\Microsoft.AspNetCore.OData.Test\Microsoft.AspNetCore.OData.Test.csproj"
ForEach($proj in $NetCoreUnitTestProjs)
{
$NetCoreUnitTestSuite += $proj
}
$ClassicE2ETestFramework = "netfx"
$ClassicE2ETestSuite = @()
ForEach($dll in $ClassicE2ETestDlls)
{
$ClassicE2ETestSuite += $ClassicE2ETestDIR + "\" + $dll
}
$NetCoreE2ETestFramnework = "netfx"
$NetCoreE2ETestSuite = @()
ForEach($dll in $NetCoreE2ETestDlls)
{
$NetCoreE2ETestSuite += $NetCoreE2ETestDIR + "\" + $dll
}
Function GetDlls
{
$dlls = @()
$dlls += $ClassicProductDIR + "\" + $ClassicProductDlls
$dlls += $NetCoreProductDIR + "\" + $NetCoreProductDlls
$dlls += $ClassicUnitTestDIR + "\" + $ClassicUnitTestDlls
$dlls += $NetCoreUnitTestDIR + "\" + $NetCoreUnitTestDlls
$dlls += $ClassicE2ETestDIR + "\" + $ClassicE2ETestDlls
$dlls += $NetCoreE2ETestDIR + "\" + $NetCoreE2ETestDlls
return $dlls
}
Function SkipStrongName
{
$SnLog = $LOGDIR + "\SkipStrongName.log"
Out-File $SnLog
Info('Skip strong name validations for assemblies...')
$dlls = GetDlls
ForEach ($dll in $dlls)
{
& $SN /Vr $dll | Out-File $SnLog -Append
}
ForEach ($dll in $dlls)
{
& $SNx64 /Vr $dll | Out-File $SnLog -Append
}
Success("SkipStrongName Done")
}
Function DisableSkipStrongName
{
$SnLog = $LOGDIR + "\DisableSkipStrongName.log"
Out-File $SnLog
Info("Disable skip strong name validations for assemblies...")
$dlls = GetDlls
ForEach ($dll in $dlls)
{
& $SN /Vu $dll | Out-File $SnLog -Append
}
ForEach ($dll in $dlls)
{
& $SNx64 /Vu $dll | Out-File $SnLog -Append
}
Success("DisableSkipStrongName Done")
}
Function NugetRestoreSolution
{
Info("Pull NuGet Packages...")
foreach($solution in $NugetRestoreSolutions)
{
& $NUGETEXE "restore" ($ENLISTMENT_ROOT + "\sln\" + $solution)
}
foreach($project in $NugetRestoreDotNetProjects)
{
& $DOTNETEXE "restore" ($ENLISTMENT_ROOT + $project)
}
Success("Pull Nuget Packages Success")
}
Function TestSummary
{
Write-Host 'Collecting test results'
$file = Get-Content -Path $TESTLOG
$pass = 0
$skipped = 0
$fail = 0
$failedtest1 = New-Object -TypeName System.Collections.ArrayList
$failedtest2 = New-Object -TypeName System.Collections.ArrayList
$part = 1
foreach ($line in $file)
{
# Consolidate logic for retrieving number of passed and skipped tests. Failed tests is separate due to the way
# VSTest and DotNet (for .NET Core tests) report results differently.
if ($line -match "^Total tests: .*")
{
# The line is in this format:
# Total tests: 5735. Passed: 5735. Failed: 0. Skipped: 0.
# We want to extract the total passed and total skipped.
# Extract total passed by taking the substring between "Passed: " and "."
# The regex first extracts the string after the hardcoded "Passed: " (i.e. "#. Failed: #. Skipped: #.")
# Then we tokenize by "." and retrieve the first token which is the number for passed.
$pattern = "Passed: (.*)"
$extractedNumber = [regex]::match($line, $pattern).Groups[1].Value.Split(".")[0]
$pass += $extractedNumber
# Extract total skipped by taking the substring between "Skipped: " and "."
# The regex first extracts the string after the hardcoded "Skipped: " (i.e. "#.")
# Then we tokenize by "." and retrieve the first token which is the number for skipped.
$pattern = "Skipped: (.*)"
$extractedNumber = [regex]::match($line, $pattern).Groups[1].Value.Split(".")[0]
$skipped += $extractedNumber
}
elseif ($line -match "^Failed\s+(.*)")
{
$fail = $fail + 1
if ($part -eq 1)
{
[void]$failedtest1.Add($Matches[1])
}
else
{
[void]$failedtest2.Add($Matches[1])
}
}
}
Write-Host "Test summary:" -ForegroundColor $Success
Write-Host "Passed :`t$pass" -ForegroundColor $Success
if ($skipped -ne 0)
{
Write-Host "Skipped:`t$skipped" -ForegroundColor $Warning
}
$color = $Success
if ($fail -ne 0)
{
$color = $Err
}
Write-Host "Failed :`t$fail" -ForegroundColor $color
Write-Host "---------------" -ForegroundColor $Success
Write-Host "Total :`t$($pass + $fail)" -ForegroundColor $Success
if ($fail -eq 0)
{
Write-Host "Congratulation! All of the tests passed!" -ForegroundColor $Success
}
}
# Incremental build and rebuild
Function RunBuild ($sln, $vsToolVersion)
{
Info("Building $sln ...")
$slnpath = $ENLISTMENT_ROOT + "\sln\$sln"
$Conf = "/p:Configuration=" + "$Configuration"
# Default to VS2015
$MSBUILD = $VS14MSBUILD
if($vsToolVersion -eq '15.0')
{
$MSBUILD=$VS15MSBUILD
}
& $MSBUILD $slnpath /t:$Build /m /nr:false /fl "/p:Platform=Any CPU" $Conf /p:Desktop=true `
/flp:LogFile=$LOGDIR/msbuild.log /flp:Verbosity=Normal 1>$null 2>$null
if($LASTEXITCODE -eq 0)
{
Success("Build $sln SUCCESS")
}
else
{
Error("Build $sln FAILED")
Info("For more information, please open the following log file:")
Info("$LOGDIR\msbuild.log")
exit
}
}
Function BuildProcess
{
Info("Building Asp.Net OData Projects...")
$script:BUILD_START_TIME = Get-Date
if (Test-Path $BUILDLOG)
{
rm $BUILDLOG
}
# Asp.Net Classic (Product & Unit Test)
RunBuild ($ClassicUnitTestSLN)
# Asp.Net Core (Product & Unit Test)
RunBuild ($NetCoreUnitTesSLN) -vsToolVersion '15.0'
if ($TestType -ne 'Quick')
{
# Asp.Net Classic (Product & Unit Test & E2E)
RunBuild ($ClassicE2ETestSLN)
# Asp.Net Core (Product & Unit Test & E2E)
RunBuild ($NetCoreE2ETestSLN) -vsToolVersion '15.0'
}
Success("Build Done!")
Write-Host
$script:BUILD_END_TIME = Get-Date
}
Function RunTest($title, $testdir, $framework)
{
Info("Running test $title...")
if ($framework -eq 'dotnet')
{
$Conf = "/p:Configuration=" + "$Configuration"
foreach($testProj in $testdir)
{
Info("Launching $testProj...")
& $DOTNETEXE "test" $testProj $Conf "--no-build" >> $TESTLOG
}
}
else
{
Info("Launching $testdir...")
& $VSTEST $testdir $XUNITADAPTER >> $TESTLOG
}
if($LASTEXITCODE -ne 0)
{
Error("Run $title FAILED")
}
}
Function TestProcess
{
Info("Testing Asp.Net OData Projects...")
if (Test-Path $TESTLOG)
{
rm $TESTLOG
}
$script:TEST_START_TIME = Get-Date
RunTest -title 'AspNetClassic UnitTest' -testdir $ClassicUnitTestSuite -framework $ClassicUnitTestFramework
RunTest -title 'AspNetCore UnitTest' -testdir $NetCoreUnitTestSuite -framework $NetCoreUnitTestFramework
if ($TestType -ne 'Quick')
{
RunTest -title 'AspNetClassic E2ETests' -testdir $ClassicE2ETestSuite -framework $ClassicE2ETestFramework
RunTest -title 'AspNetCore E2ETests' -testdir $NetCoreE2ETestSuite -framework $NetCoreE2ETestFramework
}
Info("Test Done.")
TestSummary
$script:TEST_END_TIME = Get-Date
}
# Main Process
if (! (Test-Path $LOGDIR))
{
mkdir $LOGDIR 1>$null
}
if ($TestType -eq 'EnableSkipStrongName')
{
NugetRestoreSolution
BuildProcess
SkipStrongName
Exit
}
elseif ($TestType -eq 'DisableSkipStrongName')
{
NugetRestoreSolution
BuildProcess
DisableSkipStrongName
Exit
}
NugetRestoreSolution
BuildProcess
SkipStrongName
TestProcess
$buildTime = New-TimeSpan $script:BUILD_START_TIME -end $script:BUILD_END_TIME
$testTime = New-TimeSpan $script:TEST_START_TIME -end $script:TEST_END_TIME
Write-Host("Build time:`t" + $buildTime)
Write-Host("Test time:`t" + $testTime)