-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.psake.ps1
332 lines (277 loc) · 11.4 KB
/
build.psake.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
326
327
328
329
330
331
332
#Requires -Modules psake
##############################################################################
# DO NOT MODIFY THIS FILE! Modify build.settings.ps1 instead.
##############################################################################
##############################################################################
# This is the PowerShell Module psake build script. It defines the following tasks:
#
# Clean, Build, Sign, BuildHelp, Install, Test and Publish.
#
# The default task is Build. This task copies the appropriate files from the
# $SrcRootDir under the $OutDir. Later, other tasks such as Sign and BuildHelp
# will further modify the contents of $OutDir and add new files.
#
# The Sign task will only sign scripts if the $SignScripts variable is set to
# $true. A code-signing certificate is required for this task to complete.
#
# The BuildHelp task invokes platyPS to generate markdown files from
# comment-based help for your exported commands. platyPS then generates
# a help file for your module from the markdown files.
#
# The Install task simplies copies the module folder under $OutDir to your
# profile's Modules folder.
#
# The Test task invokes Pester on the $TestRootDir.
#
# The Publish task uses the Publish-Module command to publish
# to either the PowerShell Gallery (the default) or you can change
# the $PublishRepository property to the name of an alternate repository.
# Note: the Publish task requires that the Test task execute without failures.
#
# You can exeute a specific task, such as the Test task by running the
# following command:
#
# PS C:\> invoke-psake build.psake.ps1 -taskList Test
###############################################################################
# Dot source the user's customized properties and extension tasks.
###############################################################################
. $PSScriptRoot\build.settings.ps1
###############################################################################
# Private properties.
###############################################################################
Properties {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$ModuleOutDir = "$OutDir\$ModuleName"
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$UpdatableHelpOutDir = "$OutDir\UpdatableHelp"
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$SharedProperties = @{}
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$LineSep = "-" * 78
}
###############################################################################
# Core task implementations. Avoid modifying these tasks.
###############################################################################
Task default -depends Build
Task Init -requiredVariables OutDir {
if (!(Test-Path -LiteralPath $OutDir)) {
New-Item $OutDir -ItemType Directory -Verbose:$VerbosePreference > $null
}
else {
Write-Verbose "$($psake.context.currentTaskName) - directory already exists '$OutDir'."
}
}
Task Clean -depends Init -requiredVariables OutDir {
# Maybe a bit paranoid but this task nuked \ on my laptop. Good thing I was not running as admin.
if ($OutDir.Length -gt 3) {
Get-ChildItem $OutDir | Remove-Item -Recurse -Force -Verbose:$VerbosePreference
}
else {
Write-Verbose "$($psake.context.currentTaskName) - `$OutDir '$OutDir' must be longer than 3 characters."
}
}
Task StageFiles -depends Init, Clean, BeforeStageFiles, CoreStageFiles, AfterStageFiles {
}
Task CoreStageFiles -requiredVariables ModuleOutDir, SrcRootDir {
if (!(Test-Path -LiteralPath $ModuleOutDir)) {
New-Item $ModuleOutDir -ItemType Directory -Verbose:$VerbosePreference > $null
}
else {
Write-Verbose "$($psake.context.currentTaskName) - directory already exists '$ModuleOutDir'."
}
Copy-Item -Path $SrcRootDir\* -Destination $ModuleOutDir -Recurse -Exclude $Exclude -Verbose:$VerbosePreference
}
Task Build -depends Init, Clean, BeforeBuild, StageFiles, Analyze, AfterBuild {
}
Task Analyze -depends StageFiles `
-requiredVariables ModuleOutDir, ScriptAnalysisEnabled, ScriptAnalysisFailBuildOnSeverityLevel, ScriptAnalyzerSettingsPath {
if (!$ScriptAnalysisEnabled) {
"Script analysis is not enabled. Skipping $($psake.context.currentTaskName) task."
return
}
if (!(Get-Module PSScriptAnalyzer -ListAvailable)) {
"PSScriptAnalyzer module is not installed. Skipping $($psake.context.currentTaskName) task."
return
}
"ScriptAnalysisFailBuildOnSeverityLevel set to: $ScriptAnalysisFailBuildOnSeverityLevel"
$analysisResult = Invoke-ScriptAnalyzer -Path $ModuleOutDir -Settings $ScriptAnalyzerSettingsPath -Recurse -Verbose:$VerbosePreference
$analysisResult | Format-Table
switch ($ScriptAnalysisFailBuildOnSeverityLevel) {
'None' {
return
}
'Error' {
Assert -conditionToCheck (
($analysisResult | Where-Object Severity -eq 'Error').Count -eq 0
) -failureMessage 'One or more ScriptAnalyzer errors were found. Build cannot continue!'
}
'Warning' {
Assert -conditionToCheck (
($analysisResult | Where-Object {
$_.Severity -eq 'Warning' -or $_.Severity -eq 'Error'
}).Count -eq 0) -failureMessage 'One or more ScriptAnalyzer warnings were found. Build cannot continue!'
}
default {
Assert -conditionToCheck (
$analysisResult.Count -eq 0
) -failureMessage 'One or more ScriptAnalyzer issues were found. Build cannot continue!'
}
}
}
Task BuildHelp -depends Build, BeforeBuildHelp, GenerateMarkdown, GenerateHelpFiles, AfterBuildHelp {
}
Task GenerateMarkdown -requiredVariables DefaultLocale, DocsRootDir, ModuleName, ModuleOutDir {
if (!(Get-Module platyPS -ListAvailable)) {
"platyPS module is not installed. Skipping $($psake.context.currentTaskName) task."
return
}
$moduleInfo = Import-Module $ModuleOutDir\$ModuleName.psd1 -Global -Force -PassThru
try {
if ($moduleInfo.ExportedCommands.Count -eq 0) {
"No commands have been exported. Skipping $($psake.context.currentTaskName) task."
return
}
if (!(Test-Path -LiteralPath $DocsRootDir)) {
New-Item $DocsRootDir -ItemType Directory > $null
}
if (Get-ChildItem -LiteralPath $DocsRootDir -Filter *.md -Recurse) {
Get-ChildItem -LiteralPath $DocsRootDir -Directory | ForEach-Object {
Update-MarkdownHelp -Path $_.FullName -Verbose:$VerbosePreference > $null
}
}
# ErrorAction set to SilentlyContinue so this command will not overwrite an existing MD file.
New-MarkdownHelp -Module $ModuleName -Locale $DefaultLocale -OutputFolder $DocsRootDir\$DefaultLocale `
-WithModulePage -ErrorAction SilentlyContinue -Verbose:$VerbosePreference > $null
}
finally {
Remove-Module $ModuleName
}
}
Task GenerateHelpFiles -requiredVariables DocsRootDir, ModuleName, ModuleOutDir, OutDir {
if (!(Get-Module platyPS -ListAvailable)) {
"platyPS module is not installed. Skipping $($psake.context.currentTaskName) task."
return
}
if (!(Get-ChildItem -LiteralPath $DocsRootDir -Filter *.md -Recurse -ErrorAction SilentlyContinue)) {
"No markdown help files to process. Skipping $($psake.context.currentTaskName) task."
return
}
$helpLocales = (Get-ChildItem -Path $DocsRootDir -Directory).Name
# Generate the module's primary MAML help file.
foreach ($locale in $helpLocales) {
New-ExternalHelp -Path $DocsRootDir\$locale -OutputPath $ModuleOutDir\$locale -Force `
-ErrorAction SilentlyContinue -Verbose:$VerbosePreference > $null
}
}
Task BuildUpdatableHelp -depends BuildHelp, BeforeBuildUpdatableHelp, CoreBuildUpdatableHelp, AfterBuildUpdatableHelp {
}
###############################################################################
# Helper functions
###############################################################################
function PromptUserForCredentialAndStorePassword {
[Diagnostics.CodeAnalysis.SuppressMessage("PSProvideDefaultParameterValue", '')]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]
$DestinationPath,
[Parameter(Mandatory)]
[string]
$Message,
[Parameter(Mandatory, ParameterSetName = 'SaveSetting')]
[string]
$Key
)
$cred = Get-Credential -Message $Message -UserName "ignored"
if ($DestinationPath) {
SetSetting -Key $Key -Value $cred.Password -Path $DestinationPath
}
$cred
}
function AddSetting {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSShouldProcess', '', Scope = 'Function')]
param(
[Parameter(Mandatory)]
[string]$Key,
[Parameter(Mandatory)]
[string]$Path,
[Parameter(Mandatory)]
[ValidateNotNull()]
[object]$Value
)
switch ($type = $Value.GetType().Name) {
'securestring' { $setting = $Value | ConvertFrom-SecureString }
default { $setting = $Value }
}
if (Test-Path -LiteralPath $Path) {
$storedSettings = Import-Clixml -Path $Path
$storedSettings.Add($Key, @($type, $setting))
$storedSettings | Export-Clixml -Path $Path
}
else {
$parentDir = Split-Path -Path $Path -Parent
if (!(Test-Path -LiteralPath $parentDir)) {
New-Item $parentDir -ItemType Directory > $null
}
@{$Key = @($type, $setting)} | Export-Clixml -Path $Path
}
}
function GetSetting {
param(
[Parameter(Mandatory)]
[string]$Key,
[Parameter(Mandatory)]
[string]$Path
)
if (Test-Path -LiteralPath $Path) {
$securedSettings = Import-Clixml -Path $Path
if ($securedSettings.$Key) {
switch ($securedSettings.$Key[0]) {
'securestring' {
$value = $securedSettings.$Key[1] | ConvertTo-SecureString
$cred = New-Object -TypeName PSCredential -ArgumentList 'jpgr', $value
$cred.GetNetworkCredential().Password
}
default {
$securedSettings.$Key[1]
}
}
}
}
}
function SetSetting {
param(
[Parameter(Mandatory)]
[string]$Key,
[Parameter(Mandatory)]
[string]$Path,
[Parameter(Mandatory)]
[ValidateNotNull()]
[object]$Value
)
if (GetSetting -Key $Key -Path $Path) {
RemoveSetting -Key $Key -Path $Path
}
AddSetting -Key $Key -Value $Value -Path $Path
}
function RemoveSetting {
param(
[Parameter(Mandatory)]
[string]$Key,
[Parameter(Mandatory)]
[string]$Path
)
if (Test-Path -LiteralPath $Path) {
$storedSettings = Import-Clixml -Path $Path
$storedSettings.Remove($Key)
if ($storedSettings.Count -eq 0) {
Remove-Item -Path $Path
}
else {
$storedSettings | Export-Clixml -Path $Path
}
}
else {
Write-Warning "The build setting file '$Path' has not been created yet."
}
}