-
Notifications
You must be signed in to change notification settings - Fork 8
/
ChocoCCM.build.ps1
316 lines (261 loc) · 10.5 KB
/
ChocoCCM.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
[CmdletBinding()]
param(
[Parameter()]
[string]
$OutputDirectory = "$PSScriptRoot/Output",
[Parameter()]
[string]
$TimeStampServer = $(
if ($env:CERT_TIMESTAMP_URL) {
$env:CERT_TIMESTAMP_URL
}
else {
'http://timestamp.digicert.com'
}
),
[string]
$CertificatePath = $env:CHOCOLATEY_OFFICIAL_CERT,
[string]
$CertificatePassword = $env:CHOCOLATEY_OFFICIAL_CERT_PASSWORD,
[Parameter()]
[string]
$CertificateAlgorithm = $(
if ($env:CERT_ALGORITHM) {
$env:CERT_ALGORITHM
}
else {
'Sha256'
}
),
[string]
$CertificateSubjectName = "Chocolatey Software, Inc.",
[Parameter()]
[string]
$NugetApiKey = $env:POWERSHELLPUSH_API_KEY,
[Parameter()]
[string]
$PublishUrl = $env:POWERSHELLPUSH_SOURCE
)
$ErrorActionPreference = 'Stop'
$script:SourceFolder = "$PSScriptRoot/src"
$script:ReleaseBuild = -not [string]::IsNullOrEmpty((git tag --points-at HEAD 2> $null) -replace '^v')
$script:BuildVersion = $null
$script:IsPrerelease = $false
# Fix for Register-PSRepository not working with https from StackOverflow:
# https://stackoverflow.com/questions/35296482/invalid-web-uri-error-on-register-psrepository/35296483#35296483
function Register-PSRepositoryFix {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[String]
$Name,
[Parameter(Mandatory = $true)]
[Uri]
$SourceLocation,
[ValidateSet('Trusted', 'Untrusted')]
$InstallationPolicy = 'Trusted'
)
$ErrorActionPreference = 'Stop'
try {
Write-Verbose 'Trying to register via Register-PSRepository'
Register-PSRepository -Name $Name -SourceLocation $SourceLocation -InstallationPolicy $InstallationPolicy -ErrorAction Stop
Write-Verbose 'Registered via Register-PSRepository'
}
catch {
Write-Verbose 'Register-PSRepository failed, registering via workaround'
# Adding PSRepository directly to file
Register-PSRepository -Name $Name -SourceLocation $env:TEMP -InstallationPolicy $InstallationPolicy -ErrorAction Stop
$PSRepositoriesXmlPath = "$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\PowerShellGet\PSRepositories.xml"
$repos = Import-Clixml -Path $PSRepositoriesXmlPath
$repos[$Name].SourceLocation = $SourceLocation.AbsoluteUri
$repos[$Name].PublishLocation = [uri]::new($SourceLocation, 'package').AbsoluteUri
$repos[$Name].ScriptSourceLocation = ''
$repos[$Name].ScriptPublishLocation = ''
$repos | Export-Clixml -Path $PSRepositoriesXmlPath
# Reloading PSRepository list
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Write-Verbose 'Registered via workaround'
}
}
# Synopsis: ensure GitVersion is installed
task InstallGitVersion {
if (-not (Get-Command gitversion -ErrorAction Ignore)) {
Write-Host "Installing Gitversion"
choco install gitversion.portable -y --no-progress
}
}
# Synopsis: ensure PowerShellGet has the NuGet provider installed
task BootstrapPSGet {
if (-not (Get-PackageProvider NuGet -ErrorAction Ignore)) {
Write-Host "Installing NuGet package provider"
Install-PackageProvider NuGet -MinimumVersion 2.8.5.201 -ForceBootstrap -Force
}
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
if (-not (Get-InstalledModule PowerShellGet -MinimumVersion 2.0 -MaximumVersion 2.99 -ErrorAction Ignore)) {
Install-Module PowerShellGet -MaximumVersion 2.99 -Force
Remove-Module PowerShellGet -Force
Import-Module PowerShellGet -MinimumVersion 2.0 -Force
}
}
# Synopsis: ensure Pester is installed
task InstallPester BootstrapPSGet, {
if (-not (Get-InstalledModule Pester -MaximumVersion 4.99 -ErrorAction SilentlyContinue)) {
Write-Host "Installing Pester"
Install-Module Pester -MaximumVersion 4.99 -SkipPublisherCheck -Force -Scope CurrentUser -ErrorAction Stop -Verbose:$false
}
}
# Synopsis: ensure PSScriptAnalyzer is installed
task InstallScriptAnalyzer BootstrapPSGet, {
if (-not (Get-InstalledModule PSScriptAnalyzer -MinimumVersion 1.20 -ErrorAction SilentlyContinue)) {
Write-Host "Installing PSSA"
Install-Module PSScriptAnalyzer -Scope CurrentUser -Force -MinimumVersion 1.20 -ErrorAction Stop -Verbose:$false
}
}
# Synopsis: cleanup build artifacts
task Clean {
Write-Host "Removing any prior existing files from ./Output"
remove $OutputDirectory
$null = New-Item -Path $OutputDirectory -ItemType Directory
}
# Synopsis: run PSScriptAnalyzer on project files
task ScriptAnalyzer InstallScriptAnalyzer, {
Write-Host "Checking project files with PSScriptAnalyzer"
$results = Invoke-ScriptAnalyzer -Path $script:SourceFolder -Recurse -Settings "$PSScriptRoot/PSScriptAnalyzerSettings.psd1"
if ($results) {
Write-Host "PSSA rule violations found:" -ForegroundColor Red
$results | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Red
throw "PSSA rule violations detected, see above errors for more information"
}
}
# Synopsis: build the project
task Build Clean, InstallGitVersion, ScriptAnalyzer, {
$null = New-Item "$OutputDirectory/ChocoCCM" -ItemType Directory
$manifest = Copy-Item "$script:SourceFolder/ChocoCCM.psd1" -Destination "$OutputDirectory/ChocoCCM" -PassThru
$moduleScripts = @(
Get-Item "$script:SourceFolder/module.ps1"
Get-Item "$script:SourceFolder/Private/*.ps1"
Get-Item "$script:SourceFolder/Public/*.ps1"
)
$moduleScripts |
Get-Content |
Add-Content "$OutputDirectory/ChocoCCM/ChocoCCM.psm1"
$gitversion = gitversion.exe
$gitversion | Out-String -Width 120 | Write-Host
$versionInfo = $gitversion | ConvertFrom-Json
$manifestUpdates = @{
Path = $manifest.FullName
ModuleVersion = $versionInfo.MajorMinorPatch
}
$prerelease = $versionInfo.NuGetPreReleaseTagV2 -replace '[^a-z0-9]'
if ($prerelease) {
if ($prerelease -notmatch '^(alpha|beta)') {
$prerelease = "alpha$prerelease"
}
$manifestUpdates.Prerelease = $prerelease
$script:IsPrerelease = $true
}
$script:BuildVersion = if ($prerelease) {
"$($versionInfo.MajorMinorPatch)-$prerelease"
}
else {
$versionInfo.MajorMinorPatch
}
Update-ModuleManifest @manifestUpdates
}
task ImportChecks -After Build {
$publicFunctions = Get-Item "$script:SourceFolder/Public/*.ps1"
Import-Module "$OutputDirectory/ChocoCCM" -Force
$actualFunctions = (Get-Module ChocoCCM).ExportedFunctions
if ($actualFunctions.Count -lt $publicFunctions.Count) {
$missingFunctions = $publicFunctions.BaseName | Where-Object { $_ -notin $actualFunctions.Keys }
$message = @(
"src/Public: $($publicFunctions.Count) files"
"ChocoCCM: $($actualFunctions.Count) exported functions"
"some functions in the Public folder may not be exported"
"missing functions may include: $($missingFunctions -join ', ')"
) -join "`n"
Write-Warning $message
}
elseif ($publicFunctions.Count -lt $actualFunctions.Count) {
$message = @(
"src/Public: $($publicFunctions.Count) files"
"ChocoCCM: $($actualFunctions.Count) exported functions"
"there seems to be fewer files in the Public folder than public functions exported"
) -join "`n"
Write-Warning $message
}
}
# Synopsis: CI-specific build operations to run after the normal build
task CIBuild Build, {
Write-Host $env:GitVersionTool
Write-Host "##teamcity[buildNumber '$script:BuildVersion']"
}
# Synopsis: run Pester tests
task Test InstallPester, Build, {
Import-Module Pester -MaximumVersion 4.99
Copy-Item -Path "$script:SourceFolder/Tests" -Destination "$OutputDirectory/Tests" -Recurse
$results = Invoke-Pester (Resolve-Path "$OutputDirectory/Tests") -OutputFile "$OutputDirectory/test.results.xml" -OutputFormat NUnitXml -PassThru
assert ($results.FailedCount -eq 0) "Pester test failures found, see above or the '$OutputDirectory/test.results.xml' result file for details"
}
# Synopsis: generate documentation files
task GenerateDocs {
& "$PSScriptRoot/mkdocs.ps1"
}
# Synopsis: sign PowerShell scripts
task Sign Build, {
Write-Host "Signing PSM1 module file"
$signParams = @{
CertificateAlgorithm = $CertificateAlgorithm
TimeStampServer = $TimeStampServer
ScriptsToSign = Get-ChildItem -Path "$OutputDirectory/ChocoCCM" -Recurse -Include '*.ps1', '*.psm1'
}
if ($CertificatePath) {
$signParams.CertificatePath = $CertificatePath
$signParams.CertificatePassword = $CertificatePassword
}
else {
$signParams.CertificateSubjectName = $CertificateSubjectName
}
& "$PSScriptRoot/sign.ps1" @signParams
}
# Synopsis: publish ChocoCCM either internally or to the PSGallery
task Publish -If ($script:ReleaseBuild -or $PublishUrl) Build, {
if (-not (Test-Path $OutputDirectory)) {
throw 'Build the module with `Invoke-Build` or `build.ps1` before attempting to publish the module'
}
if (-not $NugetApiKey) {
throw 'Please pass the API key for publishing to the `-NugetApiKey` parameter or set $env:NugetApiKey before publishing'
}
$psdFile = Resolve-Path "$OutputDirectory/ChocoCCM"
$publishParams = @{
Path = $psdFile
NugetApiKey = $NugetApiKey
}
if ($PublishUrl) {
Write-Verbose "Publishing to '$PublishUrl'"
$repo = Get-PSRepository | Where-Object PublishLocation -EQ $PublishUrl
if ($repo) {
$publishParams.Repository = $repo.Name
}
else {
$testRepo = @{
Name = 'ChocoCCM'
SourceLocation = $PublishUrl
InstallationPolicy = 'Trusted'
}
Register-PSRepositoryFix @testRepo
$publishParams.Repository = 'ChocoCCM'
}
Publish-Module @publishParams
}
if ($script:ReleaseBuild) {
Write-Verbose "Publishing to PSGallery"
$publishParams.NugetApiKey = $env:POWERSHELLGALLERY_API_KEY
$publishParams.Repository = 'PSGallery'
Publish-Module @publishParams
}
}
# Synopsis: CI configuration; test, build, sign the module, and publish
task CI CIBuild, Sign, Test, Publish
# Synopsis: default task; build and test
task . Build, Test