Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PSModuleDevelopment/PSModuleDevelopment.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Version number of this module.

ModuleVersion = '2.2.12.171'
ModuleVersion = '2.2.12.172'

# ID used to uniquely identify this module
GUID = '37dd5fce-e7b5-4d57-ac37-832055ce49d6'
Expand All @@ -29,7 +29,7 @@
# this module
RequiredModules = @(
@{ ModuleName = 'PSFramework'; ModuleVersion = '1.12.346' }
@{ ModuleName = 'string'; ModuleVersion = '1.1.3' }
@{ ModuleName = 'string'; ModuleVersion = '1.1.5' }
)

# Assemblies that must be loaded prior to importing this module
Expand Down
Binary file modified PSModuleDevelopment/bin/PSModuleDevelopment.dll
Binary file not shown.
Binary file modified PSModuleDevelopment/bin/PSModuleDevelopment.pdb
Binary file not shown.
30 changes: 29 additions & 1 deletion PSModuleDevelopment/bin/PSModuleDevelopment.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions PSModuleDevelopment/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.2.12.172 (2024-10-06)

+ Fix: Invoke-PSMDTemplate - On WinPS, script-logic on templates is not executed

## 2.2.12.171 (2024-10-04)

+ Upd: Raised PSFramework dependency due to critical security update
Expand Down
4 changes: 2 additions & 2 deletions PSModuleDevelopment/functions/templating/Get-PSMDTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@
#region Scan folders
if (Test-PSFParameterBinding -ParameterName "Path")
{
$templateInfos = Get-ChildItem -Path $Path -Filter "$($TemplateName)-*.Info.xml" | Where-Object { ($_.Name -replace "-\d+(\.\d+){0,3}.Info.xml$") -like $TemplateName }
$templateInfos = Get-ChildItem -Path $Path -Filter "$($TemplateName)-*-Info.xml" | Where-Object { ($_.Name -replace "-\d+(\.\d+){0,3}-Info.xml$") -like $TemplateName }

foreach ($info in $templateInfos)
{
$data = Import-PSFClixml $info.FullName
$data.Path = $info.FullName -replace '\.Info\.xml$','.xml'
$data.Path = $info.FullName -replace '-Info\.xml$','.xml'
$prospects += $data
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@
[OutputType([PSModuleDevelopment.Template.TemplateResult])]
[CmdletBinding()]
param (
[PSModuleDevelopment.Template.TemplateItemBase]
# Fixing that in the next PSFramework release: https://github.com/PowershellFrameworkCollective/psframework/issues/646
#[PSFramework.Utility.ScriptTransformation('PSModuleDevelopment.TemplateItem', [PSModuleDevelopment.Template.TemplateItemBase])]
#[PSModuleDevelopment.Template.TemplateItemBase]
$Item,

[string]
Expand Down
8 changes: 7 additions & 1 deletion PSModuleDevelopment/internal/scripts/initialize.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ if (-not (Test-Path (Get-PSFConfigValue -FullName 'PSModuleDevelopment.Debug.Con
#endregion Ensure Config path exists

# Pass on the host UI to the library
[PSModuleDevelopment.Utility.UtilityHost]::RawUI = $host.UI.RawUI
[PSModuleDevelopment.Utility.UtilityHost]::RawUI = $host.UI.RawUI

# Register Type-Conversion to fix template issues in serialization edge-casaes
Register-PSFArgumentTransformationScriptblock -Name 'PSModuleDevelopment.TemplateItem' -Scriptblock {
if ($_ -is [PSModuleDevelopment.Template.TemplateItemBase]) { return $_ }
[PSModuleDevelopment.Template.TemplateHost]::GetTemplateItem($_)
}
40 changes: 40 additions & 0 deletions PSModuleDevelopment/tests/functions/Templates.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Describe "Verifying templating component" {
BeforeAll {
$outPath = (Get-Item -Path 'TestDrive:\').FullName
$resourcePath = Resolve-PSFPath -Path "$PSScriptRoot\..\resources"
$templateName = 'TestTemplate-{0}' -f (Get-Random)
}

It "Should Record the template correctly" {
{ New-PSMDTemplate -TemplateName $templateName -FilePath "$resourcePath\þnameþ.txt" -OutPath $outPath -EnableException -ErrorAction Stop } | Should -Not -Throw
$templateInfo = Get-PSMDTemplate -TemplateName $templateName -Path $outPath
$templateRaw = Import-PSFClixml -Path $templateInfo.Path
try { $template = [PSModuleDevelopment.Template.Template]$templateRaw }
catch {
Write-Warning "Conversion to template Failed!"
Write-Warning "======================================================================="
$_ | Format-List -Force | Out-Host
Write-Warning "======================================================================="
$_.Exception | Format-List -Force | Out-Host
Write-Warning "======================================================================="
throw
}
$template.Name | Should -Be $templateName
$template.Parameters.Count | Should -Be 1
$template.Scripts.Count | Should -Be 1
$template.Scripts.Values.ScriptBlock | Should -BeOfType ([scriptblock])
}

It "Should Invoke the template correctly" {
{ Invoke-PSMDTemplate -TemplateName $templateName -Path $outPath -OutPath $outPath -Name Test -EnableException } | Should -Not -Throw
$content = Get-Content -Path "TestDrive:\Test.txt" -ErrorAction Stop
$values = $content | ConvertFrom-StringData -ErrorAction Stop
$values.Name | Should -Be Test
$values.Value | Should -Be '123'
}

It "Should Remove the template correctly" {
{ Remove-PSMDTemplate -TemplateName $templateName -EnableException -Confirm:$false } | Should -Not -Throw
Get-PSMDTemplate -TemplateName $templateName | Should -BeNullOrEmpty
}
}
2 changes: 2 additions & 0 deletions PSModuleDevelopment/tests/resources/þnameþ.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name = þnameþ
Value = þ{ "123" }þ
4 changes: 2 additions & 2 deletions PSModuleDevelopment/xml/PSModuleDevelopment.Types.ps1xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Types>
<!-- PSModuleDevelopment.Template.ParameterScript -->
<Type>
<!-- <Type>
<Name>Deserialized.PSModuleDevelopment.Template.ParameterScript</Name>
<Members>
<MemberSet>
Expand Down Expand Up @@ -33,7 +33,7 @@
<TypeConverter>
<TypeName>PSFramework.Serialization.SerializationTypeConverter</TypeName>
</TypeConverter>
</Type>
</Type> -->

<!-- PSModuleDevelopment.Template.Template -->
<Type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ public Template(PSObject Item)
Description = Item.GetValue<string>("Description");
Author = Item.GetValue<string>("Author");
CreatedOn = Item.GetValue<DateTime>("CreatedOn");
foreach (object item in Item.GetValue<ArrayList>("Tags"))
foreach (object item in Item.GetValues("Tags"))
Tags.Add((string)item);
foreach (object item in Item.GetValue<ArrayList>("Parameters"))
foreach (object item in Item.GetValues("Parameters"))
Parameters.Add((string)item);
foreach (KeyValuePair<string, ParameterScript> entry in Item.GetDictionary<ParameterScript>("Scripts"))
foreach (KeyValuePair<string, ParameterScript> entry in Item.GetParameterScriptDictionary("Scripts"))
Scripts[entry.Key] =entry.Value;
// Parameters2 not used
foreach (object item in Item.GetValue<ArrayList>("Children"))
foreach (object item in Item.GetValues("Children"))
Children.Add(TemplateHost.GetTemplateItem(item));

Generation = Item.GetValue<int>("Generation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,33 @@ namespace PSModuleDevelopment.Template
/// <summary>
/// Static helpers for the template system
/// </summary>
internal static class TemplateHost
public static class TemplateHost
{
internal static TemplateItemBase GetTemplateItem(object Item)
/// <summary>
/// Convert a deserialized template item into a fully valid object of its type.
/// </summary>
/// <param name="Item">The PSObject to transform back into what it was meant to be</param>
/// <returns>The resultant TemplateItemBase</returns>
public static TemplateItemBase GetTemplateItem(object Item)
{
if (Item.GetType() == typeof(TemplateItemFile))
return (TemplateItemFile)Item;

TemplateItemFolder result = new TemplateItemFolder(); ;
PSObject PSItem = PSObject.AsPSObject(Item);
if (PSItem.TypeNames.Contains("Deserialized.PSModuleDevelopment.Template.TemplateItemFile"))
return new TemplateItemFile(PSItem);

foreach (object child in PSItem.GetValue<ArrayList>("Children"))
TemplateItemFolder result = new TemplateItemFolder();

foreach (object child in PSItem.GetValues("Children"))
result.Children.Add(GetTemplateItem(child));

result.Name = PSItem.GetValue<string>("Name");
result.RelativePath = PSItem.GetValue<string>("RelativePath");
result.Identifier = PSItem.GetValue<string>("Identifier");
foreach (string entry in PSItem.GetValue<ArrayList>("FileSystemParameterFlat"))
foreach (string entry in PSItem.GetValues("FileSystemParameterFlat"))
result.FileSystemParameterFlat.Add(entry);
foreach (string entry in PSItem.GetValue<ArrayList>("FileSystemParameterScript"))
foreach (string entry in PSItem.GetValues("FileSystemParameterScript"))
result.FileSystemParameterScript.Add(entry);

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using PSModuleDevelopment.Utility;

namespace PSModuleDevelopment.Template
{
Expand Down Expand Up @@ -31,5 +33,37 @@ public class TemplateItemFile : TemplateItemBase
/// List of script value insertion parameters for the plaintext file
/// </summary>
public List<string> ContentParameterScript = new List<string>();

/// <summary>
/// Creates an empty TemplateItemFile
/// </summary>
public TemplateItemFile()
{

}

/// <summary>
/// Creates a filled-out TemplateItemFile.
/// Usually called when deserializing a template.
/// </summary>
/// <param name="PSItem">The deserialized instance of this template item file.</param>
public TemplateItemFile(PSObject PSItem)
{
Name = PSItem.GetValue<string>("Name");
RelativePath = PSItem.GetValue<string>("RelativePath");
Identifier = PSItem.GetValue<string>("Identifier");
foreach (string entry in PSItem.GetValues("FileSystemParameterFlat"))
FileSystemParameterFlat.Add(entry);
foreach (string entry in PSItem.GetValues("FileSystemParameterScript"))
FileSystemParameterScript.Add(entry);

foreach (string entry in PSItem.GetValues("ContentParameterFlat"))
ContentParameterFlat.Add(entry);
foreach (string entry in PSItem.GetValues("ContentParameterScript"))
ContentParameterScript.Add(entry);

Value = PSItem.GetValue<string>("Value");
PlainText = PSItem.GetValue<bool>("PlainText");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using PSModuleDevelopment.Template;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -26,19 +27,36 @@ public static T GetValue<T>(this PSObject PSObject, string Name)
return (T)value.BaseObject;
}

/// <summary>
/// Get the Values!
/// Always as an arraylist, empty if null
/// </summary>
/// <param name="PSObject">The object to extend</param>
/// <param name="Name">>The name of the property that has the values</param>
/// <returns>The values</returns>
public static ArrayList GetValues(this PSObject PSObject, string Name)
{
if (null == PSObject || null == PSObject.Properties || null == PSObject.Properties[Name] || null == PSObject.Properties[Name]?.Value)
return new ArrayList();
PSObject value = PSObject.AsPSObject(PSObject.Properties[Name].Value);
if (null == value.BaseObject)
return new ArrayList();
return (ArrayList)value.BaseObject;
}

/// <summary>
/// Get a hashtable value!
/// </summary>
/// <typeparam name="T">The type of the values in hashtable</typeparam>
/// <param name="PSObject">The object to extend</param>
/// <param name="Name">The name of the property that has the hashtable</param>
/// <returns>The hashtable!</returns>
public static Dictionary<string, T> GetDictionary<T>(this PSObject PSObject, string Name)
public static Dictionary<string, ParameterScript> GetParameterScriptDictionary(this PSObject PSObject, string Name)
{
Hashtable temp = PSObject.GetValue<Hashtable>(Name);
Dictionary<string, T> result = new Dictionary<string, T>();
Dictionary<string, ParameterScript> result = new Dictionary<string, ParameterScript>();
foreach (DictionaryEntry pair in temp)
result[(string)pair.Key] = (T)pair.Value;
result[(string)pair.Key] = new ParameterScript((PSObject)pair.Value);

return result;
}
Expand Down