@@ -11,39 +11,34 @@ function Add-Parameter {
1111
1212 Note that THIS generator does not add parameters to script files directly, but only to functions defined in the InputObject.
1313 . EXAMPLE
14- $Boilerplate = {
15- param(
16- # The Foreground Color (name, #rrggbb, etc)
17- [Alias('Fg')]
18- [PoshCode.Pansies.RgbColor]$ForegroundColor,
19-
20- # The Background Color (name, #rrggbb, etc)
21- [Alias('Bg')]
22- [PoshCode.Pansies.RgbColor]$BackgroundColor
23- )
24- $ForegroundColor.ToVt() + $BackgroundColor.ToVt($true) + (
25- Use-OriginalBlock
26- ) +"`e[0m" # Reset colors
27- }
28-
14+ # The normal way to use Add-Parameter is to set it in the build manifest for your module,
15+ # to add a common set of parameters to certain functions in the module by wildcard name.
16+ #
17+ # Set the Generator to Add-Parameter and pass a file to copy parameters from as `boilerplate`
18+ # Finally, pass the names of the functions you want to copy them to:
19+ @{
20+ ModuleManifest = "./source/TerminalBlocks.psd1"
21+ Generators = @(
22+ @{ Generator = "Add-Parameter"; Boilerplate = "NewTerminalBlock.ps1"; Function = "Show-*", "New-TerminalBlock" }
23+ )
24+ }
25+ . EXAMPLE
26+ # You can also use it through Invoke-ScriptGenerator to add parameters to a function in the wild!
27+ # The BoilerPlate must have a param block with parameters, whether it's a string, script, file path, function, or scriptblock ...
28+ # Remember, Add-Parameter will not replace parameters, so if a parameter name already exists, it will not be touched.
2929
30- $Source = {
3130 function Show-Date {
3231 param(
3332 # The text to display
3433 [string]$Format
3534 )
3635 Get-Date -Format $Format
3736 }
38- }
39-
40- Invoke-ScriptGenerator $Source -Generator Add-Parameter -Parameters @{ FunctionName = "*"; Boilerplate = $Boilerplate } -OutVariable Source
4137
42- function Show-Date {
38+ ${function:Show-Date} = Invoke-ScriptGenerator ${function:Show-Date} -Generator Add-Parameter -Parameters @{
39+ FunctionName = "*";
40+ Boilerplate = {
4341 param(
44- # The text to display
45- [string]$Format,
46-
4742 # The Foreground Color (name, #rrggbb, etc)
4843 [Alias('Fg')]
4944 [PoshCode.Pansies.RgbColor]$ForegroundColor,
@@ -52,14 +47,33 @@ function Add-Parameter {
5247 [Alias('Bg')]
5348 [PoshCode.Pansies.RgbColor]$BackgroundColor
5449 )
55- Get-Date -Format $Format
50+ }
5651 }
52+
53+ Get-Command Show-Date | Format-List
54+
55+ Name : Show-Date
56+ CommandType : Function
57+ Definition :
58+ param(
59+ # The text to display
60+ [string]$Format,
61+
62+ # The Foreground Color (name, #rrggbb, etc)
63+ [Alias('Fg')]
64+ [PoshCode.Pansies.RgbColor]$ForegroundColor,
65+
66+ # The Background Color (name, #rrggbb, etc)
67+ [Alias('Bg')]
68+ [PoshCode.Pansies.RgbColor]$BackgroundColor
69+ )
70+ Get-Date -Format $Format
5771 #>
58- [Diagnostics.CodeAnalysis.SuppressMessageAttribute (
59- <# RuleId#> ' PSReviewUnusedParameter' ,
60- <# ParameterName#> ' FunctionName' ,
61- Justification = ' This parameter IS used, the rule does not understand scopes'
62- )]
72+ # [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
73+ # <#RuleId#>'PSReviewUnusedParameter',
74+ # <#ParameterName#>'FunctionName',
75+ # Justification = 'This parameter IS used, the rule does not understand scopes'
76+ # )]
6377 [CmdletBinding ()]
6478 [OutputType ([TextReplacement ])]
6579 param (
@@ -143,33 +157,44 @@ function Add-Parameter {
143157
144158 [AstVisitAction ] VisitFunctionDefinition([FunctionDefinitionAst ]$ast ) {
145159 if (! $ast.Where ($this.FunctionFilter )) {
160+ Write-Debug " Skipping function $ ( $ast.Name ) because it does not match the filter"
146161 return [AstVisitAction ]::SkipChildren
147162 }
163+ Write-Debug " Processing function $ ( $ast.Name ) "
164+ return [AstVisitAction ]::Continue
165+ }
148166
149- [ParameterExtractor ]$ExistingParameters = $ast
167+ [AstVisitAction ] VisitParamBlock([ParamBlockAst ]$ast ) {
168+
169+ # [ParameterExtractor]$ExistingParameters = $ast
170+ $InsertOffset = if ($Ast.Parameters ) {
171+ $ast.Parameters [-1 ].Extent.EndOffset
172+ } else {
173+ $ast.Extent.EndOffset - 1
174+ }
150175
151- Write-Debug " Existing parameters in $ ( $ast .Name ) : $ ( $ExistingParameters .Parameters.Name -join ' , ' ) "
176+ Write-Debug " Existing parameters: $ ( $Ast .Parameters.Name -join ' , ' ) "
152177 $global :ParameterSource = $this.ParameterSource
153- $Additional = $this.ParameterSource.Parameters.Where { $_.Name -notin $ExistingParameters .Parameters.Name }
178+ $Additional = $this.ParameterSource.Parameters.Where { $_.Name -notin ([ string []] $Ast .Parameters.Name ) }
154179 Write-Debug " Additional parameters from boilerplate: $ ( $Additional.Count ) "
155180 if (($Text = $Additional.Text -join " ,`n`n " )) {
156- Write-Debug " Adding parameters to $ ( $ast .Name ) : $ ( $Additional.Name -join ' , ' ) "
181+ Write-Debug " Adding parameters: $ ( $Additional.Name -join ' , ' ) "
157182 $this.Replacements.Add (@ {
158- StartOffset = $ExistingParameters . InsertOffset
159- EndOffset = $ExistingParameters . InsertOffset
160- Text = if ($ExistingParameters .Parameters.Count -gt 0 ) {
183+ StartOffset = $InsertOffset
184+ EndOffset = $InsertOffset
185+ Text = if ($Ast .Parameters.Count -gt 0 ) {
161186 " ,`n`n " + $Text
162187 } else {
163188 " `n " + $Text
164189 }
165- })
190+ })
166191 }
167192 return [AstVisitAction ]::SkipChildren
168193 }
169194 }
170195 }
171196 process {
172- # Write-Debug "Add-Parameter $($InputObject.Extent.File ?? ( "L:" + $InputObject.Extent.StartLineNumber + ".." + $InputObject.Extent.EndLineNumber + " C:" + $InputObject.Extent.StartColumnNumber + ".." + $InputObject.Extent.EndColumnNumber)) $FunctionName $Boilerplate"
197+ Write-Debug " Add-Parameter $ ( $InputObject.Extent.File -ne " scriptblock " ? $InputObject .Extent.File : ( " L:" + $InputObject.Extent.StartLineNumber + " .." + $InputObject.Extent.EndLineNumber + " C:" + $InputObject.Extent.StartColumnNumber + " .." + $InputObject.Extent.EndColumnNumber )) $FunctionName $Boilerplate "
173198
174199 $Generator = [ParameterGenerator ]@ {
175200 FunctionFilter = { $Func = $_ ; $FunctionName.ForEach ({ $Func.Name -like $_ }) -contains $true }.GetNewClosure()
0 commit comments