-
Notifications
You must be signed in to change notification settings - Fork 50
/
build.psake.ps1
164 lines (124 loc) · 6.22 KB
/
build.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
function Update-AdditionalReleaseArtifact {
param(
[string] $Version,
[string] $CommitDate
)
$PSVersion = $Version -replace '^(\d+\.\d+\.\d+).*$', '$1'
Write-Host ('Updating Module Manifest version number to: {0}' -f $PSVersion)
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $PSVersion
Write-Host 'Getting release notes'
$ReleaseDescription = (gc $ReleaseFile) -join "`r`n"
Clear-Content -Path $ReleaseFile
if ($env:APPVEYOR) {
Set-AppveyorBuildVariable -Name ReleaseDescription -Value $ReleaseDescription
}
$Changelog = (gc $ChangelogFile | select -Skip 2) -join "`r`n"
"# CHANGELOG`r`n" | Out-File $ChangelogTemp -Encoding ascii
("## {0} ({1})`r`n" -f $Version, $CommitDate) | Out-File $ChangelogTemp -Append -Encoding ascii
("{0}`r`n" -f $ReleaseDescription) | Out-File $ChangelogTemp -Append -Encoding ascii
("{0}`r`n" -f $Changelog) | Out-File $ChangelogTemp -Append -Encoding ascii
Copy-Item $ChangelogTemp $ChangelogFile -Force
}
Properties {
$BranchName = $env:GitVersion_BranchName
$SemVer = $env:GitVersion_SemVer
$StableVersion = $env:GitVersion_MajorMinorPatch
$TestsFolder = '.\Tests'
$TestsFile = Join-Path $env:BHBuildOutput ('tests-{0}-{1}.xml' -f $env:GitVersion_ShortSha, $SemVer)
$Artifact = '{0}-{1}.zip' -f $env:BHProjectName.ToLower(), $SemVer
$BuildBaseModule = Join-Path $env:BHBuildOutput $env:BHProjectName
$BuildVersionedModule = Join-Path $BuildBaseModule $StableVersion
$ArtifactPath = Join-Path $env:BHBuildOutput $Artifact
$ReleaseFile = Join-Path $env:BHProjectPath 'docs\RELEASE.md'
$ChangelogFile = Join-Path $env:BHProjectPath 'docs\CHANGELOG.md'
$ChangelogTemp = Join-Path $env:BHBuildOutput 'CHANGELOG.md.tmp'
Import-Module $env:BHPSModuleManifest -Global
$ExportedFunctions = Get-Command -Module $env:BHProjectName | select -ExpandProperty Name
Remove-Module $env:BHProjectName -Force
}
FormatTaskName (('-' * 25) + ('[ {0,-28} ]') + ('-' * 25))
Task Default -Depends Tests, Build, PublishModule
Task Init {
Set-Location $env:BHProjectPath
if (Test-Path $env:BHBuildOutput) {
Remove-Item $env:BHBuildOutput -Force -Recurse
}
New-Item -Path $env:BHBuildOutput -ItemType Directory | Out-Null
Write-Host ('Working folder: {0}' -f $PWD)
Write-Host ('Build output: {0}' -f $env:BHBuildOutput)
Write-Host ('Git Version: {0}' -f $SemVer)
Write-Host ('Git Version (Stable): {0}' -f $StableVersion)
Write-Host ('Git Branch: {0}' -f $BranchName)
if ($env:APPVEYOR) {
Set-AppveyorBuildVariable -Name 'ReleaseVersion' -Value $SemVer
}
$PendingChanges = git status --porcelain
if ($null -ne $PendingChanges) {
throw 'You have pending changes, aborting release'
}
}
Task CodeAnalisys -Depends Init {
Write-Host 'ScriptAnalyzer: Running'
Invoke-ScriptAnalyzer -Path $env:BHModulePath -Recurse -Severity Warning
}
Task Tests -Depends CodeAnalisys {
$TestResults = Invoke-Pester -Path $TestsFolder -PassThru -OutputFormat NUnitXml -OutputFile $TestsFile
switch ($env:BHBuildSystem) {
'AppVeyor' {
Get-ChildItem -Path $env:BHBuildOutput -Filter 'tests-*.xml' -File | ForEach-Object {
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", "$($_.FullName)")
}
}
Default {
Write-Warning "Publish test result not implemented for build system '$($ENV:BHBuildSystem)'"
}
}
if ($TestResults.FailedCount -gt 0) {
Write-Error "Build failed. [$($TestResults.FailedCount) Errors]"
}
}
Task BuildDocs -Depends Tests -precondition {$BranchName -eq 'master'} {
Import-Module $env:BHPSModuleManifest -Global
Write-Host 'BuildDocs: Generating Help for exported functions'
New-MarkdownHelp -Module $env:BHProjectName -OutputFolder .\docs\functions -Force
Copy-Item -Path .\header-mkdocs.yml -Destination mkdocs.yml -Force
$ExportedFunctions | %{
(" - {0}: {0}.md" -f $_) | Out-File .\mkdocs.yml -Append -Encoding ascii
}
Remove-Module $env:BHProjectName -Force
}
Task IncrementVersion -Depends BuildDocs -precondition {$BranchName -eq 'master'} {
Update-AdditionalReleaseArtifact -Version $SemVer -CommitDate $env:GitVersion_CommitDate
Exec {git config --global credential.helper store}
Add-Content "$HOME\.git-credentials" "https://$($env:APPVEYOR_PERSONAL_ACCESS_TOKEN):[email protected]`n"
Exec {git config --global user.email "$env:APPVEYOR_GITHUB_EMAIL"}
Exec {git config --global user.name "$env:APPVEYOR_GITHUB_USERNAME"}
Write-Host 'Git: Committing updated docs'
Exec {git add --all}
Exec {git commit -am "Updated docs [skip ci]" --allow-empty}
Write-Host 'Git: Tagging branch'
Exec {git tag $SemVer}
if ($LASTEXITCODE -ne 0) {
Exec {git reset --hard HEAD^}
throw 'No changes detected since last release'
}
Write-Host 'Git: Pushing tags to origin'
Exec {git push -q origin $BranchName --tags}
}
Task Build -Depends IncrementVersion -precondition {$BranchName -eq 'master'} {
if (-not (Test-Path $BuildBaseModule)) {New-Item -Path $BuildBaseModule -ItemType Directory | Out-Null}
if (-not (Test-Path $BuildVersionedModule)) {New-Item -Path $BuildVersionedModule -ItemType Directory | Out-Null}
Write-Host "Build: Copying module to $ArtifactFolder"
Copy-Item -Path $env:BHModulePath\* -Destination $BuildVersionedModule -Recurse
# Write-Host "Build: Generating catalog file"
# $CatalogFilePath = '{0}\{1}.cat' -f $BuildVersionedModule, $env:BHProjectName
# New-FileCatalog -CatalogVersion 2 -CatalogFilePath $CatalogFilePath -Path $env:BHModulePath
Write-Host "Build: Compressing release to $ArtifactPath"
Compress-Archive -Path $BuildBaseModule -DestinationPath $ArtifactPath
Write-Host "Build: Pushing release to Appveyor"
Push-AppveyorArtifact -Path $ArtifactPath
}
Task PublishModule -Depends Build -precondition {$BranchName -eq 'master'} {
Write-Host "PublishModule: Publishing module to powershellgallery"
Publish-Module -Path $BuildVersionedModule -NuGetApiKey $env:APPVEYOR_NUGET_API_KEY
}