-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpsakeFile.ps1
More file actions
382 lines (347 loc) · 15.5 KB
/
psakeFile.ps1
File metadata and controls
382 lines (347 loc) · 15.5 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# spell-checker:ignore Reqs
# Load in build settings
Remove-Variable -Name PSBPreference -Scope Script -Force -ErrorAction Ignore
Set-Variable -Name PSBPreference -Option ReadOnly -Scope Script -Value (. ([IO.Path]::Combine($PSScriptRoot, 'build.properties.ps1')))
Properties {
$importLocalizedDataSplat = @{
BindingVariable = 'LocalizedData'
FileName = 'Messages.psd1'
ErrorAction = 'SilentlyContinue'
}
Import-LocalizedData @importLocalizedDataSplat
}
FormatTaskName {
param($taskName)
Write-Host 'Task: ' -ForegroundColor Cyan -NoNewline
Write-Host $taskName.ToUpper() -ForegroundColor Blue
}
#region Task Dependencies
if ($null -eq $PSBCleanDependency) {
$PSBCleanDependency = @('Init')
}
if ($null -eq $PSBStageFilesDependency) {
$PSBStageFilesDependency = @('Clean')
}
if ($null -eq $PSBBuildDependency) {
$PSBBuildDependency = @('StageFiles', 'BuildHelp')
}
if ($null -eq $PSBAnalyzeDependency) {
$PSBAnalyzeDependency = @('Build')
}
if ($null -eq $PSBPesterDependency) {
$PSBPesterDependency = @('Build')
}
if ($null -eq $PSBTestDependency) {
$PSBTestDependency = @('Pester', 'Analyze')
}
if ($null -eq $PSBBuildHelpDependency) {
$PSBBuildHelpDependency = @('GenerateMarkdown', 'GenerateMAML')
}
if ($null -eq $PSBGenerateMarkdownDependency) {
$PSBGenerateMarkdownDependency = @('StageFiles')
}
if ($null -eq $PSBGenerateMAMLDependency) {
$PSBGenerateMAMLDependency = @('GenerateMarkdown')
}
if ($null -eq $PSBGenerateUpdatableHelpDependency) {
$PSBGenerateUpdatableHelpDependency = @('BuildHelp')
}
if ($null -eq $PSBPublishDependency) {
$PSBPublishDependency = @('Test')
}
if ($null -eq $PSBSignModuleDependency) {
$PSBSignModuleDependency = @('Build')
}
if ($null -eq $PSBBuildCatalogDependency) {
$PSBBuildCatalogDependency = @('SignModule')
}
if ($null -eq $PSBSignCatalogDependency) {
$PSBSignCatalogDependency = @('BuildCatalog')
}
if ($null -eq $PSBSignDependency) {
$PSBSignDependency = @('SignCatalog')
}
#endregion Task Dependencies
# This psake file is meant to be referenced from another
# Can't have two 'default' tasks
# Task default -depends Test
Task Init {
Initialize-PSBuild -UseBuildHelpers -BuildEnvironment $PSBPreference -Verbose:($VerbosePreference -eq 'Continue')
} -Description 'Initialize build environment variables'
Task Clean -Depends $PSBCleanDependency {
Clear-PSBuildOutputFolder -Path $PSBPreference.Build.ModuleOutDir -Verbose:($VerbosePreference -eq 'Continue')
} -Description 'Clears module output directory'
Task StageFiles -Depends $PSBStageFilesDependency {
$buildParams = @{
Path = $PSBPreference.General.SrcRootDir
ModuleName = $PSBPreference.General.ModuleName
DestinationPath = $PSBPreference.Build.ModuleOutDir
Exclude = $PSBPreference.Build.Exclude
Compile = $PSBPreference.Build.CompileModule
CompileDirectories = $PSBPreference.Build.CompileDirectories
CopyDirectories = $PSBPreference.Build.CopyDirectories
Culture = $PSBPreference.Help.DefaultLocale
}
if ($PSBPreference.Help.ConvertReadMeToAboutHelp) {
$readMePath = Get-ChildItem -Path $PSBPreference.General.ProjectRoot -Include 'readme.md', 'readme.markdown', 'readme.txt' -Depth 1 |
Select-Object -First 1
if ($readMePath) {
$buildParams.ReadMePath = $readMePath
}
}
# only add these configuration values to the build parameters if they have been been set
'CompileHeader', 'CompileFooter', 'CompileScriptHeader', 'CompileScriptFooter' | ForEach-Object {
if ($PSBPreference.Build.Keys -contains $_) {
$buildParams.$_ = $PSBPreference.Build.$_
}
}
Build-PSBuildModule @buildParams -Verbose:($VerbosePreference -eq 'Continue')
} -Description 'Builds module based on source directory'
Task Build -Depends $PSBBuildDependency -Description 'Builds module and generate help documentation'
$analyzePreReqs = {
$result = $true
if (-not $PSBPreference.Test.ScriptAnalysis.Enabled) {
Write-Warning 'Script analysis is not enabled.'
$result = $false
}
if (-not (Get-Module -Name PSScriptAnalyzer -ListAvailable)) {
Write-Warning 'PSScriptAnalyzer module is not installed'
$result = $false
}
$result
}
Task Analyze -Depends $PSBAnalyzeDependency -PreCondition $analyzePreReqs {
$analyzeParams = @{
Path = $PSBPreference.Build.ModuleOutDir
SeverityThreshold = $PSBPreference.Test.ScriptAnalysis.FailBuildOnSeverityLevel
SettingsPath = $PSBPreference.Test.ScriptAnalysis.SettingsPath
}
Test-PSBuildScriptAnalysis @analyzeParams -Verbose:($VerbosePreference -eq 'Continue')
} -Description 'Execute PSScriptAnalyzer tests'
$pesterPreReqs = {
$result = $true
if (-not $PSBPreference.Test.Enabled) {
Write-Warning 'Pester testing is not enabled.'
$result = $false
}
if (-not (Get-Module -Name Pester -ListAvailable)) {
Write-Warning 'Pester module is not installed'
$result = $false
}
if (-not (Test-Path -Path $PSBPreference.Test.RootDir)) {
Write-Warning "Test directory [$($PSBPreference.Test.RootDir)] not found"
$result = $false
}
return $result
}
Task Pester -Depends $PSBPesterDependency -PreCondition $pesterPreReqs {
$pesterParams = @{
Path = $PSBPreference.Test.RootDir
ModuleName = $PSBPreference.General.ModuleName
ModuleManifest = Join-Path $PSBPreference.Build.ModuleOutDir "$($PSBPreference.General.ModuleName).psd1"
OutputPath = $PSBPreference.Test.OutputFile
OutputFormat = $PSBPreference.Test.OutputFormat
CodeCoverage = $PSBPreference.Test.CodeCoverage.Enabled
CodeCoverageThreshold = $PSBPreference.Test.CodeCoverage.Threshold
CodeCoverageFiles = $PSBPreference.Test.CodeCoverage.Files
CodeCoverageOutputFile = $PSBPreference.Test.CodeCoverage.OutputFile
CodeCoverageOutputFileFormat = $PSBPreference.Test.CodeCoverage.OutputFileFormat
ImportModule = $PSBPreference.Test.ImportModule
SkipRemainingOnFailure = $PSBPreference.Test.SkipRemainingOnFailure
OutputVerbosity = $PSBPreference.Test.OutputVerbosity
Verbose = $VerbosePreference -eq 'Continue'
}
Test-PSBuildPester @pesterParams
} -Description 'Execute Pester tests'
Task Test -Depends $PSBTestDependency {
} -Description 'Execute Pester and ScriptAnalyzer tests'
Task BuildHelp -Depends $PSBBuildHelpDependency {} -Description 'Builds help documentation'
$genMarkdownPreReqs = {
$result = $true
if (-not (Get-Module platyPS -ListAvailable)) {
Write-Warning "platyPS module is not installed. Skipping [$($psake.context.currentTaskName)] task."
$result = $false
}
$result
}
Task GenerateMarkdown -Depends $PSBGenerateMarkdownDependency -PreCondition $genMarkdownPreReqs {
$buildMDParams = @{
ModulePath = $PSBPreference.Build.ModuleOutDir
ModuleName = $PSBPreference.General.ModuleName
DocsPath = $PSBPreference.Docs.RootDir
Locale = $PSBPreference.Help.DefaultLocale
Overwrite = $PSBPreference.Docs.Overwrite
AlphabeticParamsOrder = $PSBPreference.Docs.AlphabeticParamsOrder
ExcludeDontShow = $PSBPreference.Docs.ExcludeDontShow
UseFullTypeName = $PSBPreference.Docs.UseFullTypeName
Verbose = $VerbosePreference -eq 'Continue'
}
Build-PSBuildMarkdown @buildMDParams
} -Description 'Generates PlatyPS markdown files from module help'
$genHelpFilesPreReqs = {
$result = $true
if (-not (Get-Module platyPS -ListAvailable)) {
Write-Warning "platyPS module is not installed. Skipping [$($psake.context.currentTaskName)] task."
$result = $false
}
$result
}
Task GenerateMAML -Depends $PSBGenerateMAMLDependency -PreCondition $genHelpFilesPreReqs {
Build-PSBuildMAMLHelp -Path $PSBPreference.Docs.RootDir -DestinationPath $PSBPreference.Build.ModuleOutDir -Verbose:($VerbosePreference -eq 'Continue')
} -Description 'Generates MAML-based help from PlatyPS markdown files'
$genUpdatableHelpPreReqs = {
$result = $true
if (-not (Get-Module platyPS -ListAvailable)) {
Write-Warning "platyPS module is not installed. Skipping [$($psake.context.currentTaskName)] task."
$result = $false
}
$result
}
Task GenerateUpdatableHelp -Depends $PSBGenerateUpdatableHelpDependency -PreCondition $genUpdatableHelpPreReqs {
Build-PSBuildUpdatableHelp -DocsPath $PSBPreference.Docs.RootDir -OutputPath $PSBPreference.Help.UpdatableHelpOutDir -Verbose:($VerbosePreference -eq 'Continue')
} -Description 'Create updatable help .cab file based on PlatyPS markdown help'
Task Publish -Depends $PSBPublishDependency {
Assert -ConditionToCheck ($PSBPreference.Publish.PSRepositoryApiKey -or $PSBPreference.Publish.PSRepositoryCredential) -FailureMessage "API key or credential not defined to authenticate with [$($PSBPreference.Publish.PSRepository)] with."
$publishParams = @{
Path = $PSBPreference.Build.ModuleOutDir
Version = $PSBPreference.General.ModuleVersion
Repository = $PSBPreference.Publish.PSRepository
Verbose = $VerbosePreference
}
if ($PSBPreference.Publish.PSRepositoryApiKey) {
$publishParams.ApiKey = $PSBPreference.Publish.PSRepositoryApiKey
}
if ($PSBPreference.Publish.PSRepositoryCredential) {
$publishParams.Credential = $PSBPreference.Publish.PSRepositoryCredential
}
Publish-PSBuildModule @publishParams
} -Description 'Publish module to the defined PowerShell repository'
$signModulePreReqs = {
$result = $true
if (-not $PSBPreference.Sign.Enabled) {
Write-Warning 'Module signing is not enabled.'
$result = $false
}
if (-not (Get-Command -Name 'Set-AuthenticodeSignature' -ErrorAction Ignore)) {
Write-Warning 'Set-AuthenticodeSignature is not available. Module signing requires Windows.'
$result = $false
}
$result
}
Task SignModule -Depends $PSBSignModuleDependency -PreCondition $signModulePreReqs {
$certParams = @{
CertificateSource = $PSBPreference.Sign.CertificateSource
CertStoreLocation = $PSBPreference.Sign.CertStoreLocation
CertificateEnvVar = $PSBPreference.Sign.CertificateEnvVar
CertificatePasswordEnvVar = $PSBPreference.Sign.CertificatePasswordEnvVar
SkipValidation = $PSBPreference.Sign.SkipCertificateValidation
Verbose = $VerbosePreference -eq 'Continue'
}
if ($PSBPreference.Sign.Thumbprint) {
$certParams.Thumbprint = $PSBPreference.Sign.Thumbprint
}
if ($PSBPreference.Sign.PfxFilePath) {
$certParams.PfxFilePath = $PSBPreference.Sign.PfxFilePath
}
if ($PSBPreference.Sign.PfxFilePassword) {
$certParams.PfxFilePassword = $PSBPreference.Sign.PfxFilePassword
}
$certificate = if ($PSBPreference.Sign.Certificate) {
$PSBPreference.Sign.Certificate
} else {
Get-PSBuildCertificate @certParams
}
Assert ($null -ne $certificate) $LocalizedData.NoCertificateFound
$signingParams = @{
Path = $PSBPreference.Build.ModuleOutDir
Certificate = $certificate
TimestampServer = $PSBPreference.Sign.TimestampServer
HashAlgorithm = $PSBPreference.Sign.HashAlgorithm
Include = $PSBPreference.Sign.FilesToSign
Verbose = $VerbosePreference -eq 'Continue'
}
Invoke-PSBuildModuleSigning @signingParams
} -Description 'Signs module files (*.psd1, *.psm1, *.ps1) with an Authenticode signature'
$buildCatalogPreReqs = {
$result = $true
if (-not ($PSBPreference.Sign.Enabled -and $PSBPreference.Sign.Catalog.Enabled)) {
Write-Warning 'Catalog generation is not enabled.'
$result = $false
}
if (-not (Get-Command -Name 'New-FileCatalog' -ErrorAction Ignore)) {
Write-Warning 'New-FileCatalog is not available. Catalog generation requires Windows.'
$result = $false
}
$result
}
Task BuildCatalog -Depends $PSBBuildCatalogDependency -PreCondition $buildCatalogPreReqs {
$catalogFileName = if ($PSBPreference.Sign.Catalog.FileName) {
$PSBPreference.Sign.Catalog.FileName
} else {
"$($PSBPreference.General.ModuleName).cat"
}
$catalogFilePath = Join-Path -Path $PSBPreference.Build.ModuleOutDir -ChildPath $catalogFileName
$catalogParams = @{
ModulePath = $PSBPreference.Build.ModuleOutDir
CatalogFilePath = $catalogFilePath
CatalogVersion = $PSBPreference.Sign.Catalog.Version
Verbose = $VerbosePreference -eq 'Continue'
}
New-PSBuildFileCatalog @catalogParams
} -Description 'Creates a Windows catalog (.cat) file for the built module'
$signCatalogPreReqs = {
$result = $true
if (-not ($PSBPreference.Sign.Enabled -and $PSBPreference.Sign.Catalog.Enabled)) {
Write-Warning 'Catalog signing is not enabled.'
$result = $false
}
if (-not (Get-Command -Name 'Set-AuthenticodeSignature' -ErrorAction Ignore)) {
Write-Warning 'Set-AuthenticodeSignature is not available. Catalog signing requires Windows.'
$result = $false
}
$result
}
Task SignCatalog -Depends $PSBSignCatalogDependency -PreCondition $signCatalogPreReqs {
$certParams = @{
CertificateSource = $PSBPreference.Sign.CertificateSource
CertStoreLocation = $PSBPreference.Sign.CertStoreLocation
CertificateEnvVar = $PSBPreference.Sign.CertificateEnvVar
CertificatePasswordEnvVar = $PSBPreference.Sign.CertificatePasswordEnvVar
SkipValidation = $PSBPreference.Sign.SkipCertificateValidation
Verbose = $VerbosePreference -eq 'Continue'
}
if ($PSBPreference.Sign.Thumbprint) {
$certParams.Thumbprint = $PSBPreference.Sign.Thumbprint
}
if ($PSBPreference.Sign.PfxFilePath) {
$certParams.PfxFilePath = $PSBPreference.Sign.PfxFilePath
}
if ($PSBPreference.Sign.PfxFilePassword) {
$certParams.PfxFilePassword = $PSBPreference.Sign.PfxFilePassword
}
$certificate = if ($PSBPreference.Sign.Certificate) {
$PSBPreference.Sign.Certificate
} else {
Get-PSBuildCertificate @certParams
}
Assert ($null -ne $certificate) $LocalizedData.NoCertificateFound
$catalogFileName = if ($PSBPreference.Sign.Catalog.FileName) {
$PSBPreference.Sign.Catalog.FileName
} else {
"$($PSBPreference.General.ModuleName).cat"
}
$signingParams = @{
Path = $PSBPreference.Build.ModuleOutDir
Certificate = $certificate
TimestampServer = $PSBPreference.Sign.TimestampServer
HashAlgorithm = $PSBPreference.Sign.HashAlgorithm
Include = @($catalogFileName)
Verbose = $VerbosePreference -eq 'Continue'
}
Invoke-PSBuildModuleSigning @signingParams
} -Description 'Signs the module catalog (.cat) file with an Authenticode signature'
Task Sign -Depends $PSBSignDependency {} -Description 'Signs module files and catalog (meta task)'
Task ? -Description 'Lists the available tasks' {
'Available tasks:'
$psake.context.Peek().Tasks.Keys | Sort-Object
}