forked from microsoft/OpenAPI.NET.OData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
338 lines (285 loc) · 8.96 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
# Default to Debug
$Configuration = 'Debug'
# Color
$Success = 'Green'
$Warning = 'Yellow'
$Err = 'Red'
if ($args.Count -eq 0)
{
$TestType = 'All'
$Configuration = 'Release'
}
elseif ($args[0] -match 'DisableSkipStrongName')
{
$TestType = "DisableSkipStrongName"
}
elseif ($args[0] -match 'EnableSkipStrongName')
{
$TestType = "EnableSkipStrongName"
}
elseif ($args[0] -match 'SkipStrongName')
{
# SkipStrongName is a legacy options.
$TestType = "EnableSkipStrongName"
}
else
{
Write-Host 'Please choose Test or StrongName!' -ForegroundColor $Err
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 2017
$VS15MSBUILD=$PROGRAMFILESX86 + "\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe"
$VSTEST = $PROGRAMFILESX86 + "\Microsoft Visual Studio\2017\Enterprise\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"
# Other variables
$FXCOP = $FXCOPDIR + "\FxCopCmd.exe"
$BUILDLOG = $LOGDIR + "\msbuild.log"
$TESTLOG = $LOGDIR + "\mstest.log"
$TESTDIR = $ENLISTMENT_ROOT + "\bin\$Configuration\Test\net461"
$PRODUCTDIR = $ENLISTMENT_ROOT + "\bin\$Configuration\net461"
$NUGETEXE = $PROGRAMFILESX86 + "\Microsoft Visual Studio\2017\Enterprise\MSBuild\ReadyRoll\OctoPack\build\NuGet.exe"
$NUGETPACK = $ENLISTMENT_ROOT + "\packages"
$ProductDlls = "Microsoft.OpenApi.OData.Reader.dll"
$XUnitTestDlls = "Microsoft.OpenApi.OData.Reader.Tests.dll"
$AllTestSuite = @()
ForEach($dll in $XUnitTestDlls)
{
$AllTestSuite += $TESTDIR + "\" + $dll
}
Function GetDlls
{
$dlls = @()
ForEach($dll in $ProductDlls)
{
$dlls += $PRODUCTDIR + "\" + $dll
}
ForEach($dll in $XUnitTestDlls)
{
$dlls += $TESTDIR + "\" + $dll
}
return $dlls
}
Function SkipStrongName
{
$SnLog = $LOGDIR + "\SkipStrongName.log"
Out-File $SnLog
Write-Host 'Skip strong name validations for Microsoft.OpenApi.OData 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
}
Write-Host "SkipStrongName Done" -ForegroundColor $Success
}
Function DisableSkipStrongName
{
$SnLog = $LOGDIR + "\DisableSkipStrongName.log"
Out-File $SnLog
Write-Host 'Disable skip strong name validations for Microsoft.OpenApi.OData 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
}
Write-Host "DisableSkipStrongName Done" -ForegroundColor $Success
}
Function Cleanup
{
#TODO: Add some clean tasks
Write-Host "Clean Done" -ForegroundColor $Success
}
Function CleanBeforeScorch
{
#TODO: Add some clean tasks
Write-Host "Clean Done" -ForegroundColor $Success
}
# Incremental build and rebuild
Function RunBuild ($sln)
{
Write-Host "*** Building $sln ***"
$slnpath = $ENLISTMENT_ROOT + "\$sln"
$Conf = "/p:Configuration=" + "$Configuration"
# Default to VS2017
$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)
{
Write-Host "Build $sln SUCCESS" -ForegroundColor $Success
}
else
{
Write-Host "Build $sln FAILED" -ForegroundColor $Err
Write-Host "For more information, please open the following test result files:"
Write-Host "$LOGDIR\msbuild.log"
Cleanup
exit
}
}
Function NugetRestoreSolution
{
Write-Host '**********Pull NuGet Packages*********'
& $NUGETEXE "restore" ($ENLISTMENT_ROOT + "\Microsoft.OpenApi.OData.sln")
}
Function BuildProcess
{
Write-Host '**********Start To Build The Project*********'
$script:BUILD_START_TIME = Get-Date
if (Test-Path $BUILDLOG)
{
rm $BUILDLOG
}
RunBuild ('Microsoft.OpenApi.OData.sln')
Write-Host "Build Done" -ForegroundColor $Success
$script:BUILD_END_TIME = Get-Date
}
Function TestSummary
{
Write-Host 'Collecting test results ...'
$file = Get-Content -Path $TESTLOG
$pass = 0
$skipped = 0
$fail = 0
$trxfile = New-Object -TypeName System.Collections.ArrayList
$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 failed by taking the substring between "Failed: " and "."
# The regex first extracts the string after the hardcoded "Failed: " (i.e. "#.")
# Then we tokenize by "." and retrieve the first token which is the number for skipped.
$pattern = "Failed: (.*)"
$extractedNumber = [regex]::match($line, $pattern).Groups[1].Value.Split(".")[0]
$fail += $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
}
}
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 -ne 0)
{
Write-Host "Find failed test information at:" $TESTLOG -ForegroundColor $Err
}
else
{
Write-Host "Congratulation! All of the tests passed!" -ForegroundColor $Success
}
}
Function RunTest($title, $testdir)
{
Write-Host "**********Running $title***********"
& $VSTEST $testdir >> $TESTLOG
if($LASTEXITCODE -ne 0)
{
Write-Host "Run $title FAILED" -ForegroundColor $Err
}
}
Function TestProcess
{
Write-Host '**********Start To Run The Test*********'
if (Test-Path $TESTLOG)
{
rm $TESTLOG
}
$script:TEST_START_TIME = Get-Date
cd $TESTDIR
if ($TestType -eq 'All')
{
RunTest -title 'All Tests' -testdir $AllTestSuite
}
else
{
Write-Host 'Error : TestType' -ForegroundColor $Err
Cleanup
exit
}
Write-Host "Test Done" -ForegroundColor $Success
$script:TEST_END_TIME = Get-Date
TestSummary
cd $ENLISTMENT_ROOT
}
Function FxCopProcess
{
# TODO:
}
# Main Process
if (! (Test-Path $LOGDIR))
{
mkdir $LOGDIR 1>$null
}
if ($TestType -eq 'EnableSkipStrongName')
{
CleanBeforeScorch
NugetRestoreSolution
BuildProcess
SkipStrongName
Exit
}
elseif ($TestType -eq 'DisableSkipStrongName')
{
CleanBeforeScorch
NugetRestoreSolution
BuildProcess
DisableSkipStrongName
Exit
}
CleanBeforeScorch
NugetRestoreSolution
BuildProcess
SkipStrongName
TestProcess
FxCopProcess
Cleanup
$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)