Skip to content

Commit 796534b

Browse files
committed
refactor(Plaster): ✨ Improve localized data import and error handling
* Refactored the import of localized data to use a splatting approach for better readability and maintainability. * Enhanced error handling during the import process to provide clearer warnings. * Updated the `Start-ProcessFile` function to improve parameter resolution with splatting. * Cleaned up the `Plaster.Resources.psd1` file by removing unnecessary comments and ensuring proper formatting.
1 parent 1fc47c7 commit 796534b

File tree

4 files changed

+125
-108
lines changed

4 files changed

+125
-108
lines changed

Plaster/Plaster.psm1

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,12 @@
11
# spell-checker:ignore Multichoice Assigments
2-
# Import localized data
3-
data LocalizedData {
4-
# culture="en-US"
5-
ConvertFrom-StringData @'
6-
DestPath_F1=Destination path: {0}
7-
ErrorFailedToLoadStoreFile_F1=Failed to load the default value store file: '{0}'.
8-
ErrorProcessingDynamicParams_F1=Failed to create dynamic parameters from the template's manifest file. Template-based dynamic parameters will not be available until the error is corrected. The error was: {0}
9-
ErrorTemplatePathIsInvalid_F1=The TemplatePath parameter value must refer to an existing directory. The specified path '{0}' does not.
10-
ErrorUnencryptingSecureString_F1=Failed to unencrypt value for parameter '{0}'.
11-
ErrorPathDoesNotExist_F1=Cannot find path '{0}' because it does not exist.
12-
ErrorPathMustBeRelativePath_F2=The path '{0}' specified in the {1} directive in the template manifest cannot be an absolute path. Change the path to a relative path.
13-
ErrorPathMustBeUnderDestPath_F2=The path '{0}' must be under the specified DestinationPath '{1}'.
14-
ExpressionInvalid_F2=The expression '{0}' is invalid or threw an exception. Error: {1}
15-
ExpressionNonTermErrors_F2=The expression '{0}' generated error output - {1}
16-
ExpressionExecError_F2=PowerShell expression failed execution. Location: {0}. Error: {1}
17-
ExpressionErrorLocationFile_F2=<{0}> attribute '{1}'
18-
ExpressionErrorLocationModify_F1=<modify> attribute '{0}'
19-
ExpressionErrorLocationNewModManifest_F1=<newModuleManifest> attribute '{0}'
20-
ExpressionErrorLocationParameter_F2=<parameter> name='{0}', attribute '{1}'
21-
ExpressionErrorLocationRequireModule_F2=<requireModule> name='{0}', attribute '{1}'
22-
ExpressionInvalidCondition_F3=The Plaster manifest condition '{0}' failed. Location: {1}. Error: {2}
23-
InterpolationError_F3=The Plaster manifest attribute value '{0}' failed string interpolation. Location: {1}. Error: {2}
24-
FileConflict=Plaster file conflict
25-
ManifestFileMissing_F1=The Plaster manifest file '{0}' was not found.
26-
ManifestMissingDocElement_F2=The Plaster manifest file '{0}' is missing the document element. It should be specified as <plasterManifest xmlns="{1}"></plasterManifest>.
27-
ManifestMissingDocTargetNamespace_F2=The Plaster manifest file '{0}' is missing or has an invalid target namespace on the document element. It should be specified as <plasterManifest xmlns="{1}"></plasterManifest>.
28-
ManifestPlasterVersionNotSupported_F2=The template file '{0}' specifies a plasterVersion of {1} which is greater than the installed version of Plaster. Update the Plaster module and try again.
29-
ManifestSchemaInvalidAttrValue_F5=Invalid '{0}' attribute value '{1}' on '{2}' element in file '{3}'. Error: {4}
30-
ManifestSchemaInvalidCondition_F3=Invalid condition '{0}' in file '{1}'. Error: {2}
31-
ManifestSchemaInvalidChoiceDefault_F3=Invalid default attribute value '{0}' for parameter '{1}' in file '{2}'. The default value must specify a zero-based integer index that corresponds to the default choice.
32-
ManifestSchemaInvalidMultichoiceDefault_F3=Invalid default attribute value '{0}' for parameter '{1}' in file '{2}'. The default value must specify one or more zero-based integer indexes in a comma separated list that correspond to the default choices.
33-
ManifestSchemaInvalidRequireModuleAttrs_F2=The requireModule attribute 'requiredVersion' for module '{0}' in file '{1}' cannot be used together with either the 'minimumVersion' or 'maximumVersion' attribute.
34-
ManifestSchemaValidationError_F2=Plaster manifest schema error in file '{0}'. Error: {1}
35-
ManifestSchemaVersionNotSupported_F2=The template's manifest schema version ({0}) in file '{1}' requires a newer version of Plaster. Update the Plaster module and try again.
36-
ManifestErrorReading_F1=Error reading Plaster manifest: {0}
37-
ManifestNotValid_F1=The Plaster manifest '{0}' is not valid.
38-
ManifestNotValidVerbose_F1=The Plaster manifest '{0}' is not valid. Specify -Verbose to see the specific schema errors.
39-
ManifestNotWellFormedXml_F2=The Plaster manifest '{0}' is not a well-formed XML file. {1}
40-
ManifestWrongFilename_F1=The Plaster manifest filename '{0}' is not valid. The value of the Path argument must refer to a file named 'plasterManifest.xml' or 'plasterManifest_<culture>.xml'. Change the Plaster manifest filename and then try again.
41-
MissingParameterPrompt_F1=<Missing prompt value for parameter '{0}'>
42-
NewModManifest_CreatingDir_F1=Creating destination directory for module manifest: {0}
43-
OpConflict=Conflict
44-
OpCreate=Create
45-
OpForce=Force
46-
OpIdentical=Identical
47-
OpMissing=Missing
48-
OpModify=Modify
49-
OpUpdate=Update
50-
OpVerify=Verify
51-
OverwriteFile_F1=Overwrite {0}
52-
ParameterTypeChoiceMultipleDefault_F1=Parameter name {0} is of type='choice' and can only have one default value.
53-
RequireModuleVerified_F2=The required module {0}{1} is already installed.
54-
RequireModuleMissing_F2=The required module {0}{1} was not found.
55-
RequireModuleMinVersion_F1=minimum version: {0}
56-
RequireModuleMaxVersion_F1=maximum version: {0}
57-
RequireModuleRequiredVersion_F1=required version: {0}
58-
ShouldCreateNewPlasterManifest=Create Plaster manifest
59-
ShouldProcessCreateDir=Create directory
60-
ShouldProcessExpandTemplate=Expand template file
61-
ShouldProcessNewModuleManifest=Create new module manifest
62-
TempFileOperation_F1={0} into temp file before copying to destination
63-
TempFileTarget_F1=temp file for '{0}'
64-
TestPlasterNoXmlSchemaValidationWarning=The version of .NET Core that PowerShell is running on does not support XML schema-based validation. Test-PlasterManifest will operate in "limited validation" mode primarily verifying the specified manifest file is well-formed XML. For full, XML schema-based validation, run this command on Windows PowerShell.
65-
UnrecognizedParametersElement_F1=Unrecognized manifest parameters child element: {0}.
66-
UnrecognizedParameterType_F2=Unrecognized parameter type '{0}' on parameter name '{1}'.
67-
UnrecognizedContentElement_F1=Unrecognized manifest content child element: {0}.
68-
'@
69-
}
70-
712
# Import localized data with improved error handling
723
try {
73-
Microsoft.PowerShell.Utility\Import-LocalizedData LocalizedData -FileName 'Plaster.Resources.psd1' -ErrorAction SilentlyContinue
4+
$importLocalizedDataSplat = @{
5+
BindingVariable = 'LocalizedData'
6+
FileName = 'Plaster.Resources.psd1'
7+
ErrorAction = 'SilentlyContinue'
8+
}
9+
Microsoft.PowerShell.Utility\Import-LocalizedData @importLocalizedDataSplat
7410
} catch {
7511
Write-Warning "Failed to import localized data: $_"
7612
}

Plaster/Private/Start-ProcessFile.ps1

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
# Processes both the <file> and <templateFile> directives.
22
function Start-ProcessFile {
3+
<#
4+
.SYNOPSIS
5+
Processes the <file> and <templateFile> directives in a Plaster template.
6+
7+
.DESCRIPTION
8+
This function processes the <file> and <templateFile> directives in a
9+
Plaster template.
10+
It resolves the source and destination paths, checks conditions, expands
11+
file source specifications,
12+
13+
.PARAMETER Node
14+
The XML node representing the <file> or <templateFile> directive.
15+
16+
.EXAMPLE
17+
Start-ProcessFile -Node $fileNode
18+
19+
Processes the specified file node, resolving paths and handling conditions.
20+
.NOTES
21+
This function is part of the Plaster module and is used internally to handle
22+
file processing in templates.
23+
#>
324
[CmdletBinding(SupportsShouldProcess = $true)]
425
param(
526
[ValidateNotNull()]
627
$Node
728
)
8-
$srcRelPath = Resolve-AttributeValue $Node.source (Get-ErrorLocationFileAttrVal $Node.localName source)
9-
$dstRelPath = Resolve-AttributeValue $Node.destination (Get-ErrorLocationFileAttrVal $Node.localName destination)
29+
$resolveAttributeValueSplat = @{
30+
Value = $Node.source
31+
Location = (Get-ErrorLocationFileAttrVal $Node.localName source)
32+
}
33+
$srcRelPath = Resolve-AttributeValue @resolveAttributeValueSplat
34+
35+
$resolveAttributeValueSplat = @{
36+
Value = $Node.destination
37+
Location = (Get-ErrorLocationFileAttrVal $Node.localName destination)
38+
}
39+
$dstRelPath = Resolve-AttributeValue @resolveAttributeValueSplat
1040

1141
$condition = $Node.condition
1242
if ($condition -and !(Test-ConditionAttribute $condition "'<$($Node.LocalName)>'")) {

Plaster/en-US/Plaster.Resources.psd1

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
# Localized PlasterResources.psd1
2-
32
ConvertFrom-StringData @'
4-
###PSLOC
53
DestPath_F1=Destination path: {0}
64
ErrorFailedToLoadStoreFile_F1=Failed to load the default value store file: '{0}'.
7-
ErrorProcessingDynamicParams_F1=Failed to create dynamic parameters from the template's manifest file. Template-based dynamic parameters will not be available until the error is corrected. The error was: {0}
8-
ErrorTemplatePathIsInvalid_F1=The TemplatePath parameter value must refer to an existing directory. The specified path '{0}' does not.
9-
ErrorUnencryptingSecureString_F1=Failed to unencrypt value for parameter '{0}'.
105
ErrorPathDoesNotExist_F1=Cannot find path '{0}' because it does not exist.
116
ErrorPathMustBeRelativePath_F2=The path '{0}' specified in the {1} directive in the template manifest cannot be an absolute path. Change the path to a relative path.
127
ErrorPathMustBeUnderDestPath_F2=The path '{0}' must be under the specified DestinationPath '{1}'.
13-
ExpressionInvalid_F2=The expression '{0}' is invalid or threw an exception. Error: {1}
14-
ExpressionNonTermErrors_F2=The expression '{0}' generated error output - {1}
15-
ExpressionExecError_F2=PowerShell expression failed execution. Location: {0}. Error: {1}
8+
ErrorProcessingDynamicParams_F1=Failed to create dynamic parameters from the template's manifest file. Template-based dynamic parameters will not be available until the error is corrected. The error was: {0}
9+
ErrorTemplatePathIsInvalid_F1=The TemplatePath parameter value must refer to an existing directory. The specified path '{0}' does not.
10+
ErrorUnencryptingSecureString_F1=Failed to unencrypt value for parameter '{0}'.
1611
ExpressionErrorLocationFile_F2=<{0}> attribute '{1}'
1712
ExpressionErrorLocationModify_F1=<modify> attribute '{0}'
1813
ExpressionErrorLocationNewModManifest_F1=<newModuleManifest> attribute '{0}'
1914
ExpressionErrorLocationParameter_F2=<parameter> name='{0}', attribute '{1}'
2015
ExpressionErrorLocationRequireModule_F2=<requireModule> name='{0}', attribute '{1}'
16+
ExpressionExecError_F2=PowerShell expression failed execution. Location: {0}. Error: {1}
17+
ExpressionInvalid_F2=The expression '{0}' is invalid or threw an exception. Error: {1}
2118
ExpressionInvalidCondition_F3=The Plaster manifest condition '{0}' failed. Location: {1}. Error: {2}
22-
InterpolationError_F3=The Plaster manifest attribute value '{0}' failed string interpolation. Location: {1}. Error: {2}
19+
ExpressionNonTermErrors_F2=The expression '{0}' generated error output - {1}
2320
FileConflict=Plaster file conflict
21+
InterpolationError_F3=The Plaster manifest attribute value '{0}' failed string interpolation. Location: {1}. Error: {2}
22+
ManifestErrorReading_F1=Error reading Plaster manifest: {0}
2423
ManifestFileMissing_F1=The Plaster manifest file '{0}' was not found.
2524
ManifestMissingDocElement_F2=The Plaster manifest file '{0}' is missing the document element. It should be specified as <plasterManifest xmlns="{1}"></plasterManifest>.
2625
ManifestMissingDocTargetNamespace_F2=The Plaster manifest file '{0}' is missing or has an invalid target namespace on the document element. It should be specified as <plasterManifest xmlns="{1}"></plasterManifest>.
26+
ManifestNotValid_F1=The Plaster manifest '{0}' is not valid.
27+
ManifestNotValidVerbose_F1=The Plaster manifest '{0}' is not valid. Specify -Verbose to see the specific schema errors.
28+
ManifestNotWellFormedXml_F2=The Plaster manifest '{0}' is not a well-formed XML file. {1}
2729
ManifestPlasterVersionNotSupported_F2=The template file '{0}' specifies a plasterVersion of {1} which is greater than the installed version of Plaster. Update the Plaster module and try again.
2830
ManifestSchemaInvalidAttrValue_F5=Invalid '{0}' attribute value '{1}' on '{2}' element in file '{3}'. Error: {4}
29-
ManifestSchemaInvalidCondition_F3=Invalid condition '{0}' in file '{1}'. Error: {2}
3031
ManifestSchemaInvalidChoiceDefault_F3=Invalid default attribute value '{0}' for parameter '{1}' in file '{2}'. The default value must specify a zero-based integer index that corresponds to the default choice.
32+
ManifestSchemaInvalidCondition_F3=Invalid condition '{0}' in file '{1}'. Error: {2}
3133
ManifestSchemaInvalidMultichoiceDefault_F3=Invalid default attribute value '{0}' for parameter '{1}' in file '{2}'. The default value must specify one or more zero-based integer indexes in a comma separated list that correspond to the default choices.
3234
ManifestSchemaInvalidRequireModuleAttrs_F2=The requireModule attribute 'requiredVersion' for module '{0}' in file '{1}' cannot be used together with either the 'minimumVersion' or 'maximumVersion' attribute.
3335
ManifestSchemaValidationError_F2=Plaster manifest schema error in file '{0}'. Error: {1}
3436
ManifestSchemaVersionNotSupported_F2=The template's manifest schema version ({0}) in file '{1}' requires a newer version of Plaster. Update the Plaster module and try again.
35-
ManifestErrorReading_F1=Error reading Plaster manifest: {0}
36-
ManifestNotValid_F1=The Plaster manifest '{0}' is not valid.
37-
ManifestNotValidVerbose_F1=The Plaster manifest '{0}' is not valid. Specify -Verbose to see the specific schema errors.
38-
ManifestNotWellFormedXml_F2=The Plaster manifest '{0}' is not a well-formed XML file. {1}
3937
ManifestWrongFilename_F1=The Plaster manifest filename '{0}' is not valid. The value of the Path argument must refer to a file named 'plasterManifest.xml' or 'plasterManifest_<culture>.xml'. Change the Plaster manifest filename and then try again.
4038
MissingParameterPrompt_F1=<Missing prompt value for parameter '{0}'>
4139
NewModManifest_CreatingDir_F1=Creating destination directory for module manifest: {0}
@@ -49,20 +47,19 @@ OpUpdate=Update
4947
OpVerify=Verify
5048
OverwriteFile_F1=Overwrite {0}
5149
ParameterTypeChoiceMultipleDefault_F1=Parameter name {0} is of type='choice' and can only have one default value.
52-
RequireModuleVerified_F2=The required module {0}{1} is already installed.
53-
RequireModuleMissing_F2=The required module {0}{1} was not found.
54-
RequireModuleMinVersion_F1=minimum version: {0}
5550
RequireModuleMaxVersion_F1=maximum version: {0}
51+
RequireModuleMinVersion_F1=minimum version: {0}
52+
RequireModuleMissing_F2=The required module {0}{1} was not found.
5653
RequireModuleRequiredVersion_F1=required version: {0}
54+
RequireModuleVerified_F2=The required module {0}{1} is already installed.
5755
ShouldCreateNewPlasterManifest=Create Plaster manifest
5856
ShouldProcessCreateDir=Create directory
5957
ShouldProcessExpandTemplate=Expand template file
6058
ShouldProcessNewModuleManifest=Create new module manifest
6159
TempFileOperation_F1={0} into temp file before copying to destination
6260
TempFileTarget_F1=temp file for '{0}'
6361
TestPlasterNoXmlSchemaValidationWarning=The version of .NET Core that PowerShell is running on does not support XML schema-based validation. Test-PlasterManifest will operate in "limited validation" mode primarily verifying the specified manifest file is well-formed XML. For full, XML schema-based validation, run this command on Windows PowerShell.
62+
UnrecognizedContentElement_F1=Unrecognized manifest content child element: {0}.
6463
UnrecognizedParametersElement_F1=Unrecognized manifest parameters child element: {0}.
6564
UnrecognizedParameterType_F2=Unrecognized parameter type '{0}' on parameter name '{1}'.
66-
UnrecognizedContentElement_F1=Unrecognized manifest content child element: {0}.
67-
###PSLOC
6865
'@

0 commit comments

Comments
 (0)