-
Notifications
You must be signed in to change notification settings - Fork 20
/
Build.PSake.ps1
161 lines (126 loc) · 6.13 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
#requires -Version 5;
#requires -Modules VirtualEngine.Build;
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')]
param ()
$psake.use_exit_on_error = $true;
Properties {
$moduleName = (Get-Item $PSScriptRoot\*.psd1)[0].BaseName;
$basePath = $psake.build_script_dir;
$buildDir = 'Release';
$buildPath = (Join-Path -Path $basePath -ChildPath $buildDir);
$releasePath = (Join-Path -Path $buildPath -ChildPath $moduleName);
$thumbprint = '6F72C7A1BD6979DD8F08DC066ABC12FB80A453E9';
$timeStampServer = 'http://timestamp.digicert.com';
$exclude = @(
'.git*',
'.vscode',
'Release',
'Tests',
'Build.PSake.ps1',
'*.png',
'*.md',
'*.enc',
'TestResults.xml',
'appveyor.yml',
'appveyor-tools'
);
$signExclude = @('en-US','*.mof');
}
# Synopsis:
Task Init {
# Properties are not available in the script scope.
Set-Variable manifest -Value (Get-ModuleManifest) -Scope Script;
Set-Variable version -Value $manifest.Version -Scope Script;
Write-Host (" Building module '{0}'." -f $manifest.Name) -ForegroundColor Yellow;
Write-Host (" Building version '{0}'." -f $version) -ForegroundColor Yellow;
} #end task Init
# Synopsis: Cleans the release directory
Task Clean -Depends Init {
Write-Host (' Cleaning release directory "{0}".' -f $buildPath) -ForegroundColor Yellow;
if (Test-Path -Path $buildPath) {
Remove-Item -Path $buildPath -Include * -Recurse -Force;
}
[ref] $null = New-Item -Path $buildPath -ItemType Directory -Force;
[ref] $null = New-Item -Path $releasePath -ItemType Directory -Force;
} #end task Clean
# Synopsis: Invokes Pester tests
Task Test -Depends Init {
$invokePesterParams = @{
Path = "$basePath\Tests";
OutputFile = "$basePath\TestResults.xml";
OutputFormat = 'NUnitXml';
Strict = $true;
PassThru = $true;
Verbose = $false;
}
$testResult = Invoke-Pester @invokePesterParams;
if ($testResult.FailedCount -gt 0) {
Write-Error ('Failed "{0}" unit tests.' -f $testResult.FailedCount);
}
} #end task Test
# Synopsis: Copies release files to the release directory
Task Deploy -Depends Clean {
Get-ChildItem -Path $basePath -Exclude $exclude | ForEach-Object {
Write-Host (' Copying {0}' -f $PSItem.FullName) -ForegroundColor Yellow;
Copy-Item -Path $PSItem -Destination $releasePath -Recurse;
}
} #end task Deploy
# Synopsis: Signs files in release directory
Task Sign -Depends Deploy {
if (-not (Get-ChildItem -Path 'Cert:\CurrentUser\My' | Where-Object Thumbprint -eq $thumbprint)) {
## Decrypt and import code signing cert
.\appveyor-tools\secure-file.exe -decrypt .\VE_Certificate_2023.pfx.enc -secret "$env:certificate_secret" -salt "$env:certificate_salt"
$certificatePassword = ConvertTo-SecureString -String $env:certificate_secret -AsPlainText -Force
Import-PfxCertificate -FilePath .\VE_Certificate_2023.pfx -CertStoreLocation 'Cert:\CurrentUser\My' -Password $certificatePassword
}
Get-ChildItem -Path $releasePath -Exclude $signExclude | ForEach-Object {
if ($PSItem -is [System.IO.DirectoryInfo]) {
Get-ChildItem -Path $PSItem.FullName -Include *.ps* -Recurse | ForEach-Object {
Write-Host (' Signing {0}' -f $PSItem.FullName) -ForegroundColor Yellow -NoNewline;
$signResult = Set-ScriptSignature -Path $PSItem.FullName -Thumbprint $thumbprint -TimeStampServer $timeStampServer -ErrorAction Stop;
Write-Host (' {0}.' -f $signResult.Status) -ForegroundColor Green;
}
}
elseif ($PSItem.Name -like '*.ps*') {
Write-Host (' Signing {0}' -f $PSItem.FullName) -ForegroundColor Yellow -NoNewline;
$signResult = Set-ScriptSignature -Path $PSItem.FullName -Thumbprint $thumbprint -TimeStampServer $timeStampServer -ErrorAction Stop;
Write-Host (' {0}.' -f $signResult.Status) -ForegroundColor Green;
}
}
} #end task Sign
Task Version -Depends Deploy {
$nuSpecPath = Join-Path -Path $releasePath -ChildPath "$ModuleName.nuspec"
$nuspec = [System.Xml.XmlDocument] (Get-Content -Path $nuSpecPath -Raw)
$nuspec.Package.MetaData.Version = $version.ToString()
$nuspec.Save($nuSpecPath)
} #end task Version
# Synopsis: Publishes release module to PSGallery
Task Publish_PSGallery -Depends Version {
Publish-Module -Path $releasePath -NuGetApiKey "$env:gallery_api_key" -Verbose;
} #end task Publish
# Synopsis: Creates release module Nuget package
Task Package -Depends Build {
$targetNuSpecPath = Join-Path -Path $releasePath -ChildPath "$ModuleName.nuspec"
NuGet.exe pack "$targetNuSpecPath" -OutputDirectory "$env:TEMP"
} #end task Package
# Synopsis: Publish release module to Dropbox repository
Task Publish_Dropbox -Depends Package {
$targetNuPkgPath = Join-Path -Path "$env:TEMP" -ChildPath "$ModuleName.$version.nupkg"
$destinationPath = "$env:USERPROFILE\Dropbox\PSRepository"
Copy-Item -Path "$targetNuPkgPath"-Destination $destinationPath -Force
} #end task Publish_Dropbox
# Synopsis: Publish test results to AppVeyor
Task AppVeyor {
Get-ChildItem -Path "$basePath\*Results*.xml" | Foreach-Object {
$address = 'https://ci.appveyor.com/api/testresults/nunit/{0}' -f $env:APPVEYOR_JOB_ID
$source = $_.FullName
Write-Verbose "UPLOADING TEST FILE: $address $source" -Verbose
(New-Object 'System.Net.WebClient').UploadFile( $address, $source )
}
} #end task AppVeyor
Task Default -Depends Init, Clean, Test
Task Build -Depends Default, Deploy, Version, Sign
Task Publish -Depends Build, Package, Publish_PSGallery
Task Local -Depends Build, Package, Publish_Dropbox
Task UnsignedBuild -Depends Default, Deploy, Version
Task UnsignedLocal -Depends UnsignedBuild, Package, Publish_Dropbox