Skip to content

Commit b9aa472

Browse files
committed
Explicitly specify the parameter name for ScriptGenerator
1 parent c9d4ffd commit b9aa472

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

Source/Public/Build-Module.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ function Build-Module {
243243
Join-Path -Path $ModuleInfo.ModuleBase -ChildPath $_ | Convert-Path -ErrorAction SilentlyContinue
244244
}
245245

246-
$ModuleInfo.Generators | Invoke-ScriptGenerator $RootModule -Overwrite
246+
$ModuleInfo.Generators | Invoke-ScriptGenerator -Path $RootModule -Overwrite
247247

248248
# $ParseResult = ConvertToAst $RootModule
249249
# $ParseResult | MoveUsingStatement -Encoding "$($ModuleInfo.Encoding)"

Source/Public/Invoke-ScriptGenerator.ps1

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,29 @@ function Invoke-ScriptGenerator {
2727
'@
2828
2929
# Or use a file path instead
30-
$Source = @'
31-
function Show-Date {
32-
param(
33-
# The text to display
34-
[string]$Format
35-
)
36-
Get-Date -Format $Format
30+
$Code = {
31+
function Show-Date {
32+
param(
33+
# The text to display
34+
[string]$Format
35+
)
36+
Get-Date -Format $Format
37+
}
3738
}
38-
'@
3939
4040
@( @{ Generator = "Add-Parameter"; FunctionName = "*"; Boilerplate = $Boilerplate }
4141
@{ Generator = "Merge-ScriptBlock"; FunctionName = "*"; Boilerplate = $Boilerplate }
42-
) | Invoke-ScriptGenerator $Source
42+
) | Invoke-ScriptGenerator $Code
4343
#>
44-
[CmdletBinding()]
44+
[CmdletBinding(DefaultParameterSetName = "Code")]
4545
param(
4646
# The script content, script, function info, or file path to parse
47-
[Parameter(Mandatory)]
48-
$Source,
47+
[Parameter(ParameterSetName = "Code", Position = 0)]
48+
[ScriptBlock]$Code,
49+
50+
[Parameter(ParameterSetName = "Path", Position = 0)]
51+
[Alias("PSPath", "File")]
52+
[string]$Path,
4953

5054
# The name of the Script Generator to invoke. Must be a command that takes an AST as a pipeline inputand outputs TextReplacement objects.
5155
# There are two built into ModuleBuilder:
@@ -63,16 +67,19 @@ function Invoke-ScriptGenerator {
6367
[switch]$Overwrite
6468
)
6569
begin {
66-
$ParseResults = ConvertToAst $Source
70+
$AstParam = @{} + $PSBoundParameters
71+
$null = $AstParam.Remove("Overwrite")
72+
$null = $AstParam.Remove("Generator")
73+
$null = $AstParam.Remove("Parameters")
74+
$ParseResults = ConvertToAst @AstParam
6775
[StringBuilder]$Builder = $ParseResults.Ast.Extent.Text
68-
$File = $ParseResults.Ast.Extent.File
6976
}
7077
process {
7178
if (-not $PSBoundParameters.ContainsKey("Generator") -and $Parameters.ContainsKey("Generator")) {
7279
$Generator = $Parameters["Generator"]
7380
$null = $Parameters.Remove("Generator")
7481
}
75-
Write-Debug "Invoking $Generator generator for $Source with @{$($Parameters.Keys.ForEach{ $_ } -join ', ')}"
82+
Write-Debug "Invoking $Generator generator for $($ParseResults.Path) ($Path) with @{$($Parameters.Keys.ForEach{ $_ } -join ', ')}"
7683

7784
# To make things more usable, resolve paths to "boilerplate" or "template" files based on our BoilerplateDirectory (alias TemplateDirectory)
7885
try {
@@ -112,21 +119,24 @@ function Invoke-ScriptGenerator {
112119
continue
113120
}
114121

115-
Write-Verbose "Generating $GeneratorCmd in $Source $(if($Parameters.Count){"`n with $($Parameters.GetEnumerator().ForEach{ $_.Key + ' = ' + ($_.Value -join ", ") } -join "`n and ")"})"
122+
Write-Verbose "Generating $GeneratorCmd in $($ParseResults.Path) $(if($Parameters.Count){"`n with $($Parameters.GetEnumerator().ForEach{ $_.Key + ' = ' + ($_.Value -join ", ") } -join "`n and ")"})"
116123
#! Process replacements from the bottom up, so the line numbers work
117124
foreach ($Replacement in $ParseResults | & $GeneratorCmd @Parameters | Sort-Object StartOffset -Descending) {
118125
$Builder = $Builder.Remove($replacement.StartOffset, ($replacement.EndOffset - $replacement.StartOffset)).Insert($replacement.StartOffset, $replacement.Text)
119126
}
120127

121128
#! If we're looping through multiple generators, we have to parse the new version of the source
122129
if ($MyInvocation.ExpectingInput) {
123-
$ParseResults = ConvertToAst $Builder
130+
# Update the AST
131+
$ParseResults = ConvertToAst -Code $Builder.ToString() -Path $ParseResults.Path
132+
# In case a Generator tries to use the actual files, update the content
133+
Set-Content $ParseResults.Path $Builder
124134
}
125135
}
126136
end {
127-
Write-Debug "Overwrite: $Overwrite and it's a file: $([bool]$File) (Content is $($Builder.Length) long)"
128-
if ($Overwrite -and $File) {
129-
Set-Content $File $Builder
137+
Write-Debug "Overwrite: $Overwrite and it's a file: $([bool]$ParseResults.Path) (Content is $($Builder.Length) long)"
138+
if ($Overwrite -and $ParseResults.Path) {
139+
Set-Content $ParseResults.Path $Builder
130140
} else {
131141
$Builder.ToString()
132142
}

0 commit comments

Comments
 (0)