-
Notifications
You must be signed in to change notification settings - Fork 2
/
invoke.build.ps1
203 lines (171 loc) · 8.22 KB
/
invoke.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
<#
.SYNOPSIS
Build script which leverages the InvokeBuild module.
.DESCRIPTION
Build script which leverages the InvokeBuild module.
This build script is used in the build pipeline and local development for building this project.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$ModuleName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$Author,
[Parameter()]
[String]$Version,
[Parameter()]
[Bool]$NewRelease = $false,
[Parameter()]
[Bool]$UpdateDocs = $false
)
# Synopsis: Initiate the build process
task . ImportBuildModule,
InitaliseBuildDirectory,
CopyChangeLog,
UpdateChangeLog,
CreateRootModule,
CreateProcessScript,
UpdateModuleManifest,
CreateArchive,
UpdateDocs,
UpdateProjectRepo
# Synopsis: Install dependent build modules
task InstallDependencies {
$Modules = "PlatyPS","ChangelogManagement"
if ($Script:ModuleName -ne "codaamok.build") {
$Modules += "codaamok.build"
}
Install-Module -Name $Modules -Scope CurrentUser
}
# Synopsis: Set build platform specific environment variables
task SetGitHubActionEnvironmentVariables {
New-BuildEnvironmentVariable -Platform "GitHubActions" -Variable @{
"GH_USERNAME" = $Author
"GH_PROJECTNAME" = $ModuleName
}
}
# Synopsis: Publish module to the PowerShell Gallery
task PublishModule {
Publish-Module -Path $BuildRoot\build\$ModuleName -NuGetApiKey $env:PSGALLERY_API_KEY -ErrorAction "Stop" -Force
}
# Synopsis: Import the codaamok.build module
task ImportBuildModule {
if ($Script:ModuleName -eq "codaamok.build") {
# This is to use module for building codaamok.build itself
Import-Module .\src\codaamok.build.psd1
}
else {
Import-Module "codaamok.build"
}
}
# Synopsis: Create fresh build directories and initalise it with content from the project
task InitaliseBuildDirectory {
Invoke-BuildClean -Path @(
"{0}\build" -f $BuildRoot
"{0}\build\{1}" -f $BuildRoot, $Script:ModuleName
"{0}\release" -f $BuildRoot
)
if (Test-Path -Path $BuildRoot\src\* -Include "*format.ps1xml") {
$Script:FormatFiles = Copy-Item -Path $BuildRoot\src\* -Destination $BuildRoot\build\$Script:ModuleName -Filter "*format.ps1xml" -PassThru
}
if (Test-Path -Path $BuildRoot\src\Files\*) {
$Script:FileList = Copy-Item -Path $BuildRoot\src\Files\* -Destination $BuildRoot\build\$Script:ModuleName -Recurse -Force -PassThru
}
Copy-Item -Path $BuildRoot\LICENSE -Destination $BuildRoot\build\$Script:ModuleName\LICENSE
Copy-Item -Path $BuildRoot\src\en-US -Destination $BuildRoot\build\$Script:ModuleName -Recurse
$Script:ManifestFile = Copy-Item -Path $BuildRoot\src\$Script:ModuleName.psd1 -Destination $BuildRoot\build\$Script:ModuleName\$Script:ModuleName.psd1 -PassThru
}
# Synopsis: Get change log data, copy it to the build directory, and create releasenotes.txt
task CopyChangeLog {
Copy-Item -Path $BuildRoot\CHANGELOG.md -Destination $BuildRoot\build\$Script:ModuleName\CHANGELOG.md
$Script:ChangeLogData = Get-ChangeLogData -Path $BuildRoot\CHANGELOG.md
Export-UnreleasedNotes -Path $BuildRoot\release\releasenotes.txt -ChangeLogData $Script:ChangeLogData -NewRelease $Script:NewRelease
}
# Synopsis: Update CHANGELOG.md (if building a new release with -NewRelease)
task UpdateChangeLog -If ($Script:NewRelease) {
$LinkPattern = @{
FirstRelease = "https://github.com/{0}/{1}/tree/{{CUR}}" -f $Script:Author, $Script:ModuleName
NormalRelease = "https://github.com/{0}/{1}/compare/{{PREV}}..{{CUR}}" -f $Script:Author, $Script:ModuleName
Unreleased = "https://github.com/{0}/{1}/compare/{{CUR}}..HEAD" -f $Script:Author, $Script:ModuleName
}
Update-Changelog -Path $BuildRoot\build\$Script:ModuleName\CHANGELOG.md -ReleaseVersion $Script:Version -LinkMode Automatic -LinkPattern $LinkPattern
}
# Synopsis: Creates a single .psm1 file of all private and public functions of the to-be-built module
task CreateRootModule {
$Script:RootModule = "{0}\build\{1}\{1}.psm1" -f $BuildRoot, $Script:ModuleName
$DevModulePath = "{0}\src" -f $BuildRoot
Export-RootModule -DevModulePath $DevModulePath -RootModule $Script:RootModule
}
# Synopsis: Create a single Process.ps1 script file for all script files under ScriptsToProcess\* (if any)
$Params = @{
Path = "{0}\src\ScriptsToProcess\*" -f $BuildRoot
Include = "*.ps1"
}
task CreateProcessScript -If (Test-Path @Params) {
$Path = "{0}\build\{1}\Process.ps1" -f $BuildRoot, $Script:ModuleName
Export-ScriptsToProcess -File (Get-ChildItem @Params) -Path $Path
$Script:ProcessScript = $true
}
# Synopsis: Update the module manifest in the build directory
task UpdateModuleManifest {
$UpdateModuleManifestSplat = @{
Path = $Script:ManifestFile
RootModule = (Split-Path $Script:RootModule -Leaf)
FunctionsToExport = Get-PublicFunctions -Path $BuildRoot\src\Public
ReleaseNotes = (Get-Content $BuildRoot\release\releasenotes.txt) -replace '`'
}
# Build with pre-release data from the branch if the -Version parameter is not passed (for local development and testing)
if ($Script:Version) {
$UpdateModuleManifestSplat["ModuleVersion"] = $Script:Version
}
else {
$GitVersion = (gitversion | ConvertFrom-Json)
$UpdateModuleManifestSplat["ModuleVersion"] = $GitVersion.MajorMinorPatch
$UpdateModuleManifestSplat["Prerelease"] = $GitVersion.NuGetPreReleaseTag
}
if ($Script:FormatFiles) {
$UpdateModuleManifestSplat["FormatsToProcess"] = $Script:FormatFiles.Name
}
if ($Script:FileList) {
# Use this instead of Updatet-ModuleManifest due to https://github.com/PowerShell/PowerShellGet/issues/196
$Regex = '^# FileList = @\(\)$'
$ReplaceStr = 'FileList = "{0}"' -f [String]::Join('", "', $Script:FileList.Name)
(Get-Content -Path $Script:ManifestFile.FullName) -replace $Regex, $ReplaceStr | Set-Content -Path $Script:ManifestFile
}
if ($Script:ProcessScript) {
# Use this instead of Updatet-ModuleManifest due to https://github.com/PowerShell/PowerShellGet/issues/196
$Regex = '(#? ?ScriptsToProcess.+)'
$ReplaceStr = 'ScriptsToProcess = "Process.ps1"'
(Get-Content -Path $Script:ManifestFile.FullName) -replace $Regex, $ReplaceStr | Set-Content -Path $Script:ManifestFile
}
Update-ModuleManifest @UpdateModuleManifestSplat
# Arguably a moot point as Update-MooduleManifest obviously does some testing to ensure a valid manifest is there first before updating it
# However with the regex replace for ScriptsToProcess, I want to be sure
$null = Test-ModuleManifest -Path $Script:ManifestFile
}
# Synopsis: Create archive of the module
task CreateArchive {
$ReleaseAsset = "{0}_{1}.zip" -f $Script:ModuleName, $Script:Version
Compress-Archive -Path $BuildRoot\build\$Script:ModuleName\* -DestinationPath $BuildRoot\release\$ReleaseAsset -Force
}
# Synopsis: Update documentation (-NewRelease or -UpdateDocs switch parameter)
task UpdateDocs -If ($NewRelease -Or $UpdateDocs) {
Import-Module -Name $BuildRoot\build\$Script:ModuleName -Force
New-MarkdownHelp -Module $Script:ModuleName -OutputFolder $BuildRoot\docs -Force
}
# Synopsis: Update the project's repository with files updated by the pipeline e.g. module manifest
task UpdateProjectRepo -If ($NewRelease) {
Copy-Item -Path $BuildRoot\build\$Script:ModuleName\CHANGELOG.md -Destination $BuildRoot\CHANGELOG.md
$ManifestData = Import-PowerShellDataFile -Path $Script:ManifestFile
# Instead of copying the manifest from the .\build directory, update it in place
# This enables me to keep FunctionsToExport = '*' for development. Therefore instead only update the important bits e.g. version and release notes
$UpdateModuleManifestSplat = @{
Path = "{0}\src\{1}.psd1" -f $BuildRoot, $Script:ModuleName
ModuleVersion = $ManifestData.ModuleVersion
ReleaseNotes = $ManifestData.PrivateData.PSData.ReleaseNotes
}
Update-ModuleManifest @UpdateModuleManifestSplat
$null = Test-ModuleManifest -Path ("{0}\src\{1}.psd1" -f $BuildRoot, $Script:ModuleName)
}