Skip to content

Commit 7cfe5ec

Browse files
committed
feat: add cmdlets to work around even more PlatyPS issues.
This time PowerShell/platyPS#595 hopefully this is the last one for a while... 😬
1 parent c830c46 commit 7cfe5ec

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<#
2+
.SYNOPSIS
3+
Adds a set of common parameters to a function's documentation footer.
4+
.DESCRIPTION
5+
This works around a bug in platyPS (https://github.com/PowerShell/platyPS/issues/595) that isn't likely to be
6+
fixed until the the PlatyPS rewrite has been completed.
7+
Should more common parameters be added to PowerShell in the future, this script will need to be updated.
8+
#>
9+
function Add-PlatyPSCommonParameter
10+
{
11+
[CmdletBinding()]
12+
param
13+
(
14+
# The content to check for common parameters.
15+
[Parameter(
16+
Mandatory = $true,
17+
ValueFromPipelineByPropertyName = $true,
18+
Position = 0
19+
)]
20+
[BrownserveContent[]]
21+
$Content,
22+
23+
# The common parameters to remove.
24+
[Parameter(
25+
Mandatory = $false,
26+
Position = 1
27+
)]
28+
[string[]]
29+
$Parameter = @('-ProgressAction')
30+
)
31+
32+
begin
33+
{
34+
$Return = @()
35+
}
36+
37+
process
38+
{
39+
foreach ($FileToProcess in $Content)
40+
{
41+
$StringContent = $FileToProcess.ToString()
42+
$SupportedCommonParamPattern = '(?m)^This cmdlet supports the common parameters:(?<common_params>.+?)\.'
43+
if ($StringContent -match $SupportedCommonParamPattern)
44+
{
45+
$CommonParams = $Matches['common_params'] -replace ',', '' -split ' ' |
46+
Where-Object {$_ -match '^-'}
47+
if (!$CommonParams)
48+
{
49+
throw 'Failed to extract common parameters from the supported common parameters section.'
50+
}
51+
$Parameter | ForEach-Object {
52+
$ParameterName = $_
53+
if (!$ParameterName.StartsWith('-'))
54+
{
55+
$ParameterName = "-$ParameterName"
56+
}
57+
if ($ParameterName -notin $CommonParams)
58+
{
59+
$CommonParams += $ParameterName
60+
}
61+
}
62+
$CommonParams = $CommonParams | Sort-Object
63+
$CommonParams[-1] = "and $($CommonParams[-1])"
64+
$CommonParams = $CommonParams -join ', '
65+
$StringContent = $StringContent -replace $SupportedCommonParamPattern, "This cmdlet supports the common parameters: $CommonParams."
66+
$ProcessedContent = $StringContent | Format-BrownserveContent | Select-Object -ExpandProperty Content
67+
$FileToProcess.Content = $ProcessedContent
68+
$Return += $FileToProcess
69+
}
70+
else
71+
{
72+
throw 'Failed to find the supported common parameters section.'
73+
}
74+
}
75+
}
76+
77+
end
78+
{
79+
if ($Return.Count -gt 0)
80+
{
81+
return $Return
82+
}
83+
else
84+
{
85+
return $null
86+
}
87+
}
88+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<#
2+
.SYNOPSIS
3+
Removes a set of common parameters from a function's documentation.
4+
.DESCRIPTION
5+
This works around a bug in platyPS (https://github.com/PowerShell/platyPS/issues/595) that isn't likely to be
6+
fixed until the the PlatyPS rewrite has been completed.
7+
Should more common parameters be added to PowerShell in the future, this script will need to be updated.
8+
#>
9+
function Remove-PlatyPSCommonParameter
10+
{
11+
[CmdletBinding()]
12+
param
13+
(
14+
# The content to check for common parameters.
15+
[Parameter(
16+
Mandatory = $true,
17+
ValueFromPipelineByPropertyName = $true,
18+
Position = 0
19+
)]
20+
[BrownserveContent[]]
21+
$Content,
22+
23+
# The common parameters to remove.
24+
[Parameter(
25+
Mandatory = $false,
26+
Position = 1
27+
)]
28+
[string[]]
29+
$Parameter = @('-ProgressAction')
30+
)
31+
32+
begin
33+
{
34+
$Return = @()
35+
}
36+
37+
process
38+
{
39+
foreach ($FileToProcess in $Content)
40+
{
41+
$StringContent = $FileToProcess.ToString()
42+
# Regex adapted from: https://github.com/PowerShell/platyPS/issues/595#issuecomment-1820971702
43+
$Parameter | ForEach-Object {
44+
$ParameterName = $_
45+
if (!$ParameterName.StartsWith('-'))
46+
{
47+
$ParameterName = "-$ParameterName"
48+
}
49+
$StringContent = $StringContent -replace "(?m)^### $_\r?\n[\S\s]*?(?=#{2,3}?)", ''
50+
$StringContent = $StringContent -replace " \[$_\s?.*?]"
51+
}
52+
$ProcessedContent = $StringContent | Format-BrownserveContent | Select-Object -ExpandProperty Content
53+
$FileToProcess.Content = $ProcessedContent
54+
$Return += $FileToProcess
55+
}
56+
}
57+
58+
end
59+
{
60+
if ($Return.Count -gt 0)
61+
{
62+
return $Return
63+
}
64+
else
65+
{
66+
return $null
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)