Skip to content

Commit 88a7372

Browse files
More resilient privatedata processing
1 parent 8c1fe48 commit 88a7372

File tree

1 file changed

+98
-58
lines changed

1 file changed

+98
-58
lines changed

PSFramework.NuGet/functions/Get/Update-PSFModuleManifest.ps1

+98-58
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
Stop-PSFFunction -String 'Update-PSFModuleManifest.Error.InvalidModuleReference' -StringValues $item -Target $item -EnableException $EnableException -Cmdlet $Cmdlet -Category InvalidArgument -Continue
151151
}
152152

153-
$data = @{ ModuleName = $item.ModuleName }
153+
$data = [ordered]@{ ModuleName = $item.ModuleName }
154154
if ($item.RequiredVersion) { $data.RequiredVersion = '{0}' -f $item.RequiredVersion }
155155
elseif ($item.ModuleVersion) { $data.ModuleVersion = '{0}' -f $item.ModuleVersion }
156156

@@ -219,15 +219,98 @@
219219
}
220220
#endregion Case: Key Does not exist
221221

222-
$newText = $Ast.Extent.Text.SubString(0,$start) + ($format -f $stringValue) + $Ast.Extent.SubString($end)
222+
$newText = $Ast.Extent.Text.SubString(0, $start) + ($format -f $stringValue) + $Ast.Extent.Text.SubString($end)
223223
[System.Management.Automation.Language.Parser]::ParseInput($newText, [ref]$null, [ref]$null)
224224
}
225+
226+
function Update-PrivateDataProperty {
227+
[OutputType([System.Management.Automation.Language.Ast])]
228+
[CmdletBinding()]
229+
param (
230+
[Parameter(Mandatory = $true)]
231+
[System.Management.Automation.Language.Ast]
232+
$Ast,
233+
234+
[string[]]
235+
$Tags,
236+
237+
[string]
238+
$LicenseUri,
239+
240+
[string]
241+
$IconUri,
242+
243+
[string]
244+
$ProjectUri,
245+
246+
[string]
247+
$ReleaseNotes,
248+
249+
[string]
250+
$Prerelease,
251+
252+
[object[]]
253+
$ExternalModuleDependencies,
254+
255+
[bool]
256+
$EnableException,
257+
$Cmdlet
258+
)
259+
260+
$mainHash = $Ast.FindAll({
261+
$args[0] -is [System.Management.Automation.Language.HashtableAst] -and
262+
$args[0].KeyValuePairs.Item1.Value -contains 'RootModule' -and
263+
$args[0].KeyValuePairs.Item1.Value -Contains 'ModuleVersion'
264+
}, $true)
265+
266+
$privateData = [ordered]@{
267+
PSData = [ordered]@{ }
268+
}
269+
$replacements = @{ }
270+
271+
$privateDataAst = $mainHash.KeyValuePairs | Where-Object { $_.Item1.Value -eq 'PrivateData' } | ForEach-Object { $_.Item2.PipelineElements[0].Expression }
272+
273+
if ($privateDataAst) {
274+
foreach ($pair in $privateDataAst.KeyValuePairs) {
275+
if ($pair.Item1.Value -ne 'PSData') {
276+
$id = "%PSF_$(Get-Random)%"
277+
$privateData[$pair.Item1.Value] = $id
278+
$replacements[$id] = $pair.Item2.Extent.Text
279+
continue
280+
}
281+
282+
foreach ($subPair in $pair.Item2.PipelineElements[0].Expression.KeyValuePairs) {
283+
$id = "%PSF_$(Get-Random)%"
284+
$privateData.PSData[$subPair.Item1.Value] = $id
285+
$replacements[$id] = $subPair.Item2.Extent.Text
286+
}
287+
}
288+
}
289+
290+
if ($Tags) { $privateData.PSData['Tags'] = $Tags }
291+
if ($LicenseUri) { $privateData.PSData['LicenseUri'] = $LicenseUri }
292+
if ($IconUri) { $privateData.PSData['IconUri'] = $IconUri }
293+
if ($ProjectUri) { $privateData.PSData['ProjectUri'] = $ProjectUri }
294+
if ($ReleaseNotes) { $privateData.PSData['ReleaseNotes'] = $ReleaseNotes }
295+
if ($Prerelease) { $privateData.PSData['Prerelease'] = $Prerelease }
296+
if ($ExternalModuleDependencies) { $privateData.PSData['ExternalModuleDependencies'] = ConvertTo-ModuleRequirement -InputObject $ExternalModuleDependencies -Cmdlet $Cmdlet -EnableException $killIt }
297+
298+
$privateDataString = $privateData | ConvertTo-Psd1 -Depth 5
299+
foreach ($pair in $replacements.GetEnumerator()) {
300+
$privateDataString = $privateDataString -replace "'$($pair.Key)'", $pair.Value
301+
}
302+
303+
if (-not $privateDataAst) {
304+
$newManifest = $ast.Extent.Text.Insert(($mainHash.Extent.EndOffset - 1), "PrivateData = $privateDataString`n")
305+
}
306+
else {
307+
$newManifest = $ast.Extent.Text.SubString(0, $privateDataAst.Extent.StartOffset) + $privateDataString + $ast.Extent.Text.SubString($privateDataAst.Extent.EndOffset)
308+
}
309+
[System.Management.Automation.Language.Parser]::ParseInput($newManifest, [ref]$null, [ref]$null)
310+
}
225311
#endregion Utility Functions
226312
}
227313
process {
228-
# If Nothing to do, do nothing
229-
if (-not ($Tags -or $LicenseUri -or $IconUri -or $ProjectUri -or $ReleaseNotes -or $Prerelease)) { return }
230-
231314
$killIt = $ErrorActionPreference -eq 'Stop'
232315

233316
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)
@@ -244,76 +327,33 @@
244327
}
245328

246329
#region Main Properties
247-
$stringProperties = 'Guid','Author','CompanyName','CopyRight','RootModule','ModuleVersion','Description','ProcessorArchitecture','PowerShellVersion','ClrVersion','DotNetFrameworkVersion','PowerShellHostName','PowerShellHostVersion','HelpInfoUri','DefaultCommandPrefix'
330+
$stringProperties = 'Guid', 'Author', 'CompanyName', 'CopyRight', 'RootModule', 'ModuleVersion', 'Description', 'ProcessorArchitecture', 'PowerShellVersion', 'ClrVersion', 'DotNetFrameworkVersion', 'PowerShellHostName', 'PowerShellHostVersion', 'HelpInfoUri', 'DefaultCommandPrefix'
248331
foreach ($property in $stringProperties) {
249332
if ($PSBoundParameters.Keys -notcontains $property) { continue }
250333
$ast = Update-ManifestProperty -Ast $ast -Property $property -Value $PSBoundParameters.$property -Type String
251334
}
252-
$stringArrayProperties = 'CompatiblePSEditions','TypesToProcess','FormatsToProcess','ScriptsToProcess','RequiredAssemblies','FileList','FunctionsToExport','AliasesToExport','VariablesToExport','CmdletsToExport','DscResourcesToExport'
335+
$stringArrayProperties = 'CompatiblePSEditions', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'RequiredAssemblies', 'FileList', 'FunctionsToExport', 'AliasesToExport', 'VariablesToExport', 'CmdletsToExport', 'DscResourcesToExport'
253336
foreach ($property in $stringArrayProperties) {
254337
if ($PSBoundParameters.Keys -notcontains $property) { continue }
255338
$ast = Update-ManifestProperty -Ast $ast -Property $property -Value $PSBoundParameters.$property -Type StringArray
256339
}
257-
$moduleProperties = 'RequiredModules','ModuleList','NestedModules'
340+
$moduleProperties = 'RequiredModules', 'ModuleList', 'NestedModules'
258341
foreach ($property in $moduleProperties) {
259342
if ($PSBoundParameters.Keys -notcontains $property) { continue }
260343
$ast = Update-ManifestProperty -Ast $ast -Property $property -Value ($PSBoundParameters.$property | ConvertTo-ModuleRequirement -EnableException $killIt -Cmdlet $Cmdlet) -Type StringArray
261344
}
262345
#endregion Main Properties
263346

264347
#region PrivateData Content
265-
$mainHash = $ast.FindAll({
266-
$args[0] -is [System.Management.Automation.Language.HashtableAst] -and
267-
$args[0].KeyValuePairs.Item1.Value -contains 'RootModule' -and
268-
$args[0].KeyValuePairs.Item1.Value -Contains 'ModuleVersion'
269-
}, $true)
270-
271-
$privateData = [ordered]@{
272-
PSData = [ordered]@{ }
273-
}
274-
$replacements = @{ }
275-
276-
$privateDataAst = $mainHash.KeyValuePairs | Where-Object { $_.Item1.Value -eq 'PrivateData' } | ForEach-Object { $_.Item2.PipelineElements[0].Expression }
277-
278-
if ($privateDataAst) {
279-
foreach ($pair in $privateDataAst.KeyValuePairs) {
280-
if ($pair.Item1.Value -ne 'PSData') {
281-
$id = "%PSF_$(Get-Random)%"
282-
$privateData[$pair.Item1.Value] = $id
283-
$replacements[$id] = $pair.Item2.Extent.Text
284-
continue
285-
}
286-
287-
foreach ($subPair in $pair.Item2.PipelineElements[0].Expression.KeyValuePairs) {
288-
$id = "%PSF_$(Get-Random)%"
289-
$privateData.PSData[$subPair.Item1.Value] = $id
290-
$replacements[$id] = $subPair.Item2.Extent.Text
291-
}
292-
}
293-
}
294-
295-
if ($Tags) { $privateData.PSData['Tags'] = $Tags }
296-
if ($LicenseUri) { $privateData.PSData['LicenseUri'] = $LicenseUri }
297-
if ($IconUri) { $privateData.PSData['IconUri'] = $IconUri }
298-
if ($ProjectUri) { $privateData.PSData['ProjectUri'] = $ProjectUri }
299-
if ($ReleaseNotes) { $privateData.PSData['ReleaseNotes'] = $ReleaseNotes }
300-
if ($Prerelease) { $privateData.PSData['Prerelease'] = $Prerelease }
301-
if ($ExternalModuleDependencies) { $privateData.PSData['ExternalModuleDependencies'] = ConvertTo-ModuleRequirement -InputObject $ExternalModuleDependencies -Cmdlet $Cmdlet -EnableException $killIt }
302-
303-
$privateDataString = $privateData | ConvertTo-Psd1 -Depth 5
304-
foreach ($pair in $replacements.GetEnumerator()) {
305-
$privateDataString = $privateDataString -replace "'$($pair.Key)'", $pair.Value
306-
}
307-
308-
if (-not $privateDataAst) {
309-
$newManifest = $ast.Extent.Text.Insert(($mainHash.Extent.EndOffset - 1), "PrivateData = $privateDataString`n")
310-
}
311-
else {
312-
$newManifest = $ast.Extent.Text.SubString(0, $privateDataAst.Extent.StartOffset) + $privateDataString + $ast.Extent.Text.SubString($privateDataAst.Extent.EndOffset)
348+
if ($Tags -or $LicenseUri -or $IconUri -or $ProjectUri -or $ReleaseNotes -or $Prerelease -or $ExternalModuleDependencies) {
349+
$updateParam = $PSBoundParameters | ConvertTo-PSFHashtable -ReferenceCommand Update-PrivateDataProperty
350+
$updateParam.Cmdlet = $Cmdlet
351+
$updateParam.EnableException = $killIt
352+
$ast = Update-PrivateDataProperty -Ast $ast @updateParam
313353
}
314354
#endregion PrivateData Content
315355

316-
if ($PassThru) { $newManifest }
317-
else { $newManifest | Set-Content -Path $Path }
356+
if ($PassThru) { $ast.Extent.Text }
357+
else { $ast.Extent.Text | Set-Content -Path $Path }
318358
}
319359
}

0 commit comments

Comments
 (0)