-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
219 lines (181 loc) · 6.51 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
. .\BuildFunctions.ps1
. .\FlywayFunctions.ps1
$projectName = "ChurchBulletin"
$base_dir = resolve-path .\
$source_dir = "$base_dir\src"
$unitTestProjectPath = "$source_dir\UnitTests"
$integrationTestProjectPath = "$source_dir\IntegrationTests"
$acceptanceTestProjectPath = "$source_dir\AcceptanceTests"
$uiProjectPath = "$source_dir\UI\Server"
$databaseProjectPath = "$source_dir\Database"
$databaseFlywayProjectPath = "$source_dir\DatabaseFlyway"
$mauiProjectPath = "$source_dir\UI\Maui"
$projectConfig = $env:BuildConfiguration
$framework = "net8.0"
$version = $env:BUILD_BUILDNUMBER
$verbosity = "minimal"
$build_dir = "$base_dir\build"
$test_dir = "$build_dir\test"
$aliaSql = "$source_dir\Database\scripts\AliaSql.exe"
$flywayCliDir = "$base_dir\flyway"
$flywayCli = "$flywayCliDir\flyway.cmd"
$databaseAction = $env:DatabaseAction
if ([string]::IsNullOrEmpty($databaseAction)) { $databaseAction = "Rebuild"}
#We will create 3x databases for Flyway
$databaseName = $projectName
if ([string]::IsNullOrEmpty($databaseName)) { $databaseName = $projectName}
$devDatabaseName = $databaseName + "_Dev"
$shadowDatabaseName = $databaseName + "_Shadow"
#We may need three separate servers, but for initialization and/or testing, just use the same, root SQL Server for the 3x test Flyway dbs.
$script:databaseServer = $databaseServer
if ([string]::IsNullOrEmpty($script:databaseServer)) { $script:databaseServer = "(LocalDb)\MSSQLLocalDB"}
$script:devDatabaseServer = $databaseServer
$script:shadowDatabaseServer = $databaseServer
$databaseScripts = "$source_dir\Database\scripts"
if ([string]::IsNullOrEmpty($version)) { $version = "1.0.0"}
if ([string]::IsNullOrEmpty($projectConfig)) {$projectConfig = "Release"}
Function Init {
& cmd.exe /c rd /S /Q build
mkdir $build_dir > $null
exec {
& dotnet clean $source_dir\$projectName.sln -nologo -v $verbosity
}
exec {
& dotnet restore $source_dir\$projectName.sln -nologo --interactive -v $verbosity
}
Setup-FlywayCLI -flywayCliDir $flywayCliDir # Optionally, you can provide a secondary parameter for a different
# download/version of the Flyway CLI. It will default to the latest
# version as of Sept-18-2024
Write-Output $projectConfig
Write-Output $version
}
Function Compile{
exec {
& dotnet build $source_dir\$projectName.sln -nologo --no-restore -v `
$verbosity -maxcpucount --configuration $projectConfig --no-incremental `
/p:TreatWarningsAsErrors="true" `
/p:Version=$version /p:Authors="Programming with Palermo" `
/p:Product="Church Bulletin"
}
}
Function UnitTests{
Push-Location -Path $unitTestProjectPath
try {
exec {
& dotnet test /p:CollectCoverage=true -nologo -v $verbosity --logger:trx `
--results-directory $test_dir\UnitTests --no-build `
--no-restore --configuration $projectConfig `
--collect:"XPlat Code Coverage"
}
}
finally {
Pop-Location
}
}
Function IntegrationTest{
Push-Location -Path $integrationTestProjectPath
try {
exec {
& dotnet test /p:CollectCoverage=true -nologo -v $verbosity --logger:trx `
--results-directory $test_dir\IntegrationTests --no-build `
--no-restore --configuration $projectConfig `
--collect:"XPlat Code Coverage"
}
}
finally {
Pop-Location
}
}
Function MigrateDatabaseLocal {
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$databaseServerFunc,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$databaseNameFunc
)
exec{
& $aliaSql $databaseAction $databaseServerFunc $databaseNameFunc $databaseScripts
}
if ($migrateDbWithFlyway) {
#call 'flyaway migrate' with db parameters
$migrationsPath = "$databaseFlywayProjectPath\migrations"
$flywayParameters = @(
"-configFiles=$databaseFlywayProjectPath\flyway.toml"
"-url=jdbc:sqlserver://$databaseServerFunc;databaseName=$databaseNameFunc;encrypt=false;integratedSecurity=true;trustServerCertificate=true"
"-locations=filesystem:$migrationsPath"
"migrate"
)
exec {
& $flywayCli $flywayParameters
}
}
}
Function PackageUI {
exec{
& dotnet publish $uiProjectPath -nologo --no-restore --no-build -v $verbosity --configuration $projectConfig
}
exec{
& dotnet-octo pack --id "$projectName.UI" --version $version --basePath $uiProjectPath\bin\$projectConfig\$framework\publish --outFolder $build_dir --overwrite
}
}
Function PackageDatabase {
exec{
& dotnet-octo pack --id "$projectName.Database" --version $version --basePath $databaseProjectPath --outFolder $build_dir --overwrite
}
}
Function PackageAcceptanceTests {
# Use Debug configuration so full symbols are available to display better error messages in test failures
exec{
& dotnet publish $acceptanceTestProjectPath -nologo --no-restore -v $verbosity --configuration Debug
}
exec{
& dotnet-octo pack --id "$projectName.AcceptanceTests" --version $version --basePath $acceptanceTestProjectPath\bin\Debug\$framework\publish --outFolder $build_dir --overwrite
}
}
Function PackageScript {
exec{
& dotnet publish $uiProjectPath -nologo --no-restore --no-build -v $verbosity --configuration $projectConfig
}
exec{
& dotnet-octo pack --id "$projectName.Script" --version $version --basePath $uiProjectPath --include "*.ps1" --outFolder $build_dir --overwrite
}
}
Function Package{
Write-Output "Packaging nuget packages"
dotnet tool install --global Octopus.DotNet.Cli | Write-Output $_ -ErrorAction SilentlyContinue #prevents red color is already installed
PackageUI
PackageDatabase
PackageAcceptanceTests
PackageScript
}
Function PrivateBuild{
$projectConfig = "Debug"
[Environment]::SetEnvironmentVariable("containerAppURL", "localhost:7174", "User")
$sw = [Diagnostics.Stopwatch]::StartNew()
Init
Compile
UnitTests
#We need 3 databases for
MigrateDatabaseLocal -databaseServerFunc $databaseServer -databaseNameFunc $databaseName
#These are extra Dbs that are can be part of a Flyway setup
if ($migrateDbWithFlyway) {
MigrateDatabaseLocal -databaseServerFunc $devDatabaseServer -databaseNameFunc $devDatabaseName
MigrateDatabaseLocal -databaseServerFunc $shadowDatabaseServer -databaseNameFunc $shadowDatabaseName
}
IntegrationTest
$sw.Stop()
write-host "BUILD SUCCEEDED - Build time: " $sw.Elapsed.ToString() -ForegroundColor Green
}
Function CIBuild{
$sw = [Diagnostics.Stopwatch]::StartNew()
Init
Compile
UnitTests
MigrateDatabaseLocal $databaseName
IntegrationTest
Package
$sw.Stop()
write-host "BUILD SUCCEEDED - Build time: " $sw.Elapsed.ToString() -ForegroundColor Green
}