-
Notifications
You must be signed in to change notification settings - Fork 0
/
psake.ps1
323 lines (293 loc) · 11.6 KB
/
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
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
# PSake makes variables declared here available in other scriptblocks
# Init some things
Properties {
# Find the build folder based on build system
$ProjectRoot = $ENV:BHProjectPath
if (-not $ProjectRoot) {
$ProjectRoot = $PSScriptRoot
}
$ModuleFolder = Split-Path -Path $ENV:BHPSModuleManifest -Parent
$PSVersion = $PSVersionTable.PSVersion.Major
$TestFile = "TestResults_PS$PSVersion`_$TimeStamp.xml"
$lines = '----------------------------------------------------------------------'
$Verbose = @{ }
if ($ENV:BHCommitMessage -match "!verbose") {
$Verbose = @{ Verbose = $True }
}
$CurrentVersion = [version](Get-Metadata -Path $env:BHPSModuleManifest)
$StepVersion = [version] (Step-Version $CurrentVersion)
$GalleryVersion = Get-NextPSGalleryVersion -Name $env:BHProjectName
$BuildVersion = $StepVersion
If ($GalleryVersion -gt $StepVersion) {
$BuildVersion = $GalleryVersion
}
$BuildVersion = [version]::New($BuildVersion.Major, $BuildVersion.Minor, $BuildVersion.Build, $env:BHBuildNumber)
$BuildDate = Get-Date -uFormat '%Y-%m-%d'
$ReleaseNotes = "$ProjectRoot\RELEASE.md"
$ChangeLog = "$ProjectRoot\docs\ChangeLog.md"
}
Task Default -Depends PostDeploy
Task Init {
$lines
Set-Location $ProjectRoot
"Build System Details:"
Get-Item ENV:BH* | Format-List
"`n"
"Current Version: $CurrentVersion`n"
"Build Version: $BuildVersion`n"
}
Task UnitTests -Depends Init {
$lines
"Running Pre-build unit tests`n"
$Timestamp = Get-date -uformat "%Y%m%d-%H%M%S"
$TestFile = "TestResults_PS$PSVersion`_$TimeStamp.xml"
$Parameters = @{
Script = "$ProjectRoot\Tests"
PassThru = $true
Tag = 'Unit'
OutputFormat = 'NUnitXml'
OutputFile = "$ProjectRoot\$TestFile"
}
$TestResults =Invoke-Pester @Parameters
if ($TestResults.FailedCount -gt 0) {
Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}
"`n"
If ($ENV:BHBuildSystem -eq 'AppVeyor') {
"Uploading $ProjectRoot\$TestFile to AppVeyor"
"JobID: $env:APPVEYOR_JOB_ID"
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path "$ProjectRoot\$TestFile"))
}
Remove-Item "$ProjectRoot\$TestFile" -Force -ErrorAction SilentlyContinue
}
Task Build -Depends UnitTests {
$lines
"Populating AliasesToExport and FunctionsToExport"
# Load the module, read the exported functions and aliases, update the psd1
$FunctionFiles = Get-ChildItem "$ModuleFolder\Public\*.ps1" |
Where-Object{ $_.name -notmatch 'Tests' }
$ExportFunctions = @()
$ExportAliases = @()
foreach ($FunctionFile in $FunctionFiles) {
$AST = [System.Management.Automation.Language.Parser]::ParseFile($FunctionFile.FullName, [ref]$null, [ref]$null)
$Functions = $AST.FindAll({
$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]
}, $true)
if ($Functions.Name) {
$ExportFunctions += $Functions.Name
}
$Aliases = $AST.FindAll({
$args[0] -is [System.Management.Automation.Language.AttributeAst] -and
$args[0].parent -is [System.Management.Automation.Language.ParamBlockAst] -and
$args[0].TypeName.FullName -eq 'alias'
}, $true)
if ($Aliases.PositionalArguments.value) {
$ExportAliases += $Aliases.PositionalArguments.value
}
}
Set-ModuleFunctions -Name $env:BHPSModuleManifest -FunctionsToExport $ExportFunctions
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName AliasesToExport -Value $ExportAliases
"Populating NestedModules"
# Scan the Public and Private folders and add all Files to NestedModules
# I prefer to populate this instead of dot sourcing from the .psm1
$Parameters = @{
Path = @(
"$ModuleFolder\Public\*.ps1"
"$ModuleFolder\Private\*.ps1"
)
ErrorAction = 'SilentlyContinue'
}
$ExportModules = Get-ChildItem @Parameters |
Where-Object { $_.Name -notmatch '\.tests{0,1}\.ps1' } |
ForEach-Object { $_.fullname.replace("$ModuleFolder\", "") }
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName NestedModules -Value $ExportModules
# Bump the module version
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $BuildVersion
# Update release notes with Version info and set the PSD1 release notes
$parameters = @{
Path = $ReleaseNotes
ErrorAction = 'SilentlyContinue'
}
$ReleaseText = (Get-Content @parameters | Where-Object {$_ -notmatch '^# Version '}) -join "`r`n"
if (-not $ReleaseText) {
"Skipping realse notes`n"
"Consider adding a RELEASE.md to your project.`n"
return
}
$Header = "# Version {0} ({1})`r`n" -f $BuildVersion, $BuildDate
$ReleaseText = $Header + $ReleaseText
$ReleaseText | Set-Content $ReleaseNotes
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName ReleaseNotes -Value $ReleaseText
# Update the ChangeLog with the current release notes
$releaseparameters = @{
Path = $ReleaseNotes
ErrorAction = 'SilentlyContinue'
}
$changeparameters = @{
Path = $ChangeLog
ErrorAction = 'SilentlyContinue'
}
(Get-Content @releaseparameters),"`r`n`r`n", (Get-Content @changeparameters) | Set-Content $ChangeLog
}
Task Test -Depends Build {
$lines
"`n`tSTATUS: Testing with PowerShell $PSVersion"
# Gather test results. Store them in a variable and file
$Timestamp = Get-date -uformat "%Y%m%d-%H%M%S"
$TestFile = "TestResults_PS$PSVersion`_$TimeStamp.xml"
$parameters = @{
Script = "$ProjectRoot\Tests"
PassThru = $true
OutputFormat = 'NUnitXml'
OutputFile = "$ProjectRoot\$TestFile"
}
$TestResults = Invoke-Pester @parameters
# In Appveyor? Upload our tests! #Abstract this into a function?
If ($ENV:BHBuildSystem -eq 'AppVeyor') {
"Uploading $ProjectRoot\$TestFile to AppVeyor"
"JobID: $env:APPVEYOR_JOB_ID"
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path "$ProjectRoot\$TestFile"))
}
Remove-Item "$ProjectRoot\$TestFile" -Force -ErrorAction SilentlyContinue
# Failed tests?
# Need to tell psake or it will proceed to the deployment. Danger!
if ($TestResults.FailedCount -gt 0) {
Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}
"`n"
}
Task BuildDocs -depends Test {
$lines
"Loading Module from $ENV:BHPSModuleManifest"
Remove-Module $ENV:BHProjectName -Force -ea SilentlyContinue
# platyPS + AppVeyor requires the module to be loaded in Global scope
Import-Module $ENV:BHPSModuleManifest -force -Global
#Build YAMLText starting with the header
$YMLtext = (Get-Content "$ProjectRoot\header-mkdocs.yml") -join "`n"
$YMLtext = "$YMLtext`n"
$parameters = @{
Path = $ReleaseNotes
ErrorAction = 'SilentlyContinue'
}
$ReleaseText = (Get-Content @parameters) -join "`n"
if ($ReleaseText) {
$ReleaseText | Set-Content "$ProjectRoot\docs\RELEASE.md"
$YMLText = "$YMLtext - Realse Notes: RELEASE.md`n"
}
if ((Test-Path -Path $ChangeLog)) {
$YMLText = "$YMLtext - Change Log: ChangeLog.md`n"
}
$YMLText = "$YMLtext - Functions:`n"
# Drain the swamp
$parameters = @{
Recurse = $true
Force = $true
Path = "$ProjectRoot\docs\functions"
ErrorAction = 'SilentlyContinue'
}
$null = Remove-Item @parameters
$Params = @{
Path = "$ProjectRoot\docs\functions"
type = 'directory'
ErrorAction = 'SilentlyContinue'
}
$null = New-Item @Params
$Params = @{
Module = $ENV:BHProjectName
Force = $true
OutputFolder = "$ProjectRoot\docs\functions"
NoMetadata = $true
}
New-MarkdownHelp @Params | foreach-object {
$Function = $_.Name -replace '\.md', ''
$Part = " - {0}: functions/{1}" -f $Function, $_.Name
$YMLText = "{0}{1}`n" -f $YMLText, $Part
$Part
}
$YMLtext | Set-Content -Path "$ProjectRoot\mkdocs.yml"
}
Task Deploy -Depends BuildDocs {
$lines
# Gate deployment
if (
$ENV:BHBuildSystem -ne 'Unknown' -and
$ENV:BHBranchName -eq "master" -and
$ENV:BHCommitMessage -match '!deploy'
) {
$Params = @{
Path = $ProjectRoot
Force = $true
}
Invoke-PSDeploy @Verbose @Params
}
else {
"Skipping deployment: To deploy, ensure that...`n" +
"`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" +
"`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" +
"`t* Your commit message includes !deploy (Current: $ENV:BHCommitMessage)"
}
}
Task PostDeploy -depends Deploy {
$lines
if ($ENV:APPVEYOR_REPO_PROVIDER -notlike 'github') {
"Repo provider '$ENV:APPVEYOR_REPO_PROVIDER'. Skipping PostDeploy"
return
}
If ($ENV:BHBuildSystem -eq 'AppVeyor') {
"git config --global credential.helper store"
cmd /c "git config --global credential.helper store 2>&1"
Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:access_token):[email protected]`n"
"git config --global user.email"
cmd /c "git config --global user.email ""$($ENV:BHProjectName)-$($ENV:BHBranchName)-$($ENV:BHBuildSystem)@markekraus.com"" 2>&1"
"git config --global user.name"
cmd /c "git config --global user.name ""AppVeyor"" 2>&1"
"git config --global core.autocrlf true"
cmd /c "git config --global core.autocrlf true 2>&1"
}
"git checkout $ENV:BHBranchName"
cmd /c "git checkout $ENV:BHBranchName 2>&1"
"git add -A"
cmd /c "git add -A 2>&1"
"git commit -m"
cmd /c "git commit -m ""AppVeyor post-build commit[ci skip]"" 2>&1"
"git status"
cmd /c "git status 2>&1"
"git push origin $ENV:BHBranchName"
cmd /c "git push origin $ENV:BHBranchName 2>&1"
# if this is a !deploy on master, create GitHub release
if (
$ENV:BHBuildSystem -ne 'Unknown' -and
$ENV:BHBranchName -eq "master" -and
$ENV:BHCommitMessage -match '!deploy'
) {
"Publishing Release 'v$BuildVersion' to Github"
$parameters = @{
Path = $ReleaseNotes
ErrorAction = 'SilentlyContinue'
}
$ReleaseText = (Get-Content @parameters) -join "`r`n"
if (-not $ReleaseText) {
$ReleaseText = "Release version $BuildVersion ($BuildDate)"
}
$Body = @{
"tag_name" = "v$BuildVersion"
"target_commitish"= "master"
"name" = "v$BuildVersion"
"body"= $ReleaseText
"draft" = $false
"prerelease"= $false
} | ConvertTo-Json
$releaseParams = @{
Uri = "https://api.github.com/repos/{0}/releases" -f $ENV:APPVEYOR_REPO_NAME
Method = 'POST'
Headers = @{
Authorization = 'Basic ' + [Convert]::ToBase64String(
[Text.Encoding]::ASCII.GetBytes($env:access_token + ":x-oauth-basic"));
}
ContentType = 'application/json'
Body = $Body
}
$Response = Invoke-RestMethod @releaseParams
$Response | Format-List *
}
}