Repository navigation
New-ScriptFromTemplate #9
Replies: 5 comments 7 replies
|
Another quick thought - this function creates a script file or function. That is its job. It doesn't have to write to a file. That's what |
|
You’re right, I had started working on the Author name change in a different branch and got distracted/forgot to finish it. Definitely need to split the template into a separate file as well. Funny enough, my first version of this (before putting it into the module) did use a separate file for the template. I was experimenting with an “all in one file” approach, and I can now confidently say there are numerous benefits to splitting it out. PSScriptAnalyzer and platyPS did not love parsing that one. |
|
Thoughts on this placeholder for a default author name? |
|
Here's a proof-of-concept using separate template files. This would be the template text file. Replacements are indicated with @ signs like $temp = [System.Collections.Generic.List[string]]::new()
$temp.AddRange([string[]]$(Get-Content c:\scripts\jhFunctionTemplate.txt))
#define a hashtable to hold the values to replace in the template
$hash = @{
Name = 'Get-Busy'
PSVersion = '5.1'
Author = 'Jeff Hicks'
Date = (Get-Date).ToShortDateString()
Version = 0.1.0
Synopsis = 'This does something'
Description = 'This is a long description of what the function does'
Output = $Null
Alias = 'gb'
}
$hash.Keys | ForEach-Object {
$i = 0
#loop through the collection and find the index number of the matching key
#then replace the value in the template
Do {
$i = $temp.FindIndex($i, { $args[0] -match "@$_" })
if ($i -ge 0) {
Write-Host "Replace $_ at line $i" -ForegroundColor cyan
$temp[$i] = $temp[$i] -replace "@$_", $hash[$_]
$i++
}
} until ($i -eq -1)
}
#write update function to the pipeline.
#Let the user can decide what to do with it
$tempObviously, there are many ways to build the hashtable of values. |
Uh oh!
There was an error while loading. Please reload this page.
I recognize that you are using this as an accelerator for your work, but if you are publishing a module for the public to use, you need to consider more than just your use cases. For example, when I create a new script file, you show as the author. 😁
You might also consider adding
[ValidateNotNullorEmpty()]on the parameters. Maybe even a ValidateScript on the Path to verify the path exists. You can put all of your tests as parameter validations. You have no way of knowing how a user will pass parameter values to a function. Don't assume they are manually typing parameter values.As for the template, you might consider re-designing this so that the template is an external file resource. You can provide a default in the module. That way, I can define my own template. For example, I have standard code I use in all my Begin blocks. Of course, you need to provide direction on how the template should be layed out.
You've got a decent start, but scaffolding commands are tough to build, especially when you are building a tool for anyone to use.
All reactions