-
Notifications
You must be signed in to change notification settings - Fork 25
/
Chocolatey-AU.build.ps1
402 lines (322 loc) · 13.9 KB
/
Chocolatey-AU.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
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
param(
[string]
$OutputDirectory = "$PSScriptRoot/code_drop",
[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,
[string]
$CertificateAlgorithm = $(
if ($env:CERT_ALGORITHM) {
$env:CERT_ALGORITHM
}
else {
'Sha256'
}
),
[string]
$CertificateSubjectName = $(
if ($CHOCOLATEY_OFFICIAL_CERT_SUBJECT_NAME) {
$CHOCOLATEY_OFFICIAL_CERT_SUBJECT_NAME
}
else {
'Chocolatey Software, Inc'
}
),
[string]
$NugetApiKey = $env:POWERSHELLPUSH_API_KEY,
[string]
$PublishUrl = $env:POWERSHELLPUSH_SOURCE,
[string]
$ChocolateyNugetApiKey = $env:CHOCOOPSPUSH_API_KEY,
[string]
$ChocolateyPublishUrl = $env:CHOCOOPSPUSH_SOURCE,
[string]
$ModuleName = 'Chocolatey-AU',
[switch]
$ThrowOnPSSAViolation
)
$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
$script:ModuleOutputDir = "$OutputDirectory/$ModuleName"
# 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)) -and (-not (Get-Command dotnet-gitversion -ErrorAction Ignore))) {
Write-Host "Gitversion not installed. Attempting to install"
if (-not ([Security.Principal.WindowsPrincipal]([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){
throw "You are not an administrator. We cannot use Chocolatey to install gitversion.portable."
}
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 -AllowClobber -Scope CurrentUser
Remove-Module PowerShellGet -Force
Import-Module PowerShellGet -MinimumVersion 2.0 -Force
Import-PackageProvider -Name 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 {
remove $OutputDirectory
New-Item -Path $OutputDirectory -ItemType Directory | Out-Null
}
# Synopsis: run PSScriptAnalyzer on project files
task ScriptAnalyzer InstallScriptAnalyzer, {
$results = Invoke-ScriptAnalyzer -Path $script:SourceFolder -Recurse -Settings "$PSScriptRoot/PSScriptAnalyzerSettings.psd1"
if ($results) {
Write-Warning "$($results.Count) PSSA rule violations found."
$results
# Chocolatey-AU currently has lots of PSSA violations. None of them are errors in PSSA.
# For now, the build will not fail on PSSA unless asked.
if ($ThrowOnPSSAViolation) {
throw "PSSA rule violations detected, see above errors for more information"
}
}
}
# Synopsis: build the project
task Build Clean, InstallGitVersion, ScriptAnalyzer, {
New-Item $script:ModuleOutputDir -ItemType Directory | Out-Null
Copy-Item "$script:SourceFolder/*" -Destination $script:ModuleOutputDir -Recurse
$manifest = Get-ChildItem "$OutputDirectory/$ModuleName/$ModuleName.psd1"
$gitversion = if (Get-Command gitversion -ErrorAction Ignore)
{
gitversion.exe
}
else {
dotnet-gitversion.exe
}
$gitversion | Out-String -Width 120 | Write-Host
$versionInfo = $gitversion 2>$null | 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"
}
if ($prerelease.Length -gt 20) {
$prerelease = $prerelease.Substring(0, 20)
}
$manifestUpdates.Prerelease = $prerelease
$script:IsPrerelease = $true
}
$script:BuildVersion = if ($prerelease) {
"$($versionInfo.MajorMinorPatch)-$prerelease"
}
else {
$versionInfo.MajorMinorPatch
}
$help_dir = "${script:ModuleOutputDir}/en-US"
New-Item -Type Directory -Force $help_dir | Out-Null
Get-Content $PSScriptRoot/README.md | Select-Object -Skip 4 | Set-Content "$help_dir/about_$ModuleName.help.txt" -Encoding ascii
Update-ModuleManifest @manifestUpdates
}
# Synopsis: Create the Chocolatey Package
task CreateChocolateyPackage -After Sign {
$ReadmePath = "$PSScriptRoot/README.md"
$Readme = Get-Content $ReadmePath -Raw
if (-not ($Readme -match '## Features(.|\n)+?(?=\n##)'))
{
throw "No 'Features' found in '$ReadmePath'"
}
$features = $Matches[0]
$ChocolateyPackageDir = "$OutputDirectory/temp/chocolateyPackage"
New-Item $ChocolateyPackageDir -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
Copy-Item $PSScriptRoot/chocolatey/* $ChocolateyPackageDir -Recurse
$nuspecPath = "$ChocolateyPackageDir/chocolatey-au.nuspec"
[xml]$chocolateyPackage = Get-Content $nuspecPath
$description = $chocolateyPackage.package.metadata.summary + ".`n`n" + $features
$chocolateyPackage.package.metadata.description = $description
$chocolateyPackage.Save($nuspecPath)
Copy-Item $script:ModuleOutputDir $ChocolateyPackageDir/tools -Recurse -Force
choco pack $nuspecPath --outputdirectory $OutputDirectory --version $script:BuildVersion
$script:ChocolateyPackagePath = Get-ChildItem $OutputDirectory -Filter *.nupkg | Select-Object -ExpandProperty FullName
if (-not (Test-Path $script:ChocolateyPackagePath)) {
throw 'Chocolatey Package failed to pack.'
}
}
# Synopsis: zip up the built project
task Create7zipArchive -After Sign {
$zip_path = "$OutputDirectory\${ModuleName}_${script:BuildVersion}.7z"
$cmd = "$Env:ChocolateyInstall/tools/7z.exe a '$zip_path' '$OutputDirectory/$ModuleName' '$PSScriptRoot/chocolatey/tools/install.ps1'"
$cmd | Invoke-Expression | Out-Null
if (!(Test-Path $zip_path)) { throw "Failed to build 7z package" }
}
task ImportChecks -After Build {
$publicFunctions = Get-Item "$script:SourceFolder/Public/*.ps1"
Remove-Module $ModuleName -ErrorAction Ignore
Import-Module $script:ModuleOutputDir -Force
$actualFunctions = (Get-Module $ModuleName).ExportedFunctions
if ($actualFunctions.Count -lt $publicFunctions.Count) {
$missingFunctions = $publicFunctions.BaseName | Where-Object { $_ -notin $actualFunctions.Keys }
$message = @(
"src/Public: $($publicFunctions.Count) files"
"${ModuleName}: $($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"
"${ModuleName}: $($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: sign PowerShell scripts
task Sign -After Build {
$ScriptsToSign = Get-ChildItem -Path $script:ModuleOutputDir -Recurse -Include '*.ps1', '*.psm1'
if ($CertificatePath) {
$CertificatePath = $CertificatePath
$CertificatePassword = $CertificatePassword
}
else {
$CertificateSubjectName = $CertificateSubjectName
}
$cert = if ($CertificatePath) {
New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertificatePath, $CertificatePassword)
}
else {
Get-ChildItem Cert:\LocalMachine\My | Where-Object Subject -Like "*$CertificateSubjectName*"
}
if ($null -eq $cert) {
Write-Warning "No certificate found for signing. Module not being signed."
return
}
Set-AuthenticodeSignature -FilePath $ScriptsToSign -Certificate $cert -TimestampServer $TimeStampServer -IncludeChain NotRoot -HashAlgorithm $CertificateAlgorithm
}
# Synopsis: publish $ModuleName 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 -or -not $ChocolateyNugetApiKey) {
throw 'Please pass the API key for publishing to both the `-NugetApiKey` and `-ChocolateyNugetApiKey` parameter or set $env:POWERSHELLPUSH_API_KEY and $env:CHOCOOPSPUSH_API_KEY before publishing'
}
$psdFile = Resolve-Path $script:ModuleOutputDir
$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 = "$ModuleName"
SourceLocation = $PublishUrl
InstallationPolicy = 'Trusted'
}
Register-PSRepositoryFix @testRepo
$publishParams.Repository = "$ModuleName"
}
Publish-Module @publishParams
}
if ($ChocolateyPublishUrl) {
Write-Verbose "Publishing to '$ChocolateyPublishUrl'"
choco push $script:ChocolateyPackagePath --source $ChocolateyPublishUrl --key $ChocolateyNugetApiKey
if ($LASTEXITCODE -ne 0) {
throw "Chocolatey push to $ChocolateyPublishUrl failed."
}
}
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