-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
executable file
·421 lines (342 loc) · 12.3 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
#!/usr/bin/env pwsh
param(
[ValidateNotNullOrEmpty()]
[string]$Target = "FullBuild",
[ValidateNotNullOrEmpty()]
[ValidateSet("Debug", "Release")]
[string]$Configuration = "Release",
[string]$DotnetVerbosity = "minimal",
[string]$VersionSuffix = ""
)
#######################################################################
# SHARED VARIABLES
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$repositoryDir = (Get-Item $PSScriptRoot).FullName
$srcDir = Join-Path $repositoryDir "src"
$dotnetSolutionFile = Get-ChildItem -Path $srcDir -Filter "*.sln" | Select-Object -First 1
$buildCoreVersionFile = Join-Path $repositoryDir "version.yaml"
$buildCoreVersion = $(Get-Content "$buildCoreVersionFile").Substring("version:".Length).Trim()
$buildVersion = "$buildCoreVersion$VersionSuffix"
$dockerComposeProject = "poc"
# This build system expects following solution layout:
# solution_root/ -- $repositoryDir
# build.ps1 -- this file - PowerShell build CLI
# docker-compose.yaml -- Docker Compose definition of the complete environment, including all required infrastructure
# src/ -- $srcDir
# Project1/
# Project1.csproj -- project base filename matches directory name
# Project1.Tests/
# Project1.Tests.csproj -- tests projects are xUnit-based; project name must have suffix '.Tests'
# Project2/
# Project2.fsproj
# imagename.Dockerfile -- if the `*.Dockerfile` is present, we'll build Docker image `imagename`
# Project2.Tests/
# Project2.Tests.fsproj
# SolutionName.sln -- only one '.sln' file in 'src'
# version.yaml -- core part of solution SemVer
#######################################################################
# LOGGING
function LogInfo {
param([ValidateNotNullOrEmpty()] [string]$Message)
Write-Host -ForegroundColor Green $Message
}
function LogWarning {
param([ValidateNotNullOrEmpty()] [string]$Message)
Write-Host -ForegroundColor Yellow "*** $Message"
}
function LogError {
param([ValidateNotNullOrEmpty()] [string]$Message)
Write-Host -ForegroundColor Red "*** $Message"
}
function LogStep {
param(
[ValidateNotNullOrEmpty()] [string]$Message,
[ValidateNotNullOrEmpty()] [string]$Title = "STEP"
)
Write-Host -ForegroundColor Yellow "--- ${Title}: $Message"
}
function LogTarget {
param([ValidateNotNullOrEmpty()] [string]$Message)
Write-Host ""
Write-Host -ForegroundColor Green "--- TARGET: $Message"
}
function LogCmd {
param([ValidateNotNullOrEmpty()] [string]$Message)
Write-Host -ForegroundColor Yellow "--- $Message"
}
#######################################################################
# STEPS
function PreludeStep_ValidateDotnetCli {
LogStep "Check .NET CLI" -Title "PRELUDE"
$dotnetCmd = Get-Command dotnet -ErrorAction Ignore
if (-not $dotnetCmd) {
LogError ".NET SDK CLI (dotnet) is not available. Refer to https://dotnet.microsoft.com/download for more information."
Exit 1
}
}
function PreludeStep_ValidateDockerCli {
LogStep "Check Docker CLI" -Title "PRELUDE"
$dockerCmd = Get-Command docker -ErrorAction Ignore
if (-not $dockerCmd) {
LogError "Docker CLI (docker) is not available. Refer to https://docs.docker.com/get-docker/ for more information."
Exit 1
}
}
function Step_PruneBuild {
LogStep "PruneBuild"
$pruneDir = $repositoryDir
LogWarning "Pruning $pruneDir build artifacts"
# Prune nested directories
'bin', 'obj', 'publish', 'TestResults' | ForEach-Object {
Get-ChildItem -Path $pruneDir -Filter $_ -Directory -Recurse | ForEach-Object { $_.Delete($true) }
}
# Prune nested files
'*.trx', '*.fsx.lock', '*.Tests_*.xml' | ForEach-Object {
Get-ChildItem -Path $pruneDir -Filter $_ -File -Recurse | ForEach-Object { $_.Delete() }
}
# Prune top-level items
# Adjust the list as needed
# '.ionide', (Join-Path 'benchmark' 'reports') | ForEach-Object {
# $dir = Join-Path $pruneDir $_
# if (Test-Path $dir) {
# Remove-Item -Path $dir -Recurse -Force
# }
# }
}
function Step_PruneDocker {
LogStep "PruneDocker"
$pruneDir = $repositoryDir
LogWarning "Pruning $pruneDir Docker artifacts"
'container', 'image', 'volume', 'network' | ForEach-Object {
LogCmd "docker $_ prune -f"
& docker $_ prune -f | Out-Null
}
$(docker image ls --format "{{.Repository}}:{{.Tag}}") | ForEach-Object {
if ($_.StartsWith("${dockerComposeProject}-")) {
LogCmd "docker image rm $_"
& docker image rm $_ | Out-Null
}
}
$(docker volume ls --format "{{.Name}}") | ForEach-Object {
if ($_.StartsWith("${dockerComposeProject}_")) {
LogCmd "docker volume rm $_"
& docker volume rm $_ | Out-Null
}
}
}
function Step_DotnetClean {
LogStep "dotnet clean $dotnetSolutionFile --verbosity $DotnetVerbosity -nologo"
& dotnet clean "$dotnetSolutionFile" --verbosity $DotnetVerbosity -nologo
if (-not $?) { exit $LastExitCode }
}
function Step_DotnetRestore {
LogStep "dotnet restore $dotnetSolutionFile --verbosity $DotnetVerbosity -nologo"
& dotnet restore "$dotnetSolutionFile" --verbosity $DotnetVerbosity -nologo
if (-not $?) { exit $LastExitCode }
}
function Step_DotnetBuild {
LogStep "dotnet build $dotnetSolutionFile --no-restore --configuration $Configuration --verbosity $DotnetVerbosity -nologo /p:Version=$buildVersion"
$currentLocation = Get-Location
try {
Set-Location $srcDir
& dotnet build "$dotnetSolutionFile" --no-restore --configuration $Configuration --verbosity $DotnetVerbosity -nologo /p:Version=$buildVersion
if (-not $?) { exit $LastExitCode }
}
finally {
Set-Location $currentLocation
}
}
function Step_DotnetPublish {
param([ValidateNotNullOrEmpty()] [string]$ProjectFile, [ValidateNotNullOrEmpty()] [string]$PublishOutput)
LogStep "dotnet publish $ProjectFile --output $PublishOutput --configuration $Configuration --verbosity $DotnetVerbosity -nologo /p:Version=$buildVersion"
& dotnet publish "$ProjectFile" --output "$PublishOutput" --configuration $Configuration --verbosity $DotnetVerbosity -nologo /p:Version=$buildVersion
if (-not $?) { exit $LastExitCode }
}
function Step_DotnetTest {
param([ValidateNotNullOrEmpty()] [string]$ProjectFile)
LogStep "dotnet test $ProjectFile --no-build --configuration $Configuration --logger:trx -nologo"
& dotnet test "$ProjectFile" --no-build --configuration $Configuration --logger:trx --logger:"console;verbosity=normal" -nologo
if (-not $?) { exit $LastExitCode }
}
function Step_DockerBuild {
param([ValidateNotNullOrEmpty()] [string]$DockerFilePath)
$file = Get-Item $DockerFilePath
$imageName = $file.BaseName
$imageTag = "${imageName}:${buildVersion}"
$dockerfile = $file.Name
$dir = $file.Directory.FullName
LogStep "docker build -t $imageTag -f $dockerfile ."
$currentLocation = Get-Location
try {
Set-Location $dir
& docker build -t $imageTag -f $dockerfile .
if (-not $?) { exit $LastExitCode }
}
finally {
Set-Location $currentLocation
}
}
function Step_DockerComposeStart {
LogStep "docker compose -p $dockerComposeProject up --build --abort-on-container-exit"
& docker compose -p $dockerComposeProject up --build --abort-on-container-exit
if (-not $?) { exit $LastExitCode }
}
function Step_DockerComposeStartDetached {
LogStep "docker compose -p $dockerComposeProject up --build --detach"
& docker compose -p $dockerComposeProject up --build --detach
if (-not $?) { exit $LastExitCode }
}
function Step_DockerComposeStop {
LogStep "docker compose -p $dockerComposeProject down"
& docker compose -p $dockerComposeProject down
if (-not $?) { exit $LastExitCode }
}
#######################################################################
# PRELUDE TARGETS
function Target_Prelude_DotnetCli {
PreludeStep_ValidateDotnetCli
}
function Target_Prelude_DockerCli {
PreludeStep_ValidateDockerCli
}
function Target_Prelude {
DependsOn "Prelude.DotnetCli"
DependsOn "Prelude.DockerCli"
}
#######################################################################
# TARGETS
function Target_Dotnet_Clean {
DependsOn "Prelude.DotnetCli"
LogTarget "Dotnet.Clean"
Step_DotnetClean
}
function Target_Dotnet_Restore {
DependsOn "Prelude.DotnetCli"
LogTarget "Dotnet.Restore"
Step_DotnetRestore
}
function Target_Dotnet_Build {
DependsOn "Prelude.DotnetCli"
DependsOn "Dotnet.Restore"
LogTarget "Dotnet.Build"
Step_DotnetBuild
}
function Target_Dotnet_Test {
DependsOn "Prelude.DotnetCli"
DependsOn "Dotnet.Build"
LogTarget "Dotnet.Test"
$projects = Get-ChildItem -Path $srcDir -Filter "*.Tests.?sproj" -Recurse -File
foreach ($projectFile in $projects) {
Step_DotnetTest $projectFile
}
}
function Target_Dotnet_Publish {
DependsOn "Prelude.DotnetCli"
DependsOn "Dotnet.Build"
LogTarget "Dotnet.Publish"
$dockerfiles = Get-ChildItem -Path $srcDir -Filter "*.Dockerfile" -Recurse -File
foreach ($dockerFile in $dockerfiles) {
LogInfo "Dockerfile found: $dockerFile"
$projectDirectory = $dockerFile.Directory
$projectFile = Get-ChildItem -Path $projectDirectory -Filter "*.?sproj" | Select-Object -First 1
$publishOutput = [System.IO.Path]::Combine($projectDirectory, "bin", "publish")
Step_DotnetPublish $projectFile $publishOutput
}
}
function Target_Docker_Build {
DependsOn "Prelude"
DependsOn "Dotnet.Publish"
LogTarget "Docker.Build"
$dockerFiles = Get-ChildItem -Path $srcDir -Filter "*.Dockerfile" -Recurse -File
foreach ($dockerFile in $dockerFiles) {
LogInfo "Dockerfile found: $dockerFile"
Step_DockerBuild $dockerFile
}
}
function Target_DockerCompose_Start {
DependsOn "Prelude"
DependsOn "Dotnet.Publish"
LogTarget "DockerCompose.Start"
try {
Step_DockerComposeStart
}
finally {
# ensure proper cleanup
Step_DockerComposeStop
}
}
function Target_DockerCompose_StartDetached {
DependsOn "Prelude"
DependsOn "Dotnet.Publish"
LogTarget "DockerCompose.StartDetached"
Step_DockerComposeStartDetached
}
function Target_DockerCompose_Stop {
DependsOn "Prelude.DockerCli"
LogTarget "DockerCompose.Stop"
Step_DockerComposeStop
}
function Target_Prune_Build {
DependsOn "Prelude.DotNetCli"
LogTarget "Prune.Build"
Step_PruneBuild
}
function Target_Prune_Docker {
DependsOn "Prelude.DockerCli"
LogTarget "Prune.Docker"
Step_PruneDocker
}
function Target_Prune {
DependsOn "Prelude"
DependsOn "Prune.Build"
DependsOn "Prune.Docker"
}
function Target_FullBuild {
DependsOn "Prelude"
DependsOn "Dotnet.Build"
DependsOn "Dotnet.Test"
DependsOn "Dotnet.Publish"
}
#######################################################################
# DEPENDENCIES TRACKING
$targetCalls = @{ }
function DependsOn {
param([ValidateNotNullOrEmpty()] [string]$Target)
if (-not $targetCalls.ContainsKey($Target)) {
Invoke_BuildTarget $Target
$targetCalls.Add($Target, $(Get-Date))
}
}
function Invoke_BuildTarget {
param([ValidateNotNullOrEmpty()] [string]$Target)
$normalizedTarget = $Target.Replace(".", "_")
Invoke-Expression "Target_$normalizedTarget"
}
#######################################################################
# MAIN ENTRY POINT
$exitResult = 0
$currentLocation = Get-Location
try {
LogInfo "*** BUILD: $Target ($buildVersion $Configuration) in $repositoryDir"
Set-Location $repositoryDir
Invoke_BuildTarget $Target
Write-Host ""
LogInfo "DONE"
}
catch [System.Exception] {
$errorMessage = "$_"
if ($errorMessage.StartsWith("The term 'Target_$Target' is not recognized") -or
$errorMessage.StartsWith("The term 'Target_$normalizedTarget' is not recognized")) {
LogError("Target $Target is not recognized")
}
else {
LogError($errorMessage)
}
$exitResult = 1
}
finally {
$stopwatch.Stop()
LogInfo "*** Completed in: $($stopwatch.Elapsed)"
Set-Location $currentLocation
}
Exit $exitResult