Skip to content

Commit 5387180

Browse files
committed
Look for the template(s) in a subfolder of the module source directory
1 parent 1165c6b commit 5387180

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

Source/Public/Build-Module.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ function Build-Module {
144144
# - Merge-ScriptBlock. Merges boilerplate templates into functions in your module. The command "Use-OriginalBlock" in the boilerplate indicates where the code from the original function would fit into the template. The added blocks come from a boilerplate tempalte file, which must be a script, and can have named begin, process, and end blocks.
145145
[PSCustomObject[]]$Generators = @(),
146146

147-
# The folder (relative to the module folder) which contains the scripts which serve as boilerplate templates for Script Generators
148-
# Defaults to "Generators"
149-
[string[]]$BoilerplateDirectory = @("Boilerplate", "Boilerplates", "Templates"),
147+
# The folder (relative to the module folder) which contains the scripts which serve as boilerplate, or templates, for Script Generators
148+
# Folder paths which don't exist inside the module root are ignored.
149+
# Defaults to a search of: "Boilerplate", "Template", "Generators", "Boilerplates", "Templates"
150+
[Alias("TemplateDirectory")]
151+
[string[]]$BoilerplateDirectory = @("Boilerplate", "Template", "Generators", "Boilerplates", "Templates"),
150152

151153
# Output the ModuleInfo of the "built" module
152154
[switch]$Passthru

Source/Public/Invoke-ScriptGenerator.ps1

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,34 @@ function Invoke-ScriptGenerator {
7474
$null = $Parameters.Remove("Generator")
7575
}
7676

77+
# To make things more usable, resolve paths to "boilerplate" or "template" files based on our BoilerplateDirectory (alias TemplateDirectory)
78+
try {
79+
if ($Parameters.ContainsKey("Boilerplate")) {
80+
# If there's a Boilerplate parameter and it does not point at a file, check in the BoilerplateDirectory, and update it, if we find it.
81+
if ($Parameters.Boilerplate -and -not (Test-Path $Parameters.Boilerplate)) {
82+
if ($BoilerPlate = Join-Path $BoilerplateDirectory $Parameters.Boilerplate | Where-Object { Test-Path $_ }) {
83+
$Parameters.Boilerplate = $BoilerPlate
84+
}
85+
}
86+
} elseif ($Parameters.ContainsKey("Template")) {
87+
# If there's a Template parameter and it does not point at a file, check in the BoilerplateDirectory, and update it, if we find it.
88+
if ($Parameters.Template -and -not (Test-Path $Parameters.Template)) {
89+
if ($Template = Join-Path $BoilerplateDirectory $Parameters.Template | Where-Object { Test-Path $_ }) {
90+
$Parameters.Template = $Template
91+
}
92+
}
93+
}
94+
} catch {
95+
Write-Debug "Could not resolve the Boilerplate/Template"
96+
}
97+
7798
if (-not $Generator) {
7899
Write-Error "Generator missconfiguration. The Generator name is mandatory."
79100
continue
80101
}
81102

82103
# Find that generator...
83-
$GeneratorCmd = Get-Command -Name ${Generator} <# -CommandType Function #> -ParameterType Ast -ErrorAction Ignore
104+
$GeneratorCmd = Get-Command -Name ${Generator} -ParameterType Ast -ErrorAction Ignore <# -CommandType Function #>
84105
| Where-Object { $_.OutputType.Name -eq "TextReplacement" -or ($_.CommandType -eq "Alias" -and $_.Definition -like "PesterMock*" ) }
85106
| Select-Object -First 1
86107

0 commit comments

Comments
 (0)