|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Repair PlatyPS generated markdown files. |
| 4 | +.NOTES |
| 5 | + This file is temporarily required to handle platyPS help generation. |
| 6 | + https://github.com/PowerShell/platyPS/issues/595 |
| 7 | + This is a result of a breaking change introduced in PowerShell 7.4.0: |
| 8 | + https://learn.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-74?view=powershell-7.4 |
| 9 | + Breaking Changes: Added the ProgressAction parameter to the Common Parameters |
| 10 | + modified from source: https://github.com/PowerShell/platyPS/issues/595#issuecomment-1820971702 |
| 11 | +#> |
| 12 | + |
| 13 | +function Remove-CommonParameterFromMarkdown { |
| 14 | + <# |
| 15 | + .SYNOPSIS |
| 16 | + Remove a PlatyPS generated parameter block. |
| 17 | + .DESCRIPTION |
| 18 | + Removes parameter block for the provided parameter name from the markdown file provided. |
| 19 | + #> |
| 20 | + param( |
| 21 | + [Parameter(Mandatory)] |
| 22 | + [string[]] |
| 23 | + $Path, |
| 24 | + |
| 25 | + [Parameter(Mandatory = $false)] |
| 26 | + [string[]] |
| 27 | + $ParameterName = @('ProgressAction') |
| 28 | + ) |
| 29 | + $ErrorActionPreference = 'Stop' |
| 30 | + foreach ($p in $Path) { |
| 31 | + $content = (Get-Content -Path $p -Raw).TrimEnd() |
| 32 | + $updateFile = $false |
| 33 | + foreach ($param in $ParameterName) { |
| 34 | + if (-not ($Param.StartsWith('-'))) { |
| 35 | + $param = "-$($param)" |
| 36 | + } |
| 37 | + # Remove the parameter block |
| 38 | + $pattern = "(?m)^### $param\r?\n[\S\s]*?(?=#{2,3}?)" |
| 39 | + $newContent = $content -replace $pattern, '' |
| 40 | + # Remove the parameter from the syntax block |
| 41 | + $pattern = " \[$param\s?.*?]" |
| 42 | + $newContent = $newContent -replace $pattern, '' |
| 43 | + if ($null -ne (Compare-Object -ReferenceObject $content -DifferenceObject $newContent)) { |
| 44 | + Write-Verbose "Added $param to $p" |
| 45 | + # Update file content |
| 46 | + $content = $newContent |
| 47 | + $updateFile = $true |
| 48 | + } |
| 49 | + } |
| 50 | + # Save file if content has changed |
| 51 | + if ($updateFile) { |
| 52 | + $newContent | Out-File -Encoding utf8 -FilePath $p |
| 53 | + Write-Verbose "Updated file: $p" |
| 54 | + } |
| 55 | + } |
| 56 | + return |
| 57 | +} |
| 58 | + |
| 59 | +function Add-MissingCommonParameterToMarkdown { |
| 60 | + param( |
| 61 | + [Parameter(Mandatory)] |
| 62 | + [string[]] |
| 63 | + $Path, |
| 64 | + |
| 65 | + [Parameter(Mandatory = $false)] |
| 66 | + [string[]] |
| 67 | + $ParameterName = @('ProgressAction') |
| 68 | + ) |
| 69 | + $ErrorActionPreference = 'Stop' |
| 70 | + foreach ($p in $Path) { |
| 71 | + $content = (Get-Content -Path $p -Raw).TrimEnd() |
| 72 | + $updateFile = $false |
| 73 | + foreach ($NewParameter in $ParameterName) { |
| 74 | + if (-not ($NewParameter.StartsWith('-'))) { |
| 75 | + $NewParameter = "-$($NewParameter)" |
| 76 | + } |
| 77 | + $pattern = '(?m)^This cmdlet supports the common parameters:(.+?)\.' |
| 78 | + $replacement = { |
| 79 | + $Params = $_.Groups[1].Captures[0].ToString() -split ' ' |
| 80 | + $CommonParameters = @() |
| 81 | + foreach ($CommonParameter in $Params) { |
| 82 | + if ($CommonParameter.StartsWith('-')) { |
| 83 | + if ($CommonParameter.EndsWith(',')) { |
| 84 | + $CleanParam = $CommonParameter.Substring(0, $CommonParameter.Length - 1) |
| 85 | + } |
| 86 | + elseif ($p.EndsWith('.')) { |
| 87 | + $CleanParam = $CommonParameter.Substring(0, $CommonParameter.Length - 1) |
| 88 | + } |
| 89 | + else { |
| 90 | + $CleanParam = $CommonParameter |
| 91 | + } |
| 92 | + $CommonParameters += $CleanParam |
| 93 | + } |
| 94 | + } |
| 95 | + if ($NewParameter -notin $CommonParameters) { |
| 96 | + $CommonParameters += $NewParameter |
| 97 | + } |
| 98 | + $CommonParameters[-1] = "and $($CommonParameters[-1]). " |
| 99 | + return "This cmdlet supports the common parameters: " + (($CommonParameters | Sort-Object) -join ', ') |
| 100 | + } |
| 101 | + $newContent = $content -replace $pattern, $replacement |
| 102 | + if ($null -ne (Compare-Object -ReferenceObject $content -DifferenceObject $newContent)) { |
| 103 | + Write-Verbose "Added $NewParameter to $p" |
| 104 | + $updateFile = $true |
| 105 | + $content = $newContent |
| 106 | + } |
| 107 | + } |
| 108 | + # Save file if content has changed |
| 109 | + if ($updateFile) { |
| 110 | + $newContent | Out-File -Encoding utf8 -FilePath $p |
| 111 | + Write-Verbose "Updated file: $p" |
| 112 | + } |
| 113 | + } |
| 114 | + return |
| 115 | +} |
| 116 | + |
| 117 | +function Repair-PlatyPSMarkdown { |
| 118 | + param( |
| 119 | + [Parameter(Mandatory)] |
| 120 | + [string[]] |
| 121 | + $Path, |
| 122 | + |
| 123 | + [Parameter()] |
| 124 | + [string[]] |
| 125 | + $ParameterName = @('ProgressAction') |
| 126 | + ) |
| 127 | + $ErrorActionPreference = 'Stop' |
| 128 | + $Parameters = @{ |
| 129 | + Path = $Path |
| 130 | + ParameterName = $ParameterName |
| 131 | + } |
| 132 | + $null = Remove-CommonParameterFromMarkdown @Parameters |
| 133 | + $null = Add-MissingCommonParameterToMarkdown @Parameters |
| 134 | + return |
| 135 | +} |
0 commit comments