This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-helpers.ps1
211 lines (171 loc) · 6.79 KB
/
build-helpers.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
function validate-target([Parameter(ValueFromRemainingArguments)]$expectedTargets) {
if ( ($expectedTargets -eq $null) -or (!$expectedTargets.Contains($target)) ) {
if ($global:helpBlock) {
&$global:helpBlock
}
throw "Invalid Target: $target`r`nValid Targets: $expectedTargets"
}
}
function write-help($example, $description) {
write-host
write-host "$example" -foregroundcolor GREEN
write-host " $description"
}
function project-properties($majorMinorVersion, $buildNumber) {
$versionPrefix = "$majorMinorVersion.$buildNumber"
$versionSuffix = if ($buildNumber -eq "") { "dev" } else { "" }
$copyright = $(get-copyright)
if ($versionSuffix) {
write-host "$product $versionPrefix-$versionSuffix"
} else {
write-host "$product $versionPrefix"
}
write-host $copyright
regenerate-file (resolve-path "Directory.build.props") @"
<Project>
<PropertyGroup>
<Product>$product</Product>
<VersionPrefix>$versionPrefix</VersionPrefix>
<VersionSuffix>$versionSuffix</VersionSuffix>
<Copyright>$copyright</Copyright>
<LangVersion>latest</LangVersion>
</PropertyGroup>
</Project>
"@
}
function get-copyright {
$date = Get-Date
$year = $date.Year
$copyrightSpan = if ($year -eq $yearInitiated) { $year } else { "$yearInitiated-$year" }
return "© $copyrightSpan $owner"
}
function regenerate-file($path, $newContent) {
$oldContent = [IO.File]::ReadAllText($path)
if ($newContent -ne $oldContent) {
write-host "Generating $path"
[System.IO.File]::WriteAllText($path, $newContent, [System.Text.Encoding]::UTF8)
}
}
function delete-directory($path) {
if (test-path $path) {
write-host "Deleting $path"
rd $path -recurse -force -ErrorAction SilentlyContinue | out-null
}
}
function find-dependency($name) {
$exes = @(gci packages -rec -filter $name)
if ($exes.Length -ne 1) {
throw "Expected to find 1 $name, but found $($exes.Length)."
}
return $exes[0].FullName
}
function connection-string($environmentVariablePrefix, $appsettingsPath) {
$environmentVariableName = "$($environmentVariablePrefix):ConnectionStrings:Database"
if (test-path env:$environmentVariableName) {
return (get-item env:$environmentVariableName).Value
}
return (get-content $appsettingsPath | out-string | convertfrom-json).ConnectionStrings.Database
}
function update-database([Parameter(ValueFromRemainingArguments)]$environments) {
$migrationsProject = (get-item -path .).Name
$roundhouseExePath = find-dependency rh.exe
$roundhouseOutputDir = [System.IO.Path]::GetDirectoryName($roundhouseExePath) + "\output"
$migrationScriptsPath ="Scripts"
$roundhouseVersionFile = "bin\$configuration\$targetFramework\$migrationsProject.dll"
foreach ($environment in $environments) {
$connectionString = $connectionStrings[$environment]
write-host "Executing RoundhousE for environment:" $environment
execute { & $roundhouseExePath --connectionstring $connectionString `
--commandtimeout 300 `
--env $environment `
--output $roundhouseOutputDir `
--sqlfilesdirectory $migrationScriptsPath `
--versionfile $roundhouseVersionFile `
--transaction `
--silent }
}
}
function rebuild-database([Parameter(ValueFromRemainingArguments)]$environments) {
$migrationsProject = (get-item -path .).Name
$roundhouseExePath = find-dependency rh.exe
$roundhouseOutputDir = [System.IO.Path]::GetDirectoryName($roundhouseExePath) + "\output"
$migrationScriptsPath ="Scripts"
$roundhouseVersionFile = "bin\$configuration\$targetFramework\$migrationsProject.dll"
foreach ($environment in $environments) {
$connectionString = $connectionStrings[$environment]
write-host "Executing RoundhousE for environment:" $environment
execute { & $roundhouseExePath --connectionstring $connectionString `
--commandtimeout 300 `
--env $environment `
--output $roundhouseOutputDir `
--silent `
--drop }
execute { & $roundhouseExePath --connectionstring $connectionString `
--commandtimeout 300 `
--env $environment `
--output $roundhouseOutputDir `
--sqlfilesdirectory $migrationScriptsPath `
--versionfile $roundhouseVersionFile `
--transaction `
--silent `
--simple }
}
}
function seed-database-for-examples($environment) {
$migrationsProject = (get-item -path .).Name
$roundhouseExePath = find-dependency rh.exe
$roundhouseOutputDir = [System.IO.Path]::GetDirectoryName($roundhouseExePath) + "\output"
$migrationScriptsPath = "..\..\examples\Messaging"
$roundhouseVersionFile = "bin\$configuration\$targetFramework\$migrationsProject.dll"
$connectionString = $connectionStrings[$environment]
execute { & $roundhouseExePath --connectionstring $connectionString `
--commandtimeout 300 `
--env $environment `
--output $roundhouseOutputDir `
--sqlfilesdirectory $migrationScriptsPath `
--versionfile $roundhouseVersionFile `
--transaction `
--silent }
}
function task($heading, $command, $path) {
write-host
write-host $heading -fore CYAN
execute $command $path
}
function execute($command, $path) {
if ($path -eq $null) {
$global:lastexitcode = 0
& $command
} else {
Push-Location $path
$global:lastexitcode = 0
& $command
Pop-Location
}
if ($lastexitcode -ne 0) {
throw "Error executing command:$command"
}
}
function help($helpBlock) {
$global:helpBlock = $helpBlock
}
function main($mainBlock) {
if ($target -eq "help") {
if ($global:helpBlock) {
&$global:helpBlock
return
}
}
try {
&$mainBlock
write-host
write-host "Build Succeeded" -fore GREEN
exit 0
} catch [Exception] {
write-host
write-host $_.Exception.Message -fore DARKRED
write-host
write-host "Build Failed" -fore DARKRED
exit 1
}
}