forked from PlagueHO/CosmosDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsakefile.ps1
350 lines (301 loc) · 12.9 KB
/
psakefile.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
# PSake makes variables declared here available in other scriptblocks
# Init some things
Properties {
# Prepare the folder variables
$ProjectRoot = $ENV:BHProjectPath
if (-not $ProjectRoot)
{
$ProjectRoot = $PSScriptRoot
}
# Determine the folder names for staging the module
$StagingFolder = Join-Path -Path $ProjectRoot -ChildPath 'staging'
$ModuleFolder = Join-Path -Path $StagingFolder -ChildPath 'CosmosDB'
$Timestamp = Get-Date -uformat "%Y%m%d-%H%M%S"
$PSVersion = $PSVersionTable.PSVersion.Major
$separator = '----------------------------------------------------------------------'
}
Task Default -Depends Build
Task Init {
$separator
Set-Location -Path $ProjectRoot
'Build System Details:'
Get-Item -Path ENV:BH*
"`n"
'PowerShell Details:'
$PSVersionTable
"`n"
}
Task Test -Depends Init {
$separator
# Execute tests
$testResultsFile = Join-Path -Path $ProjectRoot -ChildPath 'test\TestsResults.xml'
$testResults = Invoke-Pester `
-OutputFormat NUnitXml `
-OutputFile $testResultsFile `
-PassThru `
-ExcludeTag Incomplete `
-CodeCoverage @( Join-Path -Path $ProjectRoot -ChildPath 'src\lib\*.ps1' )
# Prepare and uploade code coverage
if ($testResults.CodeCoverage)
{
'Preparing CodeCoverage'
Import-Module `
-Name (Join-Path -Path $ProjectRoot -ChildPath '.codecovio\CodeCovio.psm1')
$jsonPath = Export-CodeCovIoJson `
-CodeCoverage $testResults.CodeCoverage `
-RepoRoot $ProjectRoot
if ($ENV:BHBuildSystem -eq 'AppVeyor')
{
'Uploading CodeCoverage to CodeCov.io'
try
{
Invoke-UploadCoveCoveIoReport -Path $jsonPath
}
catch
{
# CodeCov currently reports an error when uploading
# This is not fatal and can be ignored
Write-Warning -Message $_
}
}
}
else
{
Write-Warning -Message 'Could not create CodeCov.io report because pester results object did not contain a CodeCoverage object'
}
# Upload tests
if ($ENV:BHBuildSystem -eq 'AppVeyor')
{
'Publishing test results to AppVeyor'
(New-Object 'System.Net.WebClient').UploadFile(
"https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)",
(Resolve-Path $testResultsFile))
"Publishing test results to AppVeyor as Artifact"
Push-AppveyorArtifact $testResultsFile
if ($testResults.FailedCount -gt 0)
{
throw "$($testResults.FailedCount) tests failed."
}
}
else
{
if ($testResults.FailedCount -gt 0)
{
Write-Error -Exception "$($testResults.FailedCount) tests failed."
}
}
"`n"
}
Task Build -Depends Test {
$separator
# Generate the next version by adding the build system build number to the manifest version
$manifestPath = Join-Path -Path $ProjectRoot -ChildPath 'src/CosmosDB.psd1'
$newVersion = Get-NewVersionNumber `
-ManifestPath $manifestPath `
-Build $ENV:BHBuildNumber
if ($ENV:BHBuildSystem -eq 'AppVeyor')
{
# Update AppVeyor build version number
Update-AppveyorBuild -Version $newVersion
}
# Determine the folder names for staging the module
$VersionFolder = Join-Path -Path $ModuleFolder -ChildPath $newVersion
# Stage the module
$null = New-Item -Path $StagingFolder -Type directory -ErrorAction SilentlyContinue
$null = New-Item -Path $ModuleFolder -Type directory -ErrorAction SilentlyContinue
Remove-Item -Path $VersionFolder -Recurse -Force -ErrorAction SilentlyContinue
$null = New-Item -Path $VersionFolder -Type directory
# Populate Version Folder
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'src/CosmosDB.psd1') -Destination $VersionFolder
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'src/CosmosDB.psm1') -Destination $VersionFolder
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'src/lib') -Destination $VersionFolder -Recurse
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'src/formats') -Destination $VersionFolder -Recurse
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'src/types') -Destination $VersionFolder -Recurse
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'src/en-us') -Destination $VersionFolder -Recurse
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'LICENSE') -Destination $VersionFolder
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'README.md') -Destination $VersionFolder
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'CHANGELOG.md') -Destination $VersionFolder
$null = Copy-Item -Path (Join-Path -Path $ProjectRoot -ChildPath 'RELEASENOTES.md') -Destination $VersionFolder
# Prepare external help
'Building external help file'
New-ExternalHelp `
-Path (Join-Path -Path $ProjectRoot -ChildPath 'docs\') `
-OutputPath $VersionFolder `
-Force
# Set the new version number in the staged Module Manifest
'Updating module manifest'
$stagedManifestPath = Join-Path -Path $VersionFolder -ChildPath 'CosmosDB.psd1'
$stagedManifestContent = Get-Content -Path $stagedManifestPath -Raw
$stagedManifestContent = $stagedManifestContent -replace '(?<=ModuleVersion\s+=\s+'')(?<ModuleVersion>.*)(?='')', $newVersion
$stagedManifestContent = $stagedManifestContent -replace '## What is New in CosmosDB Unreleased', "## What is New in CosmosDB $newVersion"
Set-Content -Path $stagedManifestPath -Value $stagedManifestContent -NoNewLine -Force
# Set the new version number in the staged CHANGELOG.md
'Updating CHANGELOG.MD'
$stagedChangeLogPath = Join-Path -Path $VersionFolder -ChildPath 'CHANGELOG.md'
$stagedChangeLogContent = Get-Content -Path $stagedChangeLogPath -Raw
$stagedChangeLogContent = $stagedChangeLogContent -replace '# Unreleased', "# $newVersion"
Set-Content -Path $stagedChangeLogPath -Value $stagedChangeLogContent -NoNewLine -Force
# Set the new version number in the staged RELEASENOTES.md
'Updating RELEASENOTES.MD'
$stagedReleaseNotesPath = Join-Path -Path $VersionFolder -ChildPath 'RELEASENOTES.md'
$stagedReleaseNotesContent = Get-Content -Path $stagedReleaseNotesPath -Raw
$stagedReleaseNotesContent = $stagedReleaseNotesContent -replace '## What is New in CosmosDB Unreleased', "## What is New in CosmosDB $newVersion"
Set-Content -Path $stagedReleaseNotesPath -Value $stagedReleaseNotesContent -NoNewLine -Force
"`n"
}
Task Deploy -Depends Build {
$separator
# Generate the next version by adding the build system build number to the manifest version
$manifestPath = Join-Path -Path $ProjectRoot -ChildPath 'src/CosmosDB.psd1'
$newVersion = Get-NewVersionNumber `
-ManifestPath $manifestPath `
-Build $ENV:BHBuildNumber
# Determine the folder names for staging the module
$VersionFolder = Join-Path -Path $ModuleFolder -ChildPath $newVersion
# Copy the module to the PSModulePath
$PSModulePath = ($ENV:PSModulePath -split ';')[0]
"Copying Module to $PSModulePath"
Copy-Item `
-Path $ModuleFolder `
-Destination $PSModulePath `
-Recurse `
-Force
# Create zip artifact
$zipFilePath = Join-Path `
-Path $StagingFolder `
-ChildPath "${ENV:BHProjectName}_${ENV:BHBuildNumber}.zip"
$null = Add-Type -assemblyname System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($ModuleFolder, $zipFilePath)
if ($ENV:BHBuildSystem -eq 'AppVeyor')
{
# If AppVeyor, publish the deploy artefacts for debug purposes
"Pushing package $zipFilePath as Appveyor artifact"
Push-AppveyorArtifact $zipFilePath
Remove-Item -Path $zipFilePath -Force
<#
If this is a build of the Master branch and not a PR push
then publish the Module to the PowerShell Gallery.
#>
if ($ENV:BHBranchName -eq 'master')
{
$commitMessage = $ENV:BHCommitMessage.TrimEnd()
"Commit to Master branch detected with commit message: '$commitMessage'"
if ($commitMessage -match ' Deploy!$')
{
# This was a deploy commit so no need to do anything
'Skipping deployment because this was a commit triggered by a deployment'
}
elseif ($ENV:APPVEYOR_PULL_REQUEST_NUMBER)
{
# This is a PR so do nothing
'Skipping deployment because this is a Pull Request'
}
else
{
# This is a commit to Master
'Publishing Module to PowerShell Gallery'
Get-PackageProvider `
-Name NuGet `
-ForceBootstrap
Publish-Module `
-Name 'CosmosDB' `
-RequiredVersion $newVersion `
-NuGetApiKey $ENV:PowerShellGalleryApiKey `
-Confirm:$false
# This is not a PR so deploy
'Beginning update to master branch with deployed information'
# Pull the master branch, update the readme.md and manifest
Set-Location -Path $ProjectRoot
Invoke-Git -GitParameters @('config', '--global', 'credential.helper', 'store')
Add-Content `
-Path "$env:USERPROFILE\.git-credentials" `
-Value "https://$($env:GitHubPushFromPlagueHO):[email protected]`n"
Invoke-Git -GitParameters @('config', '--global', 'user.email', '[email protected]')
Invoke-Git -GitParameters @('config', '--global', 'user.name', 'Daniel Scott-Raynsford')
Invoke-Git -GitParameters @('checkout', '-f', 'master')
# Replace the manifest with the one that was published
'Updating files changed during deployment.='
Copy-Item `
-Path (Join-Path -Path $VersionFolder -ChildPath 'CosmosDB.psd1') `
-Destination (Join-Path -Path $ProjectRoot -ChildPath 'src') `
-Force
Copy-Item `
-Path (Join-Path -Path $VersionFolder -ChildPath 'CHANGELOG.MD') `
-Destination $ProjectRoot `
-Force
Copy-Item `
-Path (Join-Path -Path $VersionFolder -ChildPath 'RELEASENOTES.MD') `
-Destination $ProjectRoot `
-Force
# Update the master branch
'Pushing deployment changes to Master'
Invoke-Git -GitParameters @('add', '.')
Invoke-Git -GitParameters @('commit', '-m', "$NewVersion Deploy!")
Invoke-Git -GitParameters @('status')
Invoke-Git -GitParameters @('push', 'origin', 'master')
# Create the version tag and push it
"Pushing $newVersion tag to Master"
Invoke-Git -GitParameters @('tag', '-a', $newVersion, '-m', $newVersion)
Invoke-Git -GitParameters @('push', 'origin', $newVersion)
# Merge the changes to the Dev branch as well
'Pushing deployment changes to Dev'
Invoke-Git -GitParameters @('checkout', '-f', 'dev')
Invoke-Git -GitParameters @('merge', 'master')
Invoke-Git -GitParameters @('push', 'origin', 'dev')
}
}
}
}
function Get-NewVersionNumber
{
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ManifestPath,
[Parameter(Mandatory = $true)]
[System.String]
$Build
)
# Get version number from the existing manifest
$manifestContent = Get-Content -Path $ManifestPath -Raw
$regex = '(?<=ModuleVersion\s+=\s+'')(?<ModuleVersion>.*)(?='')'
$matches = @([regex]::matches($manifestContent, $regex, 'IgnoreCase'))
$version = $null
if ($matches)
{
$version = $matches[0].Value
}
# Determine the new version number
$versionArray = $version -split '\.'
$newVersion = ''
Foreach ($ver in (0..2))
{
$sem = $versionArray[$ver]
if ([String]::IsNullOrEmpty($sem))
{
$sem = '0'
}
$newVersion += "$sem."
}
$newVersion += $Build
return $newVersion
}
function Invoke-Git
{
param
(
[Parameter(Mandatory = $true)]
[System.String[]]
$GitParameters
)
try
{
"Executing 'git $($GitParameters -join ' ')'"
exec { & git $GitParameters }
}
catch
{
Write-Warning -Message $_
}
}