-
Notifications
You must be signed in to change notification settings - Fork 162
/
default.ps1
330 lines (279 loc) · 16.3 KB
/
default.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
$psake.use_exit_on_error = $true
properties {
$baseDir = (Split-Path -parent $psake.build_script_dir)
$counter = $buildCounter
$tagName = git tag -l --points-at HEAD
if ($tagName) {
Write-Host "Found tag ${'$'}tagName"
$isTagged = $true
} else {
Write-Host "No tag found for current commit"
$isTagged = $false
}
$script:version="1.0.0"
$script:packageVersion = $version
$script:informationalVersion = $version
$script:changeset = "abcdef"
$nugetExe = "$env:ChocolateyInstall\bin\nuget.exe"
$msbuildExe="${env:programFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\msbuild.exe"
$reportUnitExe = "$env:ChocolateyInstall\bin\ReportUnit.exe"
$gitVersionExe = "$env:ChocolateyInstall\lib\GitVersion.Portable\tools\gitversion.exe"
}
Task default -depends Build
Task Build -depends Run-GitVersion, Build-Clickonce, Install-ChocoPkg, Test, Package
Task Deploy -depends Build, Publish-Clickonce -description 'Versions, packages and pushes'
Task Package -depends Clean-Artifacts, Version-Module, Install-ChocoPkg, Create-ModuleZipForRemoting, Pack-Chocolatey, Package-DownloadZip -description 'Versions the psd1 and packs the module and example package'
Task All-Tests -depends Test, Integration-Test
Task Quick-Deploy -depends Run-GitVersion, Build-Clickonce, Package, Publish-Clickonce
task Run-GitVersion {
Write-Host "Testing to see if running on TeamCity..."
if($env:TEAMCITY_VERSION) {
Write-Host "Running on TeamCity."
Write-Host "Running GitVersion with output type build server..."
. $gitVersionExe /output buildserver /nocache /nofetch
Write-Host "Running GitVersion again with output type json..."
$output = . $gitVersionExe /output json /nocache /nofetch
} else {
Write-Host "Not running on TeamCity."
Write-Host "Running GitVersion with output type json..."
$output = . $gitVersionExe /output json /nocache
}
Write-Host "Writing output variable..."
Write-Host $output
$joined = $output -join "`n"
Write-Host "Writing joined variable..."
Write-Host $joined
$versionInfo = $joined | ConvertFrom-Json
$sha = $versionInfo.Sha.Substring(0,8)
$majorMinorPatch = $versionInfo.MajorMinorPatch
$buildDate = Get-Date -Format "yyyyMMdd"
$script:changeset = $versionInfo.Sha
$script:version = $versionInfo.AssemblySemVer
# Having a pre-release label of greater than 10 characters can cause problems when trying to run choco pack.
# Since we typically only see this when building a local feature branch, or a PR, let's just trim it down to
# the 10 character limit, and move on.
if ($versionInfo.PreReleaseLabel) {
$prerelease = $versionInfo.PreReleaseLabel.Replace("-","").Substring(0,10)
}
# Chocolatey doesn't support a prerelease that starts with a digit.
# If we see a digit here, merely replace it with an `a` to get around this.
if ($prerelease -match "^\d") {
$prerelease = "a$($prerelease.Substring(1,9))"
}
if ($isTagged) {
$script:packageVersion = $versionInfo.LegacySemVer
$script:informationalVersion = $versionInfo.InformationalVersion
} else {
$script:packageVersion = "$majorMinorPatch" + $(if ($prerelease) { "-$prerelease" } else { "-rc" }) + "-$buildDate" + $(if ($counter) { "-$counter" })
$script:informationalVersion = "$majorMinorPatch" + $(if ($prerelease) { "-$prerelease" } else { "-rc" }) + "-$buildDate-$sha"
}
Write-Host "Assembly Semantic Version: $version"
Write-Host "Assembly Informational Version: $informationalVersion"
Write-Host "Package Version: $packageVersion"
Write-Host "##teamcity[buildNumber '$packageVersion']"
}
task Create-ModuleZipForRemoting {
if (Test-Path "$basedir\Boxstarter.Chocolatey\Boxstarter.zip") {
Remove-Item "$baseDir\Boxstarter.Chocolatey\boxstarter.zip" -Recurse -Force
}
if(!(Test-Path "$baseDir\buildArtifacts")){
mkdir "$baseDir\buildArtifacts"
}
Remove-Item "$env:temp\Boxstarter.zip" -Force -ErrorAction SilentlyContinue
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\boxstarter.Common" | Out-Null
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\boxstarter.WinConfig" | Out-Null
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\boxstarter.bootstrapper" | Out-Null
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\boxstarter.chocolatey" | Out-Null
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\boxstarter.config" | Out-Null
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\LICENSE.txt" | Out-Null
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\NOTICE.txt" | Out-Null
if($taskList -eq 'test'){
."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\buildartifacts\Boxstarter.zip" "$basedir\Chocolatey" | Out-Null
}
Move-Item "$basedir\buildartifacts\Boxstarter.zip" "$basedir\boxstarter.chocolatey\Boxstarter.zip"
}
task Build-ClickOnce -depends Install-MSBuild, Install-Win8SDK, Restore-NuGetPackages {
Update-AssemblyInfoFiles $version $changeset
exec { .$msbuildExe "$baseDir\Boxstarter.ClickOnce\Boxstarter.WebLaunch.csproj" /t:Clean /v:minimal }
exec { .$msbuildExe "$baseDir\Boxstarter.ClickOnce\Boxstarter.WebLaunch.csproj" /t:Build /v:minimal }
}
task Publish-ClickOnce -depends Install-MSBuild {
exec { .$msbuildExe "$baseDir\Boxstarter.ClickOnce\Boxstarter.WebLaunch.csproj" /t:Publish /v:minimal /p:ApplicationVersion="$version" }
Remove-Item "$basedir\web\Launch" -Recurse -Force -ErrorAction SilentlyContinue
MkDir "$basedir\web\Launch"
Set-Content "$basedir\web\Launch\.gitattributes" -Value "* -text"
Copy-Item "$basedir\Boxstarter.Clickonce\bin\Debug\App.Publish\*" "$basedir\web\Launch" -Recurse -Force
}
Task Test -depends Install-ChocoPkg, Pack-Chocolatey, Create-ModuleZipForRemoting {
Push-Location "$baseDir"
$pesterDir = "$env:ChocolateyInstall\lib\Pester"
$pesterTestResultsFile = "$baseDir\buildArtifacts\TestResults.xml"
$pesterTestResultsHtmlFile = "$baseDir\buildArtifacts\TestResults.html"
if($testName){
."$pesterDir\tools\bin\Pester.bat" $baseDir/Tests -testName $testName -OutputFile $pesterTestResultsFile -OutputFormat NUnitXml
}
else{
."$pesterDir\tools\bin\Pester.bat" $baseDir/Tests -OutputFile $pesterTestResultsFile -OutputFormat NUnitXml
}
if($LastExitCode -ne 0) {
# Generate HTML version of report
if(Test-Path $pesterTestResultsFile) {
.$reportUnitExe $pesterTestResultsFile $pesterTestResultsHtmlFile
}
throw 'There were failed unit tests.'
}
Pop-Location
}
Task Integration-Test -depends Pack-Chocolatey, Create-ModuleZipForRemoting {
Push-Location "$baseDir"
$pesterDir = "$env:ChocolateyInstall\lib\Pester"
if($testName){
exec {."$pesterDir\tools\bin\Pester.bat" $baseDir/IntegrationTests -testName $testName}
}
else{
exec {."$pesterDir\tools\bin\Pester.bat" $baseDir/IntegrationTests }
}
Pop-Location
}
Task Version-Module -description 'Stamps the psd1 with the version and last changeset SHA' {
Get-ChildItem "$baseDir\**\*.psd1" | ForEach-Object {
$path = $_
(Get-Content $path) |
ForEach-Object {$_ -replace "^ModuleVersion = '.*'`$", "ModuleVersion = '$version'" } |
ForEach-Object {$_ -replace "^PrivateData = '.*'`$", "PrivateData = '$changeset'" } |
Set-Content $path
}
(Get-Content "$baseDir\BuildScripts\bootstrapper.ps1") |
ForEach-Object {$_ -replace "Version = .*`$", "Version = `"$packageVersion`"," } |
Set-Content "$baseDir\BuildScripts\bootstrapper.ps1"
}
Task Clean-Artifacts {
if (Test-Path "$baseDir\buildArtifacts") {
Remove-Item "$baseDir\buildArtifacts" -Recurse -Force
}
mkdir "$baseDir\buildArtifacts"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders\Boxstarter.Azure"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders\Boxstarter.Bootstrapper"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders\Boxstarter.Chocolatey"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders\Boxstarter.Common"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders\Boxstarter.HyperV"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders\Boxstarter.TestRunner"
mkdir "$baseDir\buildArtifacts\tempNuGetFolders\Boxstarter.WinConfig"
}
Task Pack-Chocolatey -depends Sign-PowerShellFiles -description 'Packs the modules and example packages' {
if (Test-Path "$baseDir\buildPackages\*.nupkg") {
Remove-Item "$baseDir\buildPackages\*.nupkg" -Force
}
PackDirectory "$baseDir\BuildPackages"
PackDirectory "$baseDir\BuildScripts\nuget" -Version $packageVersion
Move-Item "$baseDir\BuildScripts\nuget\*.nupkg" "$basedir\buildArtifacts"
}
Task Package-DownloadZip -depends Clean-Artifacts {
if (Test-Path "$basedir\BuildArtifacts\Boxstarter.*.zip") {
Remove-Item "$basedir\BuildArtifacts\Boxstarter.*.zip" -Force
}
exec { ."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\BuildArtifacts\Boxstarter.$packageVersion.zip" "$basedir\LICENSE.txt" }
exec { ."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\BuildArtifacts\Boxstarter.$packageVersion.zip" "$basedir\NOTICE.txt" }
exec { ."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\BuildArtifacts\Boxstarter.$packageVersion.zip" "$basedir\buildscripts\bootstrapper.ps1" }
exec { ."$env:chocolateyInstall\bin\7za.exe" a -tzip "$basedir\BuildArtifacts\Boxstarter.$packageVersion.zip" "$basedir\buildscripts\Setup.bat" }
}
task Install-MSBuild {
if(!(Test-Path "${env:programFiles(x86)}\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\msbuild.exe")) {
choco install visualstudio2017buildtools -params '--add Microsoft.VisualStudio.Workload.WebBuildTools' --version=15.8.7.0 --no-progress -y
choco install microsoft-build-tools --version=15.0.26320.2 --no-progress -y
}
}
task Install-Win8SDK {
if(!(Test-Path "$env:ProgramFiles\Windows Kits\8.1\bin\x64\signtool.exe")) { choco install windows-sdk-8.1 --version=8.100.26654.0 -y --no-progress }
}
Task Restore-NuGetPackages {
exec { .$nugetExe restore "$baseDir\Boxstarter.sln" -msbuildpath "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin" }
}
task Install-ChocoPkg {
MkDir $basedir\Boxstarter.Chocolatey\chocolatey -ErrorAction SilentlyContinue
$chocoVersion = "1.1.0"
$srcUrl = "https://community.chocolatey.org/api/v2/package/chocolatey/$chocoVersion"
$targetFile = "chocolatey.$chocoVersion.nupkg"
Push-Location $basedir\Boxstarter.Chocolatey\chocolatey
try {
if (-Not (Test-Path $targetFile)) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $srcUrl -OutFile $targetFile
}
}
finally {
Pop-Location
}
}
task Copy-PowerShellFiles -depends Clean-Artifacts {
$tempNuGetDirectory = "$basedir\buildArtifacts\tempNuGetFolders"
$exclude = @("bin", "obj", "*.pssproj")
Copy-Item -Path $basedir\BuildScripts\chocolateyinstall.ps1 -Destination $tempNuGetDirectory
Copy-Item -Path $basedir\BuildScripts\chocolateyUninstall.ps1 -Destination $tempNuGetDirectory
Copy-Item -Path $basedir\BuildScripts\setup.ps1 -Destination $tempNuGetDirectory
Copy-Item -Path $basedir\BuildScripts\nuget\Boxstarter.Azure.PreInstall.ps1 -Destination $tempNuGetDirectory
Copy-Item -Path $basedir\BuildScripts\BoxstarterChocolateyInstall.ps1 -Destination $tempNuGetDirectory
Copy-Item -Path $basedir\BoxstarterShell.ps1 -Destination $tempNuGetDirectory
Copy-Item -Path $basedir\BuildScripts\VERIFICATION.txt -Destination $tempNuGetDirectory
Copy-Item -Path $basedir\Boxstarter.Azure\* -Recurse -Destination $tempNuGetDirectory\Boxstarter.Azure -Exclude $exclude
Copy-Item -Path $basedir\Boxstarter.Bootstrapper\* -Recurse -Destination $tempNuGetDirectory\Boxstarter.Bootstrapper -Exclude $exclude
Copy-Item -Path $basedir\Boxstarter.Chocolatey\* -Recurse -Destination $tempNuGetDirectory\Boxstarter.Chocolatey -Exclude $exclude
Copy-Item -Path $basedir\Boxstarter.Common\* -Recurse -Destination $tempNuGetDirectory\Boxstarter.Common -Exclude $exclude
Copy-Item -Path $basedir\Boxstarter.HyperV\* -Recurse -Destination $tempNuGetDirectory\Boxstarter.HyperV -Exclude $exclude
Copy-Item -Path $basedir\Boxstarter.TestRunner\* -Recurse -Destination $tempNuGetDirectory\Boxstarter.TestRunner -Exclude $exclude
Copy-Item -Path $basedir\Boxstarter.WinConfig\* -Recurse -Destination $tempNuGetDirectory\Boxstarter.WinConfig -Exclude $exclude
}
task Sign-PowerShellFiles -depends Copy-PowerShellFiles {
$timestampServer = "http://timestamp.digicert.com"
$certPfx = "$env:CHOCOLATEY_OFFICIAL_CERT"
$certPasswordFile = "$env:CHOCOLATEY_OFFICIAL_CERT_PASSWORD"
$tempNuGetDirectory = "$basedir\buildArtifacts\tempNuGetFolders"
$powerShellFiles = Get-ChildItem -Path $tempNuGetDirectory -Recurse -Include @("*.ps1", "*.psm1", "*.psd1") -File
if($certPfx -And $certPasswordFile -And (Test-Path $certPfx) -And (Test-Path $certPasswordFile)) {
$certPassword = Get-Content "$env:CHOCOLATEY_OFFICIAL_CERT_PASSWORD"
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPfx, $certPassword)
}
elseif($env:STORE_CHOCOLATEY_OFFICIAL_CERT -eq 'true' -or $env:STORE_DEVTEST_CERT -eq 'true') {
$cert = Get-ChildItem Cert:\LocalMachine\My | Where-Object Subject -like "*$($env:CERT_SUBJECT_NAME)*"
}
if($cert) {
Set-AuthenticodeSignature -Filepath $powerShellFiles -Cert $cert -TimeStampServer $timestampServer -IncludeChain NotRoot -HashAlgorithm SHA256
}
else {
Write-Host "Unable to sign PowerShell files, as unable to locate certificate and/or password."
}
}
function PackDirectory($path, $Version = $version){
exec {
Get-ChildItem $path -Recurse -include *.nuspec |
ForEach-Object {
choco pack $_ --OutputDirectory $path --version $version
}
}
}
# Borrowed from Luis Rocha's Blog (http://www.luisrocha.net/2009/11/setting-assembly-version-with-windows.html)
function Update-AssemblyInfoFiles ([string] $version, [string] $commit) {
$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$fileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyInformationalVersionPattern = 'AssemblyInformationalVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$fileCommitPattern = 'AssemblyTrademark\("([a-f0-9]{40})?"\)'
$assemblyVersion = 'AssemblyVersion("' + $version + '")';
$fileVersion = 'AssemblyFileVersion("' + $version + '")';
$assemblyInformationalVersion = 'AssemblyInformationalVersion("' + $informationalVersion + '")';
$commitVersion = 'AssemblyTrademark("' + $commit + '")';
Get-ChildItem -path $baseDir -r -filter AssemblyInfo.cs | ForEach-Object {
$filename = $_.Directory.ToString() + '\' + $_.Name
$filename + ' -> ' + $version
# If you are using a source control that requires to check-out files before
# modifying them, make sure to check-out the file here.
# For example, TFS will require the following command:
# tf checkout $filename
(Get-Content $filename) | ForEach-Object {
ForEach-Object {$_ -replace $assemblyVersionPattern, $assemblyVersion } |
ForEach-Object {$_ -replace $fileVersionPattern, $fileVersion } |
ForEach-Object {$_ -replace $fileCommitPattern, $commitVersion } |
ForEach-Object {$_ -replace $assemblyInformationalVersionPattern, $assemblyInformationalVersion }
} | Set-Content $filename
}
}