Skip to content

Commit 13827bd

Browse files
HeyItsGilbertCopilot
authored andcommitted
fix(json): 🐛 Improve error handling for file and template actions
* Added checks to ensure both `source` and `destination` properties are provided for `file` and `templateFile` actions. * Enhanced error messages for better clarity on missing properties. * Updated JSON manifest example to reflect the change from `file` to `directory` type for better structure.
1 parent fcec341 commit 13827bd

5 files changed

Lines changed: 27 additions & 946 deletions

File tree

Plaster/Private/ConvertFrom-JsonContentAction.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ function ConvertFrom-JsonContentAction {
8282
if ($modification.isRegex) {
8383
$originalElement.SetAttribute('expand', 'true')
8484
}
85-
$replaceElement.AppendChild($originalElement)
85+
[void]$replaceElement.AppendChild($originalElement)
8686

8787
$substituteElement = $XmlDocument.CreateElement('substitute', $TargetNamespace)
8888
$substituteElement.InnerText = $modification.replace
8989
$substituteElement.SetAttribute('expand', 'true')
90-
$replaceElement.AppendChild($substituteElement)
90+
[void]$replaceElement.AppendChild($substituteElement)
9191

9292
if ($modification.condition) {
9393
$replaceElement.SetAttribute('condition', $modification.condition)
9494
}
9595

96-
$element.AppendChild($replaceElement)
96+
[void]$element.AppendChild($replaceElement)
9797
}
9898
}
9999
}

Plaster/Private/ConvertFrom-JsonManifest.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function ConvertFrom-JsonManifest {
3939

4040
# Add metadata
4141
$metadataElement = $xmlDoc.CreateElement('metadata', $TargetNamespace)
42-
$manifest.AppendChild($metadataElement)
42+
[void]$manifest.AppendChild($metadataElement)
4343

4444
# Add metadata properties
4545
$metadataProperties = @('name', 'id', 'version', 'title', 'description', 'author', 'tags')
@@ -53,13 +53,13 @@ function ConvertFrom-JsonManifest {
5353
} else {
5454
$element.InnerText = $value
5555
}
56-
$metadataElement.AppendChild($element)
56+
[void]$metadataElement.AppendChild($element)
5757
}
5858
}
5959

6060
# Add parameters
6161
$parametersElement = $xmlDoc.CreateElement('parameters', $TargetNamespace)
62-
$manifest.AppendChild($parametersElement)
62+
[void]$manifest.AppendChild($parametersElement)
6363

6464
if ($jsonObject.parameters) {
6565
foreach ($param in $jsonObject.parameters) {
@@ -98,21 +98,21 @@ function ConvertFrom-JsonManifest {
9898
$choiceElement.SetAttribute('help', $choice.help)
9999
}
100100

101-
$paramElement.AppendChild($choiceElement)
101+
[void]$paramElement.AppendChild($choiceElement)
102102
}
103103
}
104104

105-
$parametersElement.AppendChild($paramElement)
105+
[void]$parametersElement.AppendChild($paramElement)
106106
}
107107
}
108108

109109
# Add content
110110
$contentElement = $xmlDoc.CreateElement('content', $TargetNamespace)
111-
$manifest.AppendChild($contentElement)
111+
[void]$manifest.AppendChild($contentElement)
112112

113113
foreach ($action in $jsonObject.content) {
114114
$actionElement = ConvertFrom-JsonContentAction -Action $action -XmlDocument $xmlDoc
115-
$contentElement.AppendChild($actionElement)
115+
[void]$contentElement.AppendChild($actionElement)
116116
}
117117

118118
Write-PlasterLog -Level Debug -Message "JSON to XML conversion completed successfully"

Plaster/Private/Test-JsonManifestContent.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ function Test-JsonManifestContent {
2323
}
2424
}
2525
'file' {
26-
if (-not $action.source -or -not $action.destination) {
26+
# Both source and destination cannot be empty/missing
27+
# Empty destination means copy to root, empty source would be directory (but should use 'directory' type)
28+
if ((-not $action.source -and -not $action.destination) -or
29+
(-not $action.PSObject.Properties['source'] -and -not $action.PSObject.Properties['destination'])) {
30+
throw "File action missing required 'source' or 'destination' property"
31+
}
32+
# At least one must be non-empty
33+
if ([string]::IsNullOrWhiteSpace($action.source) -and [string]::IsNullOrWhiteSpace($action.destination)) {
2734
throw "File action missing required 'source' or 'destination' property"
2835
}
2936
}
3037
'templateFile' {
31-
if (-not $action.source -or -not $action.destination) {
38+
# Both source and destination cannot be empty/missing
39+
# Empty destination means copy to root
40+
if ((-not $action.source -and -not $action.destination) -or
41+
(-not $action.PSObject.Properties['source'] -and -not $action.PSObject.Properties['destination'])) {
42+
throw "TemplateFile action missing required 'source' or 'destination' property"
43+
}
44+
# At least one must be non-empty
45+
if ([string]::IsNullOrWhiteSpace($action.source) -and [string]::IsNullOrWhiteSpace($action.destination)) {
3246
throw "TemplateFile action missing required 'source' or 'destination' property"
3347
}
3448
}

examples/plasterManifest.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@
163163
"condition": "${Options} -contains 'PSake'"
164164
},
165165
{
166-
"type": "file",
167-
"source": "",
166+
"type": "directory",
168167
"destination": "docs\\",
169168
"condition": "${Options} -contains 'platyPS'"
170169
},

0 commit comments

Comments
 (0)