forked from sqlwhale/PSRedgate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.build.ps1
199 lines (166 loc) · 6.65 KB
/
module.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
$script:ModuleName = 'PSRedgate'
$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', 'classes' )
$script:TestFile = "$PSScriptRoot\output\TestResults_PS$PSVersion`_$TimeStamp.xml"
Task Default Build, Pester, UpdateSource, Publish
Task Build CopyToOutput, BuildPSM1, BuildPSD1
Task Pester Build, ImportModule, UnitTests, FullTests
Task Clean {
$null = Remove-Item $Output -Recurse -ErrorAction Ignore
$null = New-Item -Type Directory -Path $Destination
}
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 Specification {
$TestResults = Invoke-Gherkin $PSScriptRoot\Spec -PassThru
if ($TestResults.FailedCount -gt 0)
{
Write-Error "[$($TestResults.FailedCount)] specification are incomplete"
}
}
Task CopyToOutput {
Write-Output " 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, '')}
}
Task BuildPSM1 -Inputs (Get-Item "$source\*\*.ps1") -Outputs $ModulePath {
[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\*.ps1" | Where Name -NotLike '*.Tests.ps1'
foreach ($file in $fileList)
{
$shortName = $file.fullname.replace($PSScriptRoot, '')
Write-Output " Importing [.$shortName]"
[void]$stringbuilder.AppendLine( "# .$shortName" )
[void]$stringbuilder.AppendLine( [System.IO.File]::ReadAllText($file.fullname) )
}
}
}
Write-Output " Creating module [$ModulePath]"
Set-Content -Path $ModulePath -Value $stringbuilder.ToString()
}
Task NextPSGalleryVersion -if (-Not ( Test-Path "$output\version.xml" ) ) -Before BuildPSD1 {
$galleryVersion = Get-NextPSGalleryVersion -Name $ModuleName
$galleryVersion | Export-Clixml -Path "$output\version.xml"
}
Task BuildPSD1 -inputs (Get-ChildItem $Source -Recurse -File) -Outputs $ManifestPath {
Write-Output " Update [$ManifestPath]"
Copy-Item "$source\$ModuleName.psd1" -Destination $ManifestPath
$functions = Get-ChildItem "$ModuleName\Public\*.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, $_}
}
}
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'
}
}
$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 ) )
{
Write-Output " Module [$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)
{
Write-Output " Unloading Module [$ModuleName] from previous import"
Remove-Module $ModuleName
}
Write-Output " Importing Module [$ModuleName] from [$ManifestPath]"
Import-Module $ManifestPath -Force
}
}
Task Publish {
# Gate deployment
if (
$ENV:BHBuildSystem -ne 'Unknown' -and
$ENV:BHBranchName -eq "master" -and
$ENV:BHCommitMessage -match '!deploy'
)
{
$Params = @{
Path = $BuildRoot
Force = $true
}
Invoke-PSDeploy @Verbose @Params
}
else
{
"Skipping deployment: To deploy, ensure that...`n" +
"`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" +
"`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" +
"`t* Your commit message includes !deploy (Current: $ENV:BHCommitMessage)"
}
}