-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.psake.ps1
453 lines (383 loc) · 14.9 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
##############################################################################
# PSAKE SCRIPT FOR MODULE BUILD & PUBLISH TO THE INTERNAL PSPRIVATEGALLERY
##############################################################################
#
# Requirements: PSake. If you don't have this module installed use the following
# command to install it:
#
# PS C:\> Install-Module PSake -Scope CurrentUser
#
##############################################################################
# This is a PSake script that supports the following tasks:
# clean, build, test and publish. The default task is build.
#
# The publish task uses the Publish-Module command to publish
# to the internal PowerShell Gallery
#
# The test task invokes Pester to run any Pester tests in your
# workspace folder. Name your test scripts <TestName>.Tests.ps1
# and Pester will find and run the tests contained in the files.
#
# You can run this build script directly using the invoke-psake
# command which will execute the build task. This task "builds"
# a temporary folder from which the module can be published.
#
# PS C:\> invoke-psake build.ps1
#
# You can run your Pester tests (if any) by running the following command.
#
# PS C:\> invoke-psake build.ps1 -taskList test
#
# You can execute the publish task with the following command. Note that
# the publish task will run the test task first. The Pester tests must pass
# before the publish task will run.
#
# PS C:\> invoke-psake build.ps1 -taskList publish
#
# This command should only be run from a TFS Build server
###############################################################################
# Customize these properties for your module.
# PSake makes variables declared here available in other scriptblocks
###############################################################################
Properties {
Write-Host "Using PowerShell Version: $($PSversionTable.PSversion.ToString())"
$ManifestPath = (Get-Item $PSScriptRoot\*.psd1)[0]
# The name of your module should match the basename of the PSD1 file.
$ModuleName = $ManifestPath.BaseName
Write-Host "Building module: $ModuleName"
# Path to the release notes file.
# $ReleaseNotesPath = "$PSScriptRoot\CHANGELOG.md"
# The directory used to publish the module from. If you are using Git, the
# $PublishDir should be ignored if it is under the workspace directory.
$PublishDir = "$PSScriptRoot\Release\$ModuleName"
# The directory is used to publish the test results to.
$TestResultsDir = "$PSScriptRoot\TestResults"
# The following items will not be copied to the $PublishDir.
# Add items that should not be published with the module.
$Exclude = @(
'Release',
'TestResults',
'Tests',
'.git*',
'.vscode',
(Split-Path $PSCommandPath -Leaf)
)
# Name of the repository you wish to publish to
$PublishRepository = 'PSPrivateGallery'
$galleryServerUri = 'http://internalGallery:8080/api/v2'
# NuGet API key for the gallery.
$NuGetApiKey = 'myapikeywashere'
# Stop pester outputing blank lines see issue #450
$ProgressPreference = 'SilentlyContinue'
if ($ENV:BUILD_BUILDNUMBER) {
$BuildNumber = "$ENV:BUILD_BUILDNUMBER"
} else {
$BuildNumber = 0
}
}
Task ScriptSigning {
# Sign the scripts if we are running on a build server
If ((IsThisOnBuildServer) -eq $False) {Write-Error "This step should be only run from a build server, stopping." -ErrorAction Stop}
Write-Host "Signing scripts"
# We can only sign .ps1, .psm1, .psd1, and .ps1xml files
Get-ChildItem ("{0}\*.ps*"-f $PublishDir) | Sign-Script
}
###############################################################################
# Customize these tasks for performing operations before and/or after publish.
###############################################################################
# Executes before src is copied to publish dir
Task PreCopySource {
}
# Executes after src is copied to publish dir
Task PostCopySource {
}
# Executes before publishing occurs.
Task PrePublish {
}
# Executes after publishing occurs.
Task PostPublish {
}
###############################################################################
# Core task implementations
###############################################################################
Task default -depends Build
Task Init -requiredVariables PublishDir {
# Check if we are running on a tfs build server
If (IsThisOnBuildServer) {
$agent = $($env:AGENT_NAME)
}
else {
$agent = "N/A"
}
Write-Host ("Build Running on: {0}, Build Agent: {1}" -f $($env:ComputerName), $agent)
if (!(Test-Path $PublishDir)) {
Write-Host "Creating Publish folder ($PublishDir)"
$null = New-Item $PublishDir -ItemType Directory
}
if (!(Test-Path $TestResultsDir)) {
Write-Host "Creating TestResults folder ($TestResultsDir)"
$null = New-Item $TestResultsDir -ItemType Directory
}
If (!(Get-PSRepository -Name $PublishRepository -errorAction SilentlyContinue)) {
Write-Host "Repository $PublishRepository doesn't exist, Adding"
Register-PSRepository `
-Name $PublishRepository `
-SourceLocation $galleryServerUri `
-InstallationPolicy Trusted `
-PackageManagementProvider NuGet
}
}
Task Clean -depends Init -requiredVariables PublishDir {
# Sanity check the dir we are about to "clean". If $PublishDir were to
# inadvertently get set to $null, the Remove-Item commmand removes the
# contents of \*. That's a bad day. Ask me how I know? :-(
if ($PublishDir.Contains($PSScriptRoot)) {
Write-Host (' Cleaning publish directory "{0}".' -f $PublishDir)
Remove-Item $PublishDir\* -Recurse -Force
}
if ($TestResultsDir.Contains($PSScriptRoot)) {
Write-Host (' Cleaning test results directory "{0}".' -f $TestResultsDir)
Remove-Item $TestResultsDir\* -Recurse -Force
}
}
Task Build -depends Clean -requiredVariables PublishDir, Exclude, ModuleName {
If (IsThisOnBuildServer) {
Step-ModuleVersion -Path $ManifestPath -Verbose -Increment None -RevisionNumber $BuildNumber
}
else {
# Incrementing Build Version
Step-ModuleVersion -Path $ManifestPath -Verbose -Increment Build
$Version = Get-ModuleVersion -Path $ManifestPath
"$ModuleName Version: $Version"
}
Copy-Item $PSScriptRoot\* -Destination $PublishDir -Recurse -Exclude $Exclude
# Get contents of the ReleaseNotes file and update the copied module manifest file
# with the release notes.
# DO NOT USE UNTIL UPDATE-MODULEMANIFEST IS FIXED - HORRIBLY BROKEN RIGHT NOW.
# if ($ReleaseNotesPath) {
# $releaseNotes = @(Get-Content $ReleaseNotesPath)
# Update-ModuleManifest -Path $PublishDir\${ModuleName}.psd1 -ReleaseNotes $releaseNotes
# }
}
Task Test -depends TestImpl {
}
Task TestImpl -depends Build {
# Run pester tests
Import-Module Pester
# Display pester version being used to run tests
Write-Host ("Testing with Pester version {0}" -f (get-module pester).Version.ToString())
$invokePesterParams = @{
OutputFile = "$TestResultsDir/Test-Pester-$ModuleName-Script.XML";
OutputFormat = 'NUnitXml';
Strict = $true;
PassThru = $true;
Verbose = $false;
Show = "Fails";
}
$testResult = Invoke-Pester @invokePesterParams -Script @{ Path = "$PSScriptRoot\tests\$ModuleName.tests.ps1"; }
if ($testResult.FailedCount -gt 0) {
Write-Error ('Failed "{0}" unit tests.' -f $testResult.FailedCount);
}
}
Task Publish -depends Test, ScriptSigning, PrePublish, PublishImpl, PostPublish {
}
Task PublishImpl -depends Test -requiredVariables PublishDir {
$publishParams = @{
Path = $PublishDir
NuGetApiKey = $NuGetApiKey
Repository = $PublishRepository
}
# Consider not using -ReleaseNotes parameter when Update-ModuleManifest has been fixed.
if ($ReleaseNotesPath) {
$publishParams['ReleaseNotes'] = @(Get-Content $ReleaseNotesPath)
}
$Version = Get-ModuleVersion -Path $ManifestPath
"Publishing module ($ModuleName Version: $Version) to gallery: $PublishRepository"
Publish-Module @publishParams
}
Task ? -description 'Lists the available tasks' {
"Available tasks:"
$psake.context.Peek().tasks.Keys | Sort
}
###############################################################################
# Helper functions
###############################################################################
function Step-ModuleVersion {
[CmdletBinding()]
param(
# Specifies a path a valid Module Manifest file.
[Parameter(Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to one or more locations.")]
[Alias("PSPath")]
[ValidateScript({ Test-Path $_ -PathType Leaf })]
[string[]]$Path,
# Version section to step
[Parameter(Position=1)]
[ValidateSet("Major", "Minor", "Build", "Revision", "None")]
[Alias("Type")]
[string]$Increment = "Build",
[Parameter(Position=2)]
[int]$RevisionNumber
)
Begin
{
if (-not $PSBoundParameters.ContainsKey("Path")) {
$Path = (Get-Item $PWD\*.psd1)[0]
}
}
Process
{
foreach ($file in $Path)
{
$manifest = Import-PowerShellDataFile -Path $file
$newVersion = Step-Version `
-Version $manifest.ModuleVersion `
-Increment $Increment `
-RevisionNumber $RevisionNumber
$manifest.Remove("ModuleVersion")
$manifest.FunctionsToExport = $manifest.FunctionsToExport | ForEach-Object {$_}
$manifest.NestedModules = $manifest.NestedModules | ForEach-Object {$_}
$manifest.RequiredModules = $manifest.RequiredModules | ForEach-Object {$_}
$manifest.ModuleList = $manifest.ModuleList | ForEach-Object {$_}
if ($manifest.ContainsKey("PrivateData") -and $manifest.PrivateData.ContainsKey("PSData")) {
foreach ($node in $manifest.PrivateData["PSData"].GetEnumerator()) {
$key = $node.Key
if ($node.Value.GetType().Name -eq "Object[]") {
$value = $node.Value | ForEach-Object {$_}
}
else {
$value = $node.Value
}
$manifest[$key] = $value
}
$manifest.Remove("PrivateData")
}
New-ModuleManifest -Path $file -ModuleVersion $newVersion @manifest
}
}
}
function Get-ModuleVersion {
[CmdletBinding()]
param(
# Specifies a path a valid Module Manifest file.
[Parameter(Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to one or more locations.")]
[Alias("PSPath")]
[ValidateScript({ Test-Path $_ -PathType Leaf })]
[string[]]$Path
)
Begin {
if (-not $PSBoundParameters.ContainsKey("Path")) {
$Path = (Get-Item $PWD\*.psd1)[0]
}
}
Process {
foreach ($file in $Path) {
$manifest = Import-PowerShellDataFile -Path $file
return $manifest.ModuleVersion
}
}
}
function Step-Version {
Param (
[CmdletBinding()]
[OutputType([String])]
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[String]$Version,
[Parameter(Mandatory=$true, Position=1)]
[ValidateSet("Major", "Minor", "Build", "Revision", "None")]
[Alias("By")]
[string]$Increment,
[Parameter(Position=2)]
[string]$Revision = 0,
[Parameter(Position=3)]
[int]$RevisionNumber
)
Process {
$currentVersion = [version]$Version
[int]$major = $currentVersion.Major
[int]$minor = $currentVersion.Minor
[int]$build = $currentVersion.Build
[int]$revision = $currentVersion.Revision
if($Major -lt 0) { $Major = 0 }
if($Minor -lt 0) { $Minor = 0 }
if($Build -lt 0) { $Build = 0 }
if($Revision -lt 0) { $Revision = 0 }
switch($Increment) {
"Major" {
$major++
}
"Minor" {
$Minor++
}
"Build" {
$Build++
}
"Revision" {
$Revision++
}
"None" {
# Append revision number from param instead.
$Revision = $RevisionNumber
}
}
$newVersion = New-Object Version -ArgumentList $major, $minor, $Build, $Revision
Write-Output -InputObject $newVersion.ToString()
}
}
function Sign-Script {
<#
.SYNOPSIS
Script for signing scripts during the build process
.DESCRIPTION
This script is called after the pester tests are successfuly completed and then signs the code
.PARAMETER Path
Provide a path to the file you want to sign
.EXAMPLE
C:\PS> SignScript c:\temp\test.ps1
.NOTES
David Wallis
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$true,ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias('FullName')]
[string[]]$Path
)
Begin {
$cert =(Get-Childitem cert:\CurrentUser\My -CodeSigningCert)
If ($null -eq $cert) { write-error "No code signing certificate found" -ErrorAction Stop}
If($cert[0].NotAfter -le (get-date)) {
Write-error "Code signing certificate expired"
} else {
write-verbose "Certificate valid until: $($cert.NotAfter)"
}
}
Process{
Write-verbose "Signing script $($Path)"
$AuthenticodeSignature = Set-AuthenticodeSignature $Path $cert[0] -ErrorAction stop
}
}
Function IsThisOnBuildServer {
<#
.SYNOPSIS
Attempts to ensure that script signing and publishing only runs on a build server.
.DESCRIPTION
This validates the machine name that the build is running on to try and ensure
that code is only published from a build server
.EXAMPLE
IsThisOnBuildServer
.OUTPUTS
Boolean
.NOTES
David Wallis
#>
[CmdLetBinding()]
Param()
Return $env:ComputerName -match "^(PLL|VAL|CRL)WIN(DV|QA|LV|CS)BLD\d\d\d$"
}