-
Notifications
You must be signed in to change notification settings - Fork 6
/
module.build.ps1
325 lines (277 loc) · 10.3 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
$script:ModuleName = $ENV:BHProjectName
$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"
$global:SUTPath = $script:ManifestPath
Task Init SetAsLocal, InstallSUT
Task Default Build, Pester, Publish
Task Build InstallSUT, CopyToOutput, BuildPSM1, BuildPSD1
Task Pester Build, UnitTests, FullTests
function CalculateFingerprint {
param(
[Parameter(ValueFromPipeline)]
[System.Management.Automation.FunctionInfo[]] $CommandList
)
process {
$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
}
}
function PublishTestResults
{
param(
[string]$Path
)
if ($ENV:BHBuildSystem -eq 'Unknown')
{
return
}
Write-Output "Publishing test result file"
switch ($ENV:BHBuildSystem)
{
'AppVeyor'
{
(New-Object 'System.Net.WebClient').UploadFile(
"https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)",
$Path )
}
'VSTS'
{
# Skip; publish logic defined as task in vsts build config (see .vsts-ci.yml)
}
Default
{
Write-Warning "Publish test result not implemented for build system '$($ENV:BHBuildSystem)'"
}
}
}
function Read-Module {
param (
[Parameter(Mandatory)]
[string] $Name,
[Parameter(Mandatory)]
[string] $Repository,
[Parameter(Mandatory)]
[string] $Path)
$reader = {
param (
[string] $Name,
[string] $Repository,
[string] $Path)
try {
# we need to ensure $Path is one of the locations that PS will look when resolving
# dependencies of the module it is being asked to import
$originalPath = Get-Item -Path Env:\PSModulePath | Select -Exp Value
$psModulePaths = $originalPath -split ';' | Where {$_ -ne $Path}
$revisedPath = ( @($Path) + @($psModulePaths) | Select -Unique ) -join ';'
Set-Item -Path Env:\PSModulePath -Value $revisedPath -EA Stop
try {
Save-Module -Name $Name -Path $Path -Repository $Repository -EA Stop
Import-Module "$Path\$Name" -PassThru -EA Stop
}
finally {
Set-Item -Path Env:\PSModulePath -Value $originalPath -EA Stop
}
}
catch {
if ($_ -match "No match was found for the specified search criteria") {
@()
}
else {
$_
}
}
}
$params = @{
Name = $Name
Repository = $Repository
Path = $Path
}
# Create a runspace and run our $reader script to return the module requested
# The purpose of using a runspace is to avoid loading old/duplicate versions of modules
# into the current PS session and thus avoid any potential conflicts
$PowerShell = [Powershell]::Create()
[void]$PowerShell.AddScript($reader).AddParameters($params)
# return module
$PowerShell.Invoke()
}
Task InstallSUT {
Invoke-PSDepend -Path "$PSScriptRoot\test.depend.psd1" -Install -Force
}
Task SetAsLocal {
# ensure source code rather than compiled code in the output directory is being debugged / tested
$global:SUTPath = $env:BHPSModuleManifest
}
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
PublishTestResults $testFile
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 PublishedModuleInfo -if (-Not ( Test-Path "$output\previous-module-info.xml" ) ) -Before BuildPSD1 {
$downloadPath = "$output\previous-vs"
if (-not(Test-Path $downloadPath)) {
New-Item $downloadPath -ItemType Directory | Out-Null
}
$previousModule = Read-Module -Name $ModuleName -Repository ($env:PublishRepository) -Path $downloadPath
if ($null -ne $previousModule -and $previousModule.GetType() -eq [System.Management.Automation.ErrorRecord])
{
Write-Error $previousModule
return
}
$moduleInfo = if ($null -eq $previousModule)
{
[PsCustomObject] @{
Version = [System.Version]::new(0, 0, 1)
Fingerprint = @()
}
}
else
{
[PsCustomObject] @{
Version = $previousModule.Version
Fingerprint = $previousModule.ExportedFunctions.Values | CalculateFingerprint
}
}
$moduleInfo | Export-Clixml -Path "$output\previous-module-info.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
Set-ModuleAliases -Name $ManifestPath
$previousModuleInfo = Import-Clixml -Path "$output\previous-module-info.xml"
Write-Output " Detecting semantic versioning"
# avoid error trying to load a module twice
Unload-SUT
$commandList = (Import-Module ".\$ModuleName" -PassThru).ExportedFunctions.Values
# cleanup PS session
Unload-SUT
Write-Output " Calculating fingerprint"
$fingerprint = $commandList | CalculateFingerprint
$oldFingerprint = $previousModuleInfo.Fingerprint
$bumpVersionType = 'Patch'
' Detecting new features'
$fingerprint | Where {$_ -notin $oldFingerprint } | % {$bumpVersionType = 'Minor'; " $_"}
' Detecting breaking changes'
$oldFingerprint | Where {$_ -notin $fingerprint } | % {$bumpVersionType = 'Major'; " $_"}
# 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'
}
}
$publishedVersion = $previousModuleInfo.Version
if ( $version -lt $publishedVersion )
{
$version = $publishedVersion
}
if ($version -eq $publishedVersion)
{
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
}
else
{
Write-Output " Using version from $ModuleName.psd1: $version"
}
}
Task UpdateSource {
Copy-Item $ManifestPath -Destination "$source\$ModuleName.psd1"
}
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)"
}
}