Skip to content

Commit c830c46

Browse files
committed
refactor: complete overhaul of PlatyPS Markdown generation.
Make use of Get/Set/Format Brownserve content to drastically speed up documentation generation. Some de-duping may still be beneficial for m0ar gains.
1 parent 7bc2bb7 commit c830c46

File tree

6 files changed

+190
-137
lines changed

6 files changed

+190
-137
lines changed

Module/Private/PowerShell/platyPS/Update-PlatyPSModulePageDescription.ps1

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,30 @@ function Update-PlatyPSModulePageDescription
2828
Position = 1
2929
)]
3030
[ValidateNotNullOrEmpty()]
31-
[string]
32-
$ModulePagePath
31+
[BrownserveContent]
32+
$ModulePageContent
3333
)
3434
begin
3535
{
3636
}
3737
process
3838
{
39-
$ModulePageContent = Get-Content $ModulePagePath -ErrorAction 'Stop' -Raw
40-
if (!$ModulePageContent)
41-
{
42-
throw 'Module page content is empty'
43-
}
44-
if ($ModulePageContent -imatch '## Description[\s\n]*{{ Fill in the Description }}')
39+
$ModulePageContentString = $ModulePageContent.ToString()
40+
41+
# We don't yet support automatically updating the module description in the module page.
42+
# only setting it if it's not already set.
43+
if ($ModulePageContentString -imatch '## Description[\s\n]*{{ Fill in the Description }}')
4544
{
4645
# .Replace method doesn't work 🤷‍♀️ so use the -replace param instead.
47-
$NewModulePageContent = $ModulePageContent -Replace '## Description[\s\n]*{{ Fill in the Description }}', "## Description`r`n$ModuleDescription"
48-
try
49-
{
50-
Set-Content `
51-
-Path $ModulePagePath `
52-
-Value $NewModulePageContent `
53-
-NoNewline `
54-
-ErrorAction 'Stop'
55-
}
56-
catch
57-
{
58-
throw "Failed to update module page description.`n$($_.Exception.Message)"
59-
}
46+
$NewModulePageContent = $ModulePageContentString -Replace '## Description[\s\n]*{{ Fill in the Description }}', "## Description`n`n$ModuleDescription"
47+
$ModulePageContent.Content = $NewModulePageContent | Format-BrownserveContent | Select-Object -ExpandProperty Content
48+
49+
return $ModulePageContent
6050
}
6151
else
6252
{
6353
Write-Verbose 'Module page description already set'
54+
return $ModulePageContent
6455
}
6556
}
6657
end

Module/Private/PowerShell/platyPS/Update-PlatyPSModulePageGUID.ps1

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,21 @@ function Update-PlatyPSModulePageGUID
2828
Position = 1
2929
)]
3030
[ValidateNotNullOrEmpty()]
31-
[string]
32-
$ModulePagePath
31+
[BrownserveContent]
32+
$ModulePageContent
3333
)
3434
begin
3535
{
3636
}
3737
process
3838
{
39-
$ModulePageContent = Get-Content $ModulePagePath -ErrorAction 'Stop' -Raw
40-
if (!$ModulePageContent)
41-
{
42-
throw 'Module page content is empty'
43-
}
39+
$ModulePageContentString = $ModulePageContent.ToString()
4440
$NewModuleGUID = $ModuleGUID.ToString()
45-
$NewModulePageContent = $ModulePageContent -replace 'Module Guid: ([\w|\d|-]*)', "Module Guid: $NewModuleGUID"
41+
$NewModulePageContent = $ModulePageContentString -replace 'Module Guid: ([\w|\d|-]*)', "Module Guid: $NewModuleGUID"
4642

47-
try
48-
{
49-
Set-Content `
50-
-Path $ModulePagePath `
51-
-Value $NewModulePageContent `
52-
-NoNewline `
53-
-ErrorAction 'Stop'
54-
}
55-
catch
56-
{
57-
throw "Failed to update module page GUID.`n$($_.Exception.Message)"
58-
}
43+
$ModulePageContent.Content = $NewModulePageContent | Format-BrownserveContent | Select-Object -ExpandProperty Content
44+
45+
return $ModulePageContent
5946
}
6047
end
6148
{

Module/Private/PowerShell/platyPS/Update-PlatyPSModulePageLinks.ps1

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,38 @@ function Update-PlatyPSModulePageLinks
2222
[string]
2323
$CmdletDocumentationPath,
2424

25-
# The path to the module page
25+
# The content of the module page
2626
[Parameter(
2727
Mandatory = $true,
2828
ValueFromPipelineByPropertyName = $true,
2929
Position = 1
3030
)]
3131
[ValidateNotNullOrEmpty()]
32-
[string]
33-
$ModulePagePath
32+
[BrownserveContent]
33+
$ModulePageContent
3434
)
3535
begin
3636
{
3737
}
3838
process
3939
{
40-
$ModulePageContent = Get-Content $ModulePagePath -ErrorAction 'Stop' -Raw
41-
if (!$ModulePageContent)
42-
{
43-
throw 'Module page content is empty'
44-
}
40+
$ModulePageContentString = $ModulePageContent.ToString()
41+
<#
42+
As it stands we expect the module page path to be one level above the cmdlet documentation path.
43+
e.g:
44+
| ModulePage.md
45+
| CmdletDocumentation
46+
| Cmdlet1.md
47+
| Cmdlet2.md
48+
| Cmdlet3.md
49+
We may want to change this assumption in the future.
50+
#>
4551
$ModulePageAdjustment = Split-Path $CmdletDocumentationPath -Leaf
46-
$NewModulePageContent = $ModulePageContent -replace '\(([\w|\d]*-[\w|\d]*.md)\)', "(./$ModulePageAdjustment/`$1)"
52+
$NewModulePageContent = $ModulePageContentString -replace '\(([\w|\d]*-[\w|\d]*.md)\)', "(./$ModulePageAdjustment/`$1)"
53+
54+
$ModulePageContent.Content = $NewModulePageContent | Format-BrownserveContent | Select-Object -ExpandProperty Content
4755

48-
try
49-
{
50-
Set-Content `
51-
-Path $ModulePagePath `
52-
-Value $NewModulePageContent `
53-
-NoNewline `
54-
-ErrorAction 'Stop'
55-
}
56-
catch
57-
{
58-
throw "Failed to update module page links.`n$($_.Exception.Message)"
59-
}
56+
return $ModulePageContent
6057
}
6158
end
6259
{

Module/Public/Build/New-BrownservePowerShellModuleBuild.ps1

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function New-BrownservePowerShellModuleBuild
2323
[string]
2424
$RepoName
2525
)
26-
2726
begin
2827
{
2928
try
@@ -45,7 +44,6 @@ function New-BrownservePowerShellModuleBuild
4544
throw "Cannot determine RepoName automatically."
4645
}
4746
}
48-
4947
process
5048
{
5149
switch ($CICDProvider)
@@ -56,15 +54,9 @@ function New-BrownservePowerShellModuleBuild
5654
$WorkflowDirectory = Join-Path $GitHubDirectory 'workflows'
5755
$BuildWorkflowPath = Join-Path $WorkflowDirectory 'build.yaml'
5856
$ReleaseWorkflowPath = Join-Path $WorkflowDirectory 'release.yaml'
57+
$FilesToCheck = @( $BuildWorkflowPath, $ReleaseWorkflowPath)
5958

60-
if ((Test-Path $BuildWorkflowPath))
61-
{
62-
throw "Build workflow already exists at '$BuildWorkflowPath'"
63-
}
64-
if ((Test-Path $ReleaseWorkflowPath))
65-
{
66-
throw "Build workflow already exists at '$ReleaseWorkflowPath'"
67-
}
59+
$FilesToCheck | Assert-PathDoesNotExist
6860

6961
# These steps are always needed regardless of the build
7062
$DefaultSteps = @(
@@ -77,51 +69,81 @@ function New-BrownservePowerShellModuleBuild
7769
}
7870
)
7971

72+
# This command is used to run the module build script, we build up the params separately and splat them in
73+
$RunCommand = "./$($ModuleInfo.Name)/.build/build.ps1 @Params"
74+
75+
# Params for building/testing the module
76+
$BuildTestParams = @"
77+
`$Params = @{
78+
BranchName = `$env:GITHUB_REF
79+
GitHubRepoName = '$RepoName'
80+
Build = 'BuildTestAndCheck'
81+
Verbose = `$true
82+
}
83+
"@
8084
# Steps for building and testing the module
81-
$BuildRunCommand = @"
82-
./$($ModuleInfo.Name)/.build/build.ps1 ``
83-
-GitHubRepoName '$RepoName' ``
84-
-BranchName `$env:GITHUB_REF ``
85-
-Build BuildPackTest ``
86-
-Verbose
85+
$StageReleaseParams = @"
86+
`$Params = @{
87+
BranchName = `$env:GITHUB_REF
88+
GitHubRepoName = '$RepoName'
89+
Build = 'StageRelease'
90+
GitHubStageReleaseToken = `$env:GitHubPAT
91+
ReleaseType = '`${{inputs.release_type}}'
92+
UseWorkingCopy = `$true
93+
Verbose = `$true
94+
}
95+
if (`$env:ReleaseNotice)
96+
{
97+
`$Params.Add('ReleaseNotice', `$env:ReleaseNotice)
98+
}
99+
"@
100+
$ReleaseParams = @"
101+
`$Params = @{
102+
BranchName = `$env:GITHUB_REF
103+
GitHubRepoName = '$RepoName'
104+
Build = 'Release'
105+
GitHubReleaseToken = `$env:GitHubPAT
106+
NugetFeedAPIKey = `$env:NugetFeedAPIKey
107+
PSGalleryAPIKey = `$env:PSGalleryAPIKey
108+
PublishTo = `${{ inputs.publish_to }}
109+
Verbose = `$true
110+
}
87111
"@
88112
$BuildSteps = $DefaultSteps
89113
$BuildSteps += [ordered]@{
90-
name = 'build-and-test-module'
114+
name = 'run-build-script'
91115
shell = 'pwsh'
92-
run = $BuildRunCommand
116+
run = $BuildTestParams + "`n" + $RunCommand
93117
}
94118

95119
$BuildJobs = @{
96-
JobTitle = 'build-and-test'
120+
JobTitle = 'BuildTestAndCheck'
97121
RunsOn = 'ubuntu-latest'
98122
Steps = $BuildSteps
99123
}
100124

101-
# Steps for releasing the module
102-
$ReleaseRunCommand = @"
103-
./$($ModuleInfo.Name)/.build/build.ps1 ``
104-
-GitHubRepoName '$RepoName' ``
105-
-BranchName `$env:GITHUB_REF ``
106-
-Build release ``
107-
-NugetFeedAPIKey `$env:NugetFeedAPIKey ``
108-
-PSGalleryAPIKey `$env:PSGalleryAPIKey ``
109-
-Verbose
110-
"@
125+
$StageReleaseSteps = $DefaultSteps
126+
# We need to fetch the full history so that we can determine the changelog entries from the previous release
127+
$StageReleaseSteps[0].with.Add('fetch-depth', 0)
128+
$StageReleaseSteps += [ordered]@{
129+
name = 'stage-release'
130+
shell = 'pwsh'
131+
run = $StageReleaseParams + "`n" + $RunCommand
132+
}
111133

112134
$ReleaseSteps = $DefaultSteps
113135
$ReleaseSteps += [ordered]@{
114136
name = 'build-and-release-module'
115137
shell = 'pwsh'
116-
run = $ReleaseRunCommand
138+
run = $ReleaseParams + "`n" + $RunCommand
117139
}
118-
140+
119141
$ReleaseJobs = @{
120142
JobTitle = 'release'
121143
RunsOn = 'ubuntu-latest'
122144
Steps = $ReleaseSteps
123145
}
124-
146+
125147
try
126148
{
127149
$BuildWorkflowContent = New-GitHubActionsWorkflow `
@@ -132,6 +154,14 @@ function New-BrownservePowerShellModuleBuild
132154
{
133155
throw 'Workflow content empty.'
134156
}
157+
$StageReleaseWorkflowContent = New-GitHubActionsWorkflow `
158+
-Name 'stage-release' `
159+
-ExecuteOn 'workflow_dispatch' `
160+
-Jobs $StageReleaseJobs
161+
if (!$StageReleaseWorkflowContent)
162+
{
163+
throw 'Workflow content empty'
164+
}
135165
$ReleaseWorkflowContent = New-GitHubActionsWorkflow `
136166
-Name 'release' `
137167
-ExecuteOn 'workflow_dispatch' `
@@ -146,6 +176,7 @@ function New-BrownservePowerShellModuleBuild
146176
throw "Failed to create GitHub Actions workflow.`n$($_.Exception.Message)"
147177
}
148178
Write-Verbose "Build workflow content:`n$BuildWorkflowContent"
179+
Write-Verbose "Stage release workflow content:`n$StageReleaseWorkflowContent"
149180
Write-Verbose "Release workflow content:`n$ReleaseWorkflowContent"
150181
try
151182
{
@@ -158,6 +189,7 @@ function New-BrownservePowerShellModuleBuild
158189
New-Item $WorkflowDirectory -ItemType Directory -ErrorAction 'stop' | Out-Null
159190
}
160191
New-Item $BuildWorkflowPath -ItemType File -Value $BuildWorkflowContent -ErrorAction 'Stop' | Out-Null
192+
New-Item $StageReleaseWorkflowPath -ItemType File -Value $StageReleaseWorkflowContent -ErrorAction 'Stop' | Out-Null
161193
New-Item $ReleaseWorkflowPath -ItemType File -Value $ReleaseWorkflowContent -ErrorAction 'Stop' | Out-Null
162194
}
163195
catch
@@ -171,9 +203,7 @@ function New-BrownservePowerShellModuleBuild
171203
}
172204
}
173205
}
174-
175206
end
176207
{
177-
178208
}
179-
}
209+
}

Module/Public/Markdown/Format-Markdown.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
#>
77
function Format-Markdown
88
{
9-
[CmdletBinding()]
9+
[CmdletBinding(
10+
DefaultParameterSetName = 'Path'
11+
)]
1012
param
1113
(
1214
# The path to the markdown file to format.
1315
[Parameter(
16+
ParameterSetName = 'Path',
1417
Mandatory = $true,
1518
ValueFromPipelineByPropertyName = $true,
1619
Position = 0
@@ -39,12 +42,12 @@ function Format-Markdown
3942

4043
# Special hidden parameter to pass in markdown from the pipeline.
4144
[Parameter(
45+
ParameterSetName = 'Pipeline',
4246
Mandatory = $false,
4347
ValueFromPipeline = $true,
4448
ValueFromPipelineByPropertyName = $true,
4549
DontShow
4650
)]
47-
[ValidateNotNullOrEmpty()]
4851
[array]
4952
$Markdown
5053
)

0 commit comments

Comments
 (0)