forked from BullOak/BullOak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
executable file
·254 lines (203 loc) · 7.45 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
#!/usr/bin/env pwsh
Param(
[ValidateNotNullOrEmpty()]
[string]$Target = "Default",
[ValidateNotNullOrEmpty()]
[ValidateSet("Debug", "Release")]
[string]$Configuration = "Release",
[string]$Version = "0.0.1",
[string]$DotnetVerbosity = "minimal"
)
#######################################################################
# SHARED VARIABLES
$buildDir = $PSScriptRoot
$repositoryDir = (Get-Item $buildDir).FullName
$srcDir = [System.IO.Path]::Combine($repositoryDir, "src")
$dotnetSolutionFile = Get-ChildItem -Path $srcDir -Filter "*.sln" | Select-Object -First 1
$requiredDotnetVersion = "2.2"
$completedTargets = @{}
# This build system expects following solution structure:
# solution_root/
# build.ps1 -- PowerShell build CLI
# src/
# 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'
#######################################################################
# 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)
Write-Host -ForegroundColor Yellow "--- STEP: $Message"
}
Function LogTarget {
Param([ValidateNotNullOrEmpty()] [string]$Message)
Write-Host -ForegroundColor Green "--- TARGET: $Message"
}
Function LogCmd {
Param([ValidateNotNullOrEmpty()] [string]$Message)
Write-Host -ForegroundColor Yellow "--- $Message"
}
#######################################################################
# TARGETS MANAGEMENT
Function DependsOn {
Param([ValidateNotNullOrEmpty()] [string]$Target)
Invoke-Expression $Target
}
Function IsTargetCompleted {
Param([ValidateNotNullOrEmpty()] [string]$Target)
$completedTargets[$Target]
}
Function MarkTargetAsCompleted {
Param([ValidateNotNullOrEmpty()] [string]$Target)
$completedTargets[$Target] = $True
}
#######################################################################
# STEPS
Function PreludeStep_ValidateDotNetCli {
LogStep "Prelude: .NET CLI"
# Check if a suitable .NET CLI is available
$requiredDotnetMessage = "*** Required 'dotnet' version $requiredDotnetVersion or higher. Install .NET Core SDK from https://www.microsoft.com/net/download"
try {
[System.Version]$dotnetVersion = dotnet --version
[System.Version]$minSupportedDotnetVersion = $requiredDotnetVersion
if (-Not($dotnetVersion -ge $minSupportedDotnetVersion)) {
LogError Red $requiredDotnetMessage
Exit 1
}
}
catch {
LogError $requiredDotnetMessage
Exit 1
}
}
Function Step_PruneBuild {
LogStep "PruneBuild"
$pruneDir = $repositoryDir
LogWarning "Pruning $pruneDir build artifacts"
# Prune nested directories
'bin', 'obj', 'publish' | 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
'.fable', '.ionide', 'build/.fake', 'build/tools/fake-cli', 'build/tools/pbm', 'node_modules', 'paket-files' | ForEach-Object {
if (Test-Path $_) {
Remove-Item -Path $_ -Recurse -Force
}
}
}
Function Step_DotnetClean {
LogStep "dotnet clean $dotnetSolutionFile --verbosity $DotnetVerbosity"
& dotnet clean "$dotnetSolutionFile" --verbosity $DotnetVerbosity
if (! $?) { Exit 1 }
}
Function Step_DotnetRestore {
LogStep "dotnet restore $dotnetSolutionFile --verbosity $DotnetVerbosity"
& dotnet restore "$dotnetSolutionFile" --verbosity $DotnetVerbosity
if (! $?) { Exit 1 }
}
Function Step_DotnetBuild {
LogStep "dotnet build $dotnetSolutionFile --no-restore --configuration $Configuration --verbosity $DotnetVerbosity /p:Version=$Version"
& dotnet build "$dotnetSolutionFile" --no-restore --configuration $Configuration --verbosity $DotnetVerbosity /p:Version="$Version"
if (! $?) { Exit 1 }
}
Function Step_DotnetTest {
Param([ValidateNotNullOrEmpty()] [string]$ProjectFile)
LogStep "dotnet test $ProjectFile --no-build --configuration $Configuration"
& dotnet test "$ProjectFile" --no-build --configuration $Configuration
if (! $?) { Exit 1 }
}
#######################################################################
# TARGETS
Function Target_Clean {
$targetDotnetClean = "Dotnet.Clean"
if (IsTargetCompleted $targetDotnetClean) { return }
LogTarget $targetDotnetClean
Step_DotnetClean
MarkTargetAsCompleted $targetDotnetClean
}
Function Target_Restore {
$targetDotnetRestore = "Dotnet.Restore"
if (IsTargetCompleted $targetDotnetRestore) { return }
LogTarget $targetDotnetRestore
Step_DotnetRestore
MarkTargetAsCompleted $targetDotnetRestore
}
Function Target_Build {
$targetDotnetBuild = "Dotnet.Build"
if (IsTargetCompleted $targetDotnetBuild) { return }
DependsOn Target_Restore
LogTarget $targetDotnetBuild
Step_DotnetBuild
MarkTargetAsCompleted $targetDotnetBuild
}
Function Target_TestUnit {
$targetDotnetTestUnit = "DotNet.TestUnit"
if (IsTargetCompleted $targetDotnetTestUnit) { return }
DependsOn Target_Build
LogTarget $targetDotnetTestUnit
$projects = Get-ChildItem -Path $srcDir -Filter "*Test.Unit.csproj" -Recurse -File
Foreach ($projectFile in $projects) {
Step_DotnetTest $projectFile
}
MarkTargetAsCompleted $targetDotnetTestUnit
}
Function Target_TestAcceptance {
$targetDotnetTestAcceptance = "DotNet.TestAcceptance"
if (IsTargetCompleted $targetDotnetTestAcceptance) { return }
DependsOn Target_Build
LogTarget $targetDotnetTestAcceptance
$projects = Get-ChildItem -Path $srcDir -Filter "*Test.Acceptance.csproj" -Recurse -File
Foreach ($projectFile in $projects) {
Step_DotnetTest $projectFile
}
MarkTargetAsCompleted $targetDotnetTestAcceptance
}
Function Target_FullBuild {
$targetFullBuild = "FullBuild"
if (IsTargetCompleted $targetFullBuild) { return }
DependsOn Target_Build
DependsOn Target_TestUnit
DependsOn Target_TestAcceptance
LogTarget $targetFullBuild
MarkTargetAsCompleted $targetFullBuild
}
Function Target_Default {
DependsOn Target_FullBuild
LogInfo "DONE"
}
#######################################################################
# PRUNE TARGETS
if ($Target -eq "Prune") {
Step_PruneBuild
Exit 0
}
#######################################################################
# PRELUDE
PreludeStep_ValidateDotNetCli
#######################################################################
# MAIN ENTRY POINT
LogInfo "*** BUILD: $Target ($Configuration) in $repositoryDir"
Invoke-Expression "Target_$Target"