-
Notifications
You must be signed in to change notification settings - Fork 6
/
MSSQL-CICD-Helper.build.ps1
271 lines (228 loc) · 9.29 KB
/
MSSQL-CICD-Helper.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
#requires -Modules InvokeBuild, PSDeploy, BuildHelpers, PSScriptAnalyzer, PlatyPS, Pester
$script:ModuleName = 'MSSQL-CICD-Helper'
$script:Source = Join-Path $BuildRoot $ModuleName
$script:Output = Join-Path $BuildRoot output
$script:Destination = Join-Path $Output $ModuleName
$script:ModulePath = "$Destination\$ModuleName.psm1"
$script:ManifestPath = "$Destination\$ModuleName.psd1"
$script:Imports = ( 'Private', 'Public' )
$script:TestFile = "$PSScriptRoot\output\TestResults_PS$PSVersion`_$TimeStamp.xml"
$script:HelpRoot = Join-Path $Output 'help'
function TaskX($Name, $Parameters) {task $Name @Parameters -Source $MyInvocation}
Task Default Clean, Build, Pester, UpdateSource
Task Build CopyToOutput, BuildPSM1, BuildPSD1
Task Pester Build, ImportModule, UnitTests, FullTests
Task LocalPester Build, ImportModule, UnitTests
Task Local Build, Pester, UpdateSource
Task Clean {
If (Test-Path $Output)
{
$null = Remove-Item $Output -Recurse -ErrorAction Ignore
}
$null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore
}
Task UnitTests {
$TestResults = Invoke-Pester -Path Tests\*unit* -PassThru -Tag Build -ExcludeTag Slow
if ($TestResults.FailedCount -gt 0)
{
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
}
}
Task FullTests {
$TestResults = Invoke-Pester -Path Tests -PassThru -OutputFormat NUnitXml -OutputFile $testFile -Tag Build
if ($TestResults.FailedCount -gt 0)
{
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
}
}
Task CopyToOutput {
" Create Directory [$Destination]"
$null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore
Get-ChildItem $source -File |
where name -NotMatch "$ModuleName\.ps[dm]1" |
Copy-Item -Destination $Destination -Force -PassThru |
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')}
Get-ChildItem $source -Directory |
where name -NotIn $imports |
Copy-Item -Destination $Destination -Recurse -Force -PassThru |
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')}
}
TaskX BuildPSM1 @{
Inputs = (Get-Item "$source\*\*.ps1")
Outputs = $ModulePath
Jobs = {
[System.Text.StringBuilder]$stringbuilder = [System.Text.StringBuilder]::new()
foreach ($folder in $imports )
{
[void]$stringbuilder.AppendLine( "Write-Verbose 'Importing from [$Source\$folder]'" )
if (Test-Path "$source\$folder")
{
$fileList = Get-ChildItem $source\$folder\ -Recurse -include *.ps1 | Where Name -NotLike '*.Tests.ps1'
foreach ($file in $fileList)
{
$shortName = $file.fullname.replace($PSScriptRoot, '')
" Importing [.$shortName]"
[void]$stringbuilder.AppendLine( "# .$shortName" )
[void]$stringbuilder.AppendLine( [System.IO.File]::ReadAllText($file.fullname) )
}
}
}
" Creating module [$ModulePath]"
Set-Content -Path $ModulePath -Value $stringbuilder.ToString()
}
}
TaskX BuildPSD1 @{
Inputs = (Get-ChildItem $Source -Recurse -File)
Outputs = $ManifestPath
Jobs = {
Write-Output " Update [$ManifestPath]"
Copy-Item "$source\$ModuleName.psd1" -Destination $ManifestPath
$functions = Get-ChildItem $ModuleName\Public -Recurse -include *.ps1 | Where-Object { $_.name -notmatch 'Tests'} | Select-Object -ExpandProperty basename
Set-ModuleFunctions -Name $ManifestPath -FunctionsToExport $functions
Write-Output " Detecting semantic versioning"
Import-Module ".\$ModuleName"
$commandList = Get-Command -Module $ModuleName
Remove-Module $ModuleName
Write-Output " Calculating fingerprint"
$fingerprint = foreach ($command in $commandList )
{
foreach ($parameter in $command.parameters.keys)
{
'{0}:{1}' -f $command.name, $command.parameters[$parameter].Name
$command.parameters[$parameter].aliases | Foreach-Object { '{0}:{1}' -f $command.name, $_}
}
}
$fingerprint = $fingerprint | Sort-Object
if (Test-Path .\fingerprint)
{
$oldFingerprint = Get-Content .\fingerprint
}
$bumpVersionType = 'Patch'
' Detecting new features'
$fingerprint | Where {$_ -notin $oldFingerprint } | % {$bumpVersionType = 'Minor'; " $_"}
' Detecting breaking changes'
$oldFingerprint | Where {$_ -notin $fingerprint } | % {$bumpVersionType = 'Major'; " $_"}
Set-Content -Path .\fingerprint -Value $fingerprint
# Bump the module version
$version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion')
if ( $version -lt ([version]'1.0.0') )
{
# Still in beta, don't bump major version
if ( $bumpVersionType -eq 'Major' )
{
$bumpVersionType = 'Minor'
}
else
{
$bumpVersionType = 'Patch'
}
}
if(-not(Test-Path "$output\version.xml")){
" PSXML not found. Determining version with Get-NextPSGalleryVersion and creating PSXML"
$galleryVersion = Get-NextPSGalleryVersion -Name $ModuleName
$galleryVersion | Export-Clixml -Path "$output\version.xml"
}else{
" PSXML found. Grabbing value from previous build."
$galleryVersion = Import-Clixml -Path "$output\version.xml"
}
if ( $version -lt $galleryVersion )
{
$version = $galleryVersion
}
Write-Output " Stepping [$bumpVersionType] version [$version]"
$version = [version] (Step-Version $version -Type $bumpVersionType)
Write-Output " Using version: $version"
Update-Metadata -Path $ManifestPath -PropertyName ModuleVersion -Value $version
}
}
Task UpdateSource {
Copy-Item $ManifestPath -Destination "$source\$ModuleName.psd1"
}
Task ImportModule {
if ( -Not ( Test-Path $ManifestPath ) )
{
" Modue [$ModuleName] is not built, cannot find [$ManifestPath]"
Write-Error "Could not find module manifest [$ManifestPath]. You may need to build the module first"
}
else
{
if (Get-Module $ModuleName)
{
" Unloading Module [$ModuleName] from previous import"
Remove-Module $ModuleName
}
" Importing Module [$ModuleName] from [$ManifestPath]"
Import-Module $ManifestPath -Force
}
}
TaskX CreateHelp @{
Partial = $true
Inputs = {Get-ChildItem "$ModuleName\Public\*.ps1"}
Outputs = {
process
{
Get-ChildItem $_ | % {'{0}\{1}.md' -f $HelpRoot, $_.basename}
}
}
Jobs = 'ImportModule', {
process
{
$null = New-Item -Path $HelpRoot -ItemType Directory -ErrorAction SilentlyContinue
$mdHelp = @{
#Module = $script:ModuleName
OutputFolder = $HelpRoot
AlphabeticParamsOrder = $true
Verbose = $true
Force = $true
Command = Get-Item $_ | % basename
}
New-MarkdownHelp @mdHelp | % fullname
}
}
}
TaskX PackageHelp @{
Inputs = {Get-ChildItem $HelpRoot -Recurse -File}
Outputs = "$Destination\en-us\$ModuleName-help.xml"
Jobs = 'CreateHelp', {
New-ExternalHelp -Path $HelpRoot -OutputPath "$Destination\en-us" -force | % fullname
}
}
task Install Uninstall, {
$version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion')
$path = $env:PSModulePath.Split(';').Where( {
$_ -like 'C:\Users\*'
}, 'First', 1)
if ($path -and (Test-Path -Path $path))
{
"Using [$path] as base path..."
$path = Join-Path -Path $path -ChildPath $ModuleName
$path = Join-Path -Path $path -ChildPath $version
"Creating directory at [$path]..."
New-Item -Path $path -ItemType 'Directory' -Force -ErrorAction 'Ignore'
"Copying items from [$Destination] to [$path]..."
Copy-Item -Path "$Destination\*" -Destination $path -Recurse -Force
}
}
task Uninstall {
'Unloading Modules...'
Get-Module -Name $ModuleName -ErrorAction 'Ignore' | Remove-Module
'Uninstalling Module packages...'
$modules = Get-Module $ModuleName -ErrorAction 'Ignore' -ListAvailable
foreach ($module in $modules)
{
Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -ErrorAction 'Ignore'
}
'Cleaning up manually installed Modules...'
$path = $env:PSModulePath.Split(';').Where( {
$_ -like 'C:\Users\*'
}, 'First', 1)
$path = Join-Path -Path $path -ChildPath $ModuleName
if ($path -and (Test-Path -Path $path))
{
'Removing files... (This may fail if any DLLs are in use.)'
Get-ChildItem -Path $path -File -Recurse |
Remove-Item -Force | ForEach-Object 'FullName'
'Removing folders... (This may fail if any DLLs are in use.)'
Remove-Item $path -Recurse -Force
}
}