-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
HaloAPI.build.ps1
280 lines (261 loc) · 13.7 KB
/
HaloAPI.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
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
<#
.SYNOPSIS
Homotechsual portable module build script.
#>
[CmdletBinding()]
Param (
[String]$Configuration = 'Development',
[String[]]$Remotes = @('homotechsual'),
[Switch]$Push,
[Switch]$UpdateHelp,
[System.IO.DirectoryInfo]$DocusaurusPath,
[Switch]$ForceUpdateCategoryFiles,
[Switch]$CopyModuleFiles,
[Switch]$Test,
[Switch]$UpdateManifest,
[Switch]$PublishModule,
[Switch]$Clean
)
$ModuleName = 'HaloAPI'
# Install required modules
if (-Not(Get-Module -Name 'Install-RequiredModule')) {
Install-Script -Name 'Install-RequiredModule' -Force -Scope CurrentUser
}
Install-RequiredModule -RequiredModulesFile ('{0}\RequiredModules.psd1' -f $PSScriptRoot) -Scope CurrentUser -TrustRegisteredRepositories -Import -Quiet
Import-Module 'R:\Development\Docusaurus.PowerShell\Output\Alt3.Docusaurus.PowerShell\1.0.34\Alt3.Docusaurus.PowerShell.psd1' -Force
# Use strict mode when building.
Set-StrictMode -Version Latest
if ($Push) {
# Push to remote repositories.
foreach ($Remote in $Remotes) {
Start-Process -FilePath 'git' -ArgumentList @('push', $Remote) -Wait -NoNewWindow
Start-Process -FilePath 'git' -ArgumentList @('push', $Remote, '--tags') -Wait -NoNewWindow
}
}
# Update the PowerShell Module Help Files.
## Requires PlatyPS, Pester, PSScriptAnalyzer and Alt3.Docusaurus.PowerShell installed.
if ($UpdateHelp) {
$DocsFolderPath = Join-Path -Path $DocusaurusPath -ChildPath 'docs' -AdditionalChildPath $Script:ModuleName
if (-Not(Test-Path -Path $DocsFolderPath)) {
New-Item -Path $DocsFolderPath -ItemType Directory | Out-Null
}
$MarkdownHeader = @'
:::powershell[Generated Cmdlet Help]
This page has been generated from the {0} PowerShell module source. To make changes please edit the appropriate PowerShell source file.
:::
'@ -f $Script:ModuleName
$ExcludeFiles = Get-ChildItem -Path "$($PSScriptRoot)\Private" -Filter '*.ps1' -Recurse | ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension($_.FullName) }
$NewDocusaurusHelpParams = @{
Module = ('.\{0}.psd1' -f $Script:ModuleName)
DocsFolder = $DocsFolderPath
Exclude = $ExcludeFiles
Sidebar = 'commandlets'
# MetaDescription = 'Generated cmdlet help for the %1 commandlet.'
GroupByVerb = $true
UseDescriptionFromHelp = $true
NoPlaceHolderExamples = $true
UseCustomShortTitles = $false
PrependMarkdown = $MarkdownHeader
RemoveParameters = @('-ProgressAction', '-FakeParam')
}
New-DocusaurusHelp @NewDocusaurusHelpParams | Out-Null
$CommandletDocsFolder = Join-Path -Path $DocusaurusPath -ChildPath 'docs' -AdditionalChildPath @($ModuleName, 'commandlets')
$VerbFolders = Get-ChildItem -Path $CommandletDocsFolder -Directory
$CategoryFileBase = @{
position = 1
collapsible = $true
collapsed = $true
link = @{
type = 'generated-index'
}
customProps = @{
description = ''
}
}
foreach ($VerbFolder in $VerbFolders) {
$HasCategoryFile = Get-ChildItem -Path $VerbFolder.FullName -Filter '_category_.*' -File -ErrorAction SilentlyContinue
$CategoryFilePath = Join-Path -Path $VerbFolder.FullName -ChildPath '_category_.json'
switch ($VerbFolder.Name) {
'Connect' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Connect to Services'
$CategoryFile.position = 0.1
$CategoryFile.className = 'category-connect'
$CategoryFile.link.title = 'Connect to Services'
$CategoryFile.customProps.description = 'This category contains commands for connecting to services, retrieving and storing credentials and managing connections.'
}
'Find' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Find Information'
$CategoryFile.position = 0.2
$CategoryFile.className = 'category-find'
$CategoryFile.link.title = 'Find Information'
$CategoryFile.customProps.description = 'This category contains commands for finding information from services, this may include data, objects, settings and more.'
}
'Get' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Retrieve Information'
$CategoryFile.position = 0.3
$CategoryFile.className = 'category-get'
$CategoryFile.link.title = 'Retrieve Information'
$CategoryFile.customProps.description = 'This category contains commands for retrieving information from services, this may include data, objects, settings and more.'
}
'Invoke' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Invoke Actions'
$CategoryFile.position = 0.4
$CategoryFile.className = 'category-invoke'
$CategoryFile.link.title = 'Invoke Actions'
$CategoryFile.customProps.description = 'This category contains commands for invoking actions, this may include running scripts, executing commands and more. For API modules, this category will contain commands for sending arbitrary requests to the API - that is requests not covered by existing commands.'
}
'New' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Create Data'
$CategoryFile.position = 0.5
$CategoryFile.className = 'category-new'
$CategoryFile.link.title = 'Create Data'
$CategoryFile.customProps.description = 'This category contains commands for creating data, objects, settings and more.'
}
'Remove' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Remove Data'
$CategoryFile.position = 0.6
$CategoryFile.className = 'category-remove'
$CategoryFile.link.title = 'Remove Data'
$CategoryFile.customProps.description = 'This category contains commands for removing data, objects, settings and more.'
}
'Reset' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Reset State'
$CategoryFile.position = 0.6
$CategoryFile.className = 'category-reset'
$CategoryFile.link.title = 'Reset State'
$CategoryFile.customProps.description = 'This category contains commands for resetting state, this may include resetting settings, connections and more.'
}
'Restart' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Restart Services'
$CategoryFile.position = 0.6
$CategoryFile.className = 'category-restart'
$CategoryFile.link.title = 'Restart Services'
$CategoryFile.customProps.description = 'This category contains commands for restarting services, this may include restarting services, processes and more.'
}
'Restore' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Restore Data'
$CategoryFile.position = 0.6
$CategoryFile.className = 'category-restore'
$CategoryFile.link.title = 'Restore Data'
$CategoryFile.customProps.description = 'This category contains commands for restoring data, objects, settings and more. These commands will primarily be used for restoring data to a previous state.'
}
'Set' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Update Data (Set)'
$CategoryFile.position = 0.4
$CategoryFile.className = 'category-set'
$CategoryFile.link.title = 'Update Data (Set)'
$CategoryFile.customProps.description = 'This category contains commands for updating data, objects, settings and more. This category will overlap with the Update category.'
}
'Update' {
$CategoryFile = $CategoryFileBase
$CategoryFile.label = 'Update Data (Update)'
$CategoryFile.position = 0.4
$CategoryFile.className = 'category-update'
$CategoryFile.link.title = 'Update Data (Update)'
$CategoryFile.customProps.description = 'This category contains commands for updating data, objects, settings and more. This category will overlap with the Set category.'
}
}
if (-Not($HasCategoryFile)) {
$CategoryFile | ConvertTo-Json | Out-File -FilePath $CategoryFilePath -Force
} else {
if (-Not($ForceUpdateCategoryFiles)) {
Write-Warning -Message ('Category file already exists in "{0}" verb folder. Use the ForceUpdateCategoryFiles switch to overwrite existing category files.' -f $VerbFolder.Name)
} else {
Set-Content -Path $CategoryFilePath -Value ($CategoryFile | ConvertTo-Json) -Force
}
}
}
}
# Copy PowerShell Module files to output folder for release on PSGallery
if ($CopyModuleFiles) {
# Copy Module Files to Output Folder
if (-not (Test-Path "$($PSScriptRoot)\Output\$ModuleName")) {
New-Item -Path "$($PSScriptRoot)\Output\$ModuleName" -ItemType Directory | Out-Null
}
if (Test-Path -Path "$($PSScriptRoot)\Classes\") {
Copy-Item -Path "$($PSScriptRoot)\Classes\" -Filter *.* -Recurse -Destination "$($PSScriptRoot)\Output\$ModuleName" -Force
}
if (Test-Path -Path "$($PSScriptRoot)\Data\") {
Copy-Item -Path "$($PSScriptRoot)\Data\" -Filter *.* -Recurse -Destination "$($PSScriptRoot)\Output\$ModuleName" -Force
}
Copy-Item -Path "$($PSScriptRoot)\Private\" -Filter *.* -Recurse -Destination "$($PSScriptRoot)\Output\$ModuleName" -Force
Copy-Item -Path "$($PSScriptRoot)\Public\" -Filter *.* -Recurse -Destination "$($PSScriptRoot)\Output\$ModuleName" -Force
# Copy module, manifest and scaffold files
Copy-Item -Path @(
"$($PSScriptRoot)\LICENSE.md"
"$($PSScriptRoot)\CHANGELOG.md"
"$($PSScriptRoot)\README.md"
"$($PSScriptRoot)\$ModuleName.psd1"
"$($PSScriptRoot)\$ModuleName.psm1"
) -Destination "$($PSScriptRoot)\Output\$ModuleName" -Force
}
# Run all Pester tests in folder .\Tests
if ($Test) {
$Result = Invoke-Pester "$($PSScriptRoot)\Tests" -PassThru
if ($Result.FailedCount -gt 0) {
throw 'Pester tests failed'
}
}
# Update the Module Manifest file with info from the Changelog.
if ($UpdateManifest) {
# Import PlatyPS. Needed for parsing the versions in the Changelog.
Import-Module -Name PlatyPS
# Find Latest Version in Change log.
$CHANGELOG = Get-Content -Path "$($PSScriptRoot)\CHANGELOG.md" -Raw
$MarkdownObject = [Markdown.MAML.Parser.MarkdownParser]::new()
[regex]$ReleaseRegex = '#{2}.*\d*\.\d*\.\d*$/m'
$Releases = ($ReleaseRegex.Matches($MarkdownObject.ParseString($CHANGELOG).Children.Spans.Text))
[regex]$VersionRegex = '\d*\.\d*\.\d*'
$Versions = $Regex.Matches($MarkdownObject.ParseString($CHANGELOG).Children.Spans.Text) | ForEach-Object { $_.Value }
$ChangeLogVersion = ($Versions | Measure-Object -Maximum).Maximum
$ManifestPath = "$($PSScriptRoot)\$ModuleName.psd1"
# Start by importing the manifest to determine the version, then add 1 to the Build
$Manifest = Test-ModuleManifest -Path $ManifestPath
[System.Version]$Version = $Manifest.Version
if ($ChangeLogVersion -eq $Version) {
Throw 'No new version found in CHANGELOG.md'
}
Write-Output -InputObject ("Current Module Version: $($Version)")
Write-Output -InputObject ("New Module version: $($ChangeLogVersion)")
# Update Manifest file with Release Notes
$CHANGELOG = Get-Content -Path "$($PSScriptRoot)\CHANGELOG.md"
$MarkdownObject = [Markdown.MAML.Parser.MarkdownParser]::new()
$ReleaseNotes = ((($MarkdownObject.ParseString($CHANGELOG).Children.Spans.Text) -Match '#{2}.*\d*\.\d*\.\d') -Split ' - ')[1]
# Update Module with new version
Update-ModuleManifest -ModuleVersion $ChangeLogVersion -Path "$($PSScriptRoot)\$ModuleName.psd1" -ReleaseNotes $ReleaseNotes
}
# Publish Module to PowerShell Gallery
if ($PublishModule -and $Configuration -eq 'Production') {
Try {
# Build a splat containing the required details and make sure to Stop for errors which will trigger the catch
$params = @{
Path = ("$($PSScriptRoot)\Output\$ModuleName")
NuGetApiKey = $ENV:TF_BUILD ? $ENV:PSGalleryAPIKey : (Get-AzKeyVaultSecret -VaultName $ENV:PSGalleryVault -Name $ENV:PSGallerySecret -AsPlainText) # If running in Azure DevOps, use the Environment Variable, otherwise use the Key Vault
ErrorAction = 'Stop'
}
$ManifestPath = "$($PSScriptRoot)\$ModuleName.psd1"
$Manifest = Test-ModuleManifest -Path $ManifestPath
[System.Version]$Version = $Manifest.Version
Publish-Module @params
Write-Output -InputObject ("$ModuleName PowerShell Module version $($Version) published to the PowerShell Gallery")
} Catch {
Throw $_
}
}
# Clean up Output folder
if ($Clean) {
# Clean output folder
if ((Test-Path "$($PSScriptRoot)\Output")) {
Remove-Item -Path "$($PSScriptRoot)\Output" -Recurse -Force
}
}