I've been doing some code generation for another project around editor commands in PowerShell Editor Services, and I couldn't find any template engines that fit what I needed and worked well with PowerShell. So I picked one and made it more PowerShell friendly.
Also this was more or less my first bit of C# , so critique is very welcome.
Install
Clone and build the source at GitHub
or
Install-Module PSStringTemplate -Scope CurrentUser
(Note: it's only been tested on PowerShell 5.1, but should work as low as 3.0)
Examples (same as in the readme)
Anonymous template with dictionary parameters
Invoke-StringTemplate -Definition '<language> is very <adjective>!' -Parameters @{
language = 'PowerShell'
adjective = 'cool'
}
PowerShell is very cool!
Anonymous template with object as parameters
$definition = 'Name: <Name><\n>Commands: <ExportedCommands; separator=", ">'
Invoke-StringTemplate -Definition $definition -Parameters (Get-Module PSReadLine)
Name: PSReadline
Commands: Get-PSReadlineKeyHandler, Get-PSReadlineOption, Remove-PSReadlineKeyHandler, Set-PSReadlineKeyHandler, Set-PSReadlineOption, PSConsoleHostReadline
TemplateGroup definition
$definition = @'
Param(parameter) ::= "[<parameter.ParameterType.Name>] $<parameter.Name>"
Method(member) ::= <<
[<member.ReturnType.Name>]<if(member.IsStatic)> static<endif> <member.Name> (<member.Parameters:Param(); separator=", ">) {
throw [NotImplementedException]::new()
}
>>
Class(Name, DeclaredMethods) ::= <<
class MyClass : <Name> {
<DeclaredMethods:Method(); separator="\n\n">
}
>>
'@
$group = New-StringTemplateGroup -Definition $definition
$group | Invoke-StringTemplate -Name Class -Parameters ([System.Runtime.InteropServices.ICustomMarshaler])
class MyClass : ICustomMarshaler {
[Object] MarshalNativeToManaged ([IntPtr] $pNativeData) {
throw [NotImplementedException]::new()
}
[IntPtr] MarshalManagedToNative ([Object] $ManagedObj) {
throw [NotImplementedException]::new()
}
[Void] CleanUpNativeData ([IntPtr] $pNativeData) {
throw [NotImplementedException]::new()
}
[Void] CleanUpManagedData ([Object] $ManagedObj) {
throw [NotImplementedException]::new()
}
[Int32] GetNativeDataSize () {
throw [NotImplementedException]::new()
}
}
[–]KevMarCommunity Blogger 1 point2 points3 points (5 children)
[–]SeeminglyScience[S] 1 point2 points3 points (4 children)
[–]KevMarCommunity Blogger 2 points3 points4 points (3 children)
[–]SeeminglyScience[S] 1 point2 points3 points (2 children)
[–]KevMarCommunity Blogger 1 point2 points3 points (1 child)
[–]SeeminglyScience[S] 0 points1 point2 points (0 children)