-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.ps1
executable file
·404 lines (349 loc) · 15.1 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
<#
.SYNOPSIS
NAnt2 build script.
.DESCRIPTION
Build script for NAnt2
.PARAMETER Tasks
One or more execution tasks(s), similar to NAnt or Cake targets as a string of tasks divided by comma
.PARAMETER BuildTarget
Build configuration (mono /.NET and framework version). Valid choices are net-4.5 and mono-4.5."
.PARAMETER BuildMode
MSBuild configuration. Valid choices are Debug and Release.
.PARAMETER Rebuild
Rebuilds the solution. Default is to do a simple build.
.PARAMETER WithCoverage
Calculate coverage when running tests
.PARAMETER StagingDir
Destination folder for staging build artifacts. If not provided it is assumed to be ''.\build''
.PARAMETER PublishDir
Destination folder for publishing build artifacts
.PARAMETER Version
Project version
.PARAMETER BuildNumber
CI build number
.RELEASENOTES
1.0.0 - Initial version
#>
#Requires -Version 7.0
#Requires -PSEdition Core
#####################################
#Required script parameters
#####################################
param(
[Parameter(Position=0, HelpMessage = "One or more execution tasks(s), similar to NAnt or Cake targets")]
[string[]]$Tasks,
[Parameter(HelpMessage = "Build configuration (mono /.NET and framework version). Valid choices are net-4.5 and mono-4.5.")]
#[ValidateSet('net-4.5', 'mono-4.5', IgnoreCase = $true, ErrorMessage="Value '{0}' is invalid. Try one of: '{1}'")]
[string] $BuildTarget = 'net-4.5',
[Parameter(HelpMessage = "MSBuild configuration. Valid choices are Debug and Release.")]
[ValidateSet('Debug', 'Release', IgnoreCase = $true, ErrorMessage="Value '{0}' is invalid. Try one of: '{1}'")]
[string] $BuildMode = 'Debug',
[Parameter(HelpMessage = 'Rebuilds the solution. Equivalent of clean + build')]
[switch] $Rebuild,
[Parameter(HelpMessage = 'Calculate coverage when running tests')]
[switch] $WithCoverage,
[Parameter(HelpMessage = 'Publish folder for the release artifacts. If not provided it is assumed to be ''.\publish''')]
[string] $PublishDir,
[Parameter(HelpMessage = 'Project version')]
[string] $Version = '0.93',
[Parameter(HelpMessage = 'CI build number')]
[string] $BuildNumber = '0'
)
Set-StrictMode -Version latest
#####################################################
# initialization: Invoke-Build module
#####################################################
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
$ErrorActionPreference = 'Stop'
$policy = (Get-PSRepository PSGallery).InstallationPolicy
try {
Import-Module InvokeBuild
}
catch {
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module InvokeBuild -Scope CurrentUser -Force -Repository PSGallery -SkipPublisherCheck -Verbose
Import-Module InvokeBuild
}
finally {
Set-PSRepository PSGallery -InstallationPolicy $policy
}
try {
Invoke-Build -Task $Tasks -File $MyInvocation.MyCommand.Path @PSBoundParameters
}
catch {
$PSItem | Format-List * -Force | Out-String
$LASTEXITCODE = 1
}
if(0 -ne $LASTEXITCODE)
{
Write-Host "Exit code: $LASTEXITCODE" -ForegroundColor Red
}
exit $LASTEXITCODE
}
# load first own script related to powershell and powershell modules
. "$(Join-Path $PSScriptRoot 'scripts' 'pwsh_functions.ps1')"
#load additional modules
Set-PSModules -Modules jit-semver -AllowPrerelease
#load own build-related powershell scripts
. "$(Join-Path $PSScriptRoot 'scripts' 'dotnet_framework_functions.ps1')"
. "$(Join-Path $PSScriptRoot 'scripts' 'nant_functions.ps1')"
#####################################################
# initialization: global variables
#####################################################
if(-not $PublishDir) {
$PublishDir = Join-Path $BuildRoot "publish"
}
if(-not (Test-Path -LiteralPath $PublishDir))
{
New-Item $PublishDir -ItemType Directory -Force
}
$SRC_DIR = Join-Path $BuildRoot "src"
$TESTS_DIR = Join-Path $BuildRoot "tests"
$BOOTSTRAP_DIR = Join-Path $BuildRoot "bootstrap"
$BUILD_DIR = Join-Path $BuildRoot "build"
$PUBLISH_DIR = $PublishDir
$TOOLS_DIR = Join-Path $BuildRoot "tools"
$REPORTS_DIR = Join-Path $BuildRoot "reports"
$MSBUILD = Get-MsBuild -ToolsDir $TOOLS_DIR
$BUILD_CONFIGURATION = Get-BuildConfiguration -FrameworkTarget $BuildTarget -IsDebug:$($BuildMode -eq 'Debug')
#MSBuild paralelism
$parallelism = 3
#MSBuild verbosity: q[uiet], m[inimal], n[ormal] (default), d[etailed], and diag[nostic]
$verbosity = 'm'
#####################################################
# initialization: before build
#####################################################
Enter-Build {
Write-Build Blue "
Src directory: $SRC_DIR,
Tests directory: $TESTS_DIR,
Boostrap directory: $BOOTSTRAP_DIR,
Build directory: $BUILD_DIR,
Publish directory: $PUBLISH_DIR,
Tools directory: $TOOLS_DIR,
Reports directory: $REPORTS_DIR,
msbuild.exe path: $MSBUILD,
Build configuration - platform: $($BUILD_CONFIGURATION.Platform),
Build configuration - version: $($BUILD_CONFIGURATION.Version),
Build configuration - compilation flags: $($BUILD_CONFIGURATION.CompilationFlags),
Build configuration - compiler: $($BUILD_CONFIGURATION.Compiler),
Build configuration - compiler language: $($BUILD_CONFIGURATION.CompilerLanguage),
Build configuration - resgen executable: $($BUILD_CONFIGURATION.ResgenExe),
Build configuration - compiler debug flag: $($BUILD_CONFIGURATION.CompilerDebug)"
Invoke-VsDevCmd -ToolsDir $TOOLS_DIR
}
#####################################
# Tasks
#####################################
task Init {
<#
.SYNOPSIS
Initialization of any folder structure of files required for a succesful build
#>
Write-Output '[Init] Preparing to run build script'
# Make sure bootstrap folder exists
if ((Test-Path $BuildRoot) -and !(Test-Path $BOOTSTRAP_DIR)) {
Write-Output "[Init] Creating bootstrap directory..."
New-Item -Path $BOOTSTRAP_DIR -Type Directory -Force | Out-Null
}
Copy-Item -Path (Join-Path $BuildRoot 'lib') -Destination $BOOTSTRAP_DIR -Recurse -Force
# Mono loads log4net before privatebinpath is set-up, so we need this in the same directory as NAnt.exe
Copy-Item -Path (Join-Path $BuildRoot 'lib' 'common' 'neutral' '*') -Include 'log4net.dll' -Destination $BOOTSTRAP_DIR -Force
Copy-Item -Path (Join-Path $SRC_DIR 'NAnt.Console' '*') -Include 'App.config' -Destination (Join-Path $BOOTSTRAP_DIR 'NAnt.exe.config') -Force
# Make sure publish folder exists
if ((Test-Path $BuildRoot) -and $BuildMode -eq 'Release' -and !(Test-Path $PUBLISH_DIR)) {
Write-Output "[Init] Creating publish directory..."
New-Item -Path $PUBLISH_DIR -Type Directory -Force | Out-Null
}
# Make sure reports folder exists
if ((Test-Path $BuildRoot) -and !(Test-Path $REPORTS_DIR)) {
Write-Output "[Init] Creating reports directory..."
New-Item -Path $REPORTS_DIR -Type Directory -Force | Out-Null
}
}
task Clean {
<#
.SYNOPSIS
Executes any cleanup required before building the project.
#>
if (Test-Path $BOOTSTRAP_DIR ) {
Write-Output "[Clean] Cleaning bootstrap directory..."
Get-ChildItem -Path $BOOTSTRAP_DIR | Remove-Item -Recurse -Force -ErrorAction Ignore
}
if (Test-Path $BUILD_DIR ) {
Write-Output "[Clean] Cleaning build directory..."
Get-ChildItem -Path $BUILD_DIR | Remove-Item -Recurse -Force -ErrorAction Ignore
}
if (Test-Path $PUBLISH_DIR ) {
Write-Output "[Clean] Cleaning publish directory..."
Get-ChildItem -Path $PUBLISH_DIR | Remove-Item -Recurse -Force -ErrorAction Ignore
}
Write-Output '[Clean] Cleaning reports folder except NDepend one'
if (Test-Path $REPORTS_DIR ) {
Write-Output -Message "[Clean] Cleaning reports directory..."
Get-ChildItem -Path $REPORTS_DIR -Exclude NDependOut | Remove-Item -Recurse -Force -ErrorAction Ignore
}
}
task Deep-Clean Clean, {
<#
.SYNOPSIS
Executes default cleanup (boostrap, build, publish and reports directories) and also cleans /bin and /obj folders
#>
Write-Output "[Deep-Clean] Cleaning /bin and /obj folders from $SRC_DIR, tests and $TOOLS_DIR"
Get-ChildItem $SRC_DIR -Include bin,obj -Recurse | Remove-Item -Recurse -Force -ErrorAction Ignore
Get-ChildItem (Join-Path $BuildRoot "tests") -Include bin,obj -Recurse | Remove-Item -Recurse -Force -ErrorAction Ignore
Get-ChildItem $TOOLS_DIR -Include bin,obj -Recurse | Remove-Item -Recurse -Force -ErrorAction Ignore
}
task Bootstrap Clean, Init, {
<#
.SYNOPSIS
Builds NAnt.exe, NAnt.Core, NAnt.DotNet, NAnt.Compression and, if on Windows, NAnt.Win32 and places them in bootstrap dir
#>
Write-Output "[Bootstrap] Building NAnt.exe"
& $($BUILD_CONFIGURATION.Compiler) `
$($BUILD_CONFIGURATION.CompilerDebug) `
/target:exe `
/platform:anycpu `
/langversion:$($BUILD_CONFIGURATION.CompilerLanguage) `
/define:$($BUILD_CONFIGURATION.CompilationFlags) `
/out:$(Join-Path $BOOTSTRAP_DIR 'NAnt.exe') `
/r:$(Join-Path $BOOTSTRAP_DIR 'log4net.dll') `
/r:System.Configuration.dll `
/recurse:$(Join-Path $SRC_DIR 'NAnt.Console' '*.cs') $(Join-Path $SRC_DIR SolutionInfo.cs)
Write-Output "[Bootstrap] Building NAnt.Core"
& $($BUILD_CONFIGURATION.ResgenExe) `
$(Join-Path $SRC_DIR 'NAnt.Core' 'Resources' 'Strings.resx') `
$(Join-Path $BOOTSTRAP_DIR 'NAnt.Core.Resources.Strings.resources')
assert($LastExitCode -eq 0) "Resource generation for NAnt.Core failed"
& $($BUILD_CONFIGURATION.Compiler) `
$($BUILD_CONFIGURATION.CompilerDebug) `
/target:library `
/platform:anycpu `
/langversion:$($BUILD_CONFIGURATION.CompilerLanguage) `
/warn:0 `
/define:$($BUILD_CONFIGURATION.CompilationFlags) `
/out:$(Join-Path $BOOTSTRAP_DIR 'NAnt.Core.dll') `
/resource:$(Join-Path $BOOTSTRAP_DIR 'NAnt.Core.Resources.Strings.resources') `
/r:$(Join-Path $BOOTSTRAP_DIR 'log4net.dll') `
/r:System.Web.dll `
/r:System.Configuration.dll `
/recurse:$(Join-Path $SRC_DIR 'NAnt.Core' '*.cs') $(Join-Path $SRC_DIR SolutionInfo.cs)
assert($LastExitCode -eq 0) "Compilation of NAnt.Core failed"
Write-Output "[Bootstrap] Building NAnt.DotNet"
& $($BUILD_CONFIGURATION.ResgenExe) `
$(Join-Path $SRC_DIR 'NAnt.DotNet' 'Resources' 'Strings.resx') `
$(Join-Path $BOOTSTRAP_DIR 'NAnt.DotNet.Resources.Strings.resources')
assert($LastExitCode -eq 0) "Resource generation for NAnt.DotNet failed"
& $($BUILD_CONFIGURATION.Compiler) `
$($BUILD_CONFIGURATION.CompilerDebug) `
/target:library `
/platform:anycpu `
/langversion:$($BUILD_CONFIGURATION.CompilerLanguage) `
/warn:0 `
/define:$($BUILD_CONFIGURATION.CompilationFlags) `
/out:$(Join-Path $BOOTSTRAP_DIR 'NAnt.DotNetTasks.dll') `
/resource:$(Join-Path $BOOTSTRAP_DIR 'NAnt.DotNet.Resources.Strings.resources') `
/r:$(Join-Path $BOOTSTRAP_DIR 'NAnt.Core.dll') `
/r:$(Join-Path $BOOTSTRAP_DIR 'lib' 'common' 'neutral' 'NDoc.Core.dll') `
/recurse:$(Join-Path $SRC_DIR 'NAnt.DotNet' '*.cs') `
$(Join-Path $SRC_DIR SolutionInfo.cs)
assert($LastExitCode -eq 0) "Compilation of NAnt.DotNet failed"
Write-Output "[Bootstrap] Building NAnt.Compression"
& $($BUILD_CONFIGURATION.Compiler) `
$($BUILD_CONFIGURATION.CompilerDebug) `
/target:library `
/platform:anycpu `
/langversion:$($BUILD_CONFIGURATION.CompilerLanguage) `
/warn:0 `
/define:$($BUILD_CONFIGURATION.CompilationFlags) `
/out:$(Join-Path $BOOTSTRAP_DIR 'NAnt.CompressionTasks.dll') `
/r:$(Join-Path $BOOTSTRAP_DIR 'NAnt.Core.dll') `
/r:$(Join-Path $BOOTSTRAP_DIR 'lib' 'common' 'neutral' 'ICSharpCode.SharpZipLib.dll') `
/recurse:$(Join-Path $SRC_DIR 'NAnt.Compression' '*.cs') `
$(Join-Path $SRC_DIR SolutionInfo.cs)
assert($LastExitCode -eq 0) "Compilation of NAnt.Compression failed"
if($IsWindows) {
Write-Output "[Bootstrap] On Windows. Building NAnt.Win32"
& $($BUILD_CONFIGURATION.Compiler) `
$($BUILD_CONFIGURATION.CompilerDebug) `
/target:library `
/platform:anycpu `
/langversion:$($BUILD_CONFIGURATION.CompilerLanguage) `
/warn:0 `
/define:$($BUILD_CONFIGURATION.CompilationFlags) `
/out:$(Join-Path $BOOTSTRAP_DIR 'NAnt.Win32Tasks.dll') `
/r:$(Join-Path $BOOTSTRAP_DIR 'NAnt.Core.dll') `
/r:$(Join-Path $BOOTSTRAP_DIR 'NAnt.DotNetTasks.dll') `
/r:System.ServiceProcess.dll `
/recurse:$(Join-Path $SRC_DIR 'NAnt.Win32' '*.cs') `
$(Join-Path $SRC_DIR SolutionInfo.cs)
assert($LastExitCode -eq 0) "Compilation of NAnt.Win32 failed"
}
}
task Build-NAnt Bootstrap, {
<#
.SYNOPSIS
Builds the rest of the NAnt projects using the boostraped NAnt.exe
#>
Write-Output "[Build-NAnt] Building all NAnt projects"
Invoke-NAnt `
-BuildFile NAnt.build `
-Framework $BuildTarget `
-BuildMode $BuildMode `
-PrjVersion $Version `
-PrjBuildNumber $BuildNumber `
-NAntTarget build
assert($LastExitCode -eq 0) "Running NAnt2 with target ''build'' failed"
}
task Run-Test Bootstrap, {
<#
.SYNOPSIS
Builds the rest of the NAnt projects using the boostraped NAnt.exe and runs all available tests
#>
Write-Output "[Run-Test] Building and runing all tests for all NAnt projects"
Invoke-NAnt `
-BuildFile NAnt.build `
-Framework $BuildTarget `
-BuildMode $BuildMode `
-PrjVersion $Version `
-PrjBuildNumber $BuildNumber `
-NAntTarget test
assert($LastExitCode -eq 0) "Running NAnt2 with target ''test'' failed"
}
task Run-CodeQuality Bootstrap, {
<#
.SYNOPSIS
Builds everything, runs the tests and calculates code coverage
#>
Write-Output "[Run-Test] Code quality analysis of NAnt projects"
Invoke-NAnt `
-BuildFile NAnt.build `
-Framework $BuildTarget `
-BuildMode $BuildMode `
-PrjVersion $Version `
-PrjBuildNumber $BuildNumber `
-NAntTarget code.quality
assert($LastExitCode -eq 0) "Running NAnt2 with target ''code.quality'' failed"
}
task Stage {
#$(NANT) $(TARGET_FRAMEWORK) -f:NAnt.build install -D:prefix="$(prefix)" -D:destdir="$(DESTDIR)" -D:doc.prefix="$(docdir)"
}
task Demo {
Write-Output "[Demo] SemVer stuff" #"\[[v|V](\d+(\.\d+){1,3}) (?:Unreleased)\]"
$filter = 'rel-\d{1}-\d+'
Get-SemVer -Filter $filter -Verbose
Get-SemVerNext -Verbose
$items = (git tag)
@($items |
Select-String -Pattern '[0-9]+-' |
Sort-Object) + ( $items |
Select-String -Pattern '[0-9]+-' -NotMatch |
Sort-Object) | Select-String -Pattern "^$filter"
}
task All Bootstrap, Build-NAnt
#region Default Task
task . Build
<# rel-0-92
rel-0-92-alpha1
rel-0-92-beta1
rel-0-92-rc1#>