|
| 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 | +} |
0 commit comments