Skip to content

Commit 81a7434

Browse files
feat: Import-TypeView Data File Caching ( Fixes #223 )
Data files become note properties upon first use
1 parent c05d138 commit 81a7434

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

Commands/Types.PS1XML/Import-TypeView.ps1

+21-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.Synopsis
55
Imports a Type View
66
.Description
7-
Imports a Type View, defined in a external file .method or .property file
7+
Imports a Type View, defined in a external file.
88
.Link
99
Write-TypeView
1010
.Example
@@ -111,7 +111,7 @@
111111

112112
# Congratulations! These very few lines are all it takes to enable inheritance!
113113
}
114-
}
114+
}
115115
}
116116
}
117117
}
@@ -333,7 +333,12 @@ data { $([ScriptBlock]::Create($fileText)) }
333333
$aliasProperty = (& $dataScriptBlock) -as [Collections.IDictionary]
334334
} elseif (-not $noteProperty.Contains($itemName)) {
335335
# otherwise, we load it in a ScriptProperty
336-
$scriptPropertyGet[$itemName] = $dataScriptBlock
336+
$scriptPropertyGet[$itemName] = @"
337+
`$dataObjects = $dataScriptBlock
338+
# Overwrite our own static property with an instance property.
339+
`$this.psobject.properties.add([PSNoteProperty]::new('$itemName', `$dataObjects), `$true)
340+
return `$dataObjects
341+
"@
337342
}
338343
}
339344
#endregion .psd1 Files
@@ -342,9 +347,12 @@ data { $([ScriptBlock]::Create($fileText)) }
342347
# Xml content is cached inline in a ScriptProperty and returned casted to [xml]
343348
if (-not $noteProperty.Contains($itemName)) {
344349
$scriptPropertyGet[$itemName] = [ScriptBlock]::Create(@"
345-
[xml]@'
350+
`$fileTextAsXml = [xml]@'
346351
$fileText
347352
'@
353+
# Overwrite our own static property with an instance property.
354+
`$this.psobject.properties.add([PSNoteProperty]::new('$itemName', `$fileTextAsXml), `$true)
355+
return `$fileTextAsXml
348356
"@)
349357
}
350358
}
@@ -354,9 +362,12 @@ $fileText
354362
# Json files are piped to ConvertFrom-Json
355363
if (-not $noteProperty.Contains($itemName)) {
356364
$scriptPropertyGet[$itemName] = [ScriptBlock]::Create(@"
357-
@'
365+
`$convertedFromJson = @'
358366
$fileText
359367
'@ | ConvertFrom-Json
368+
# Overwrite our own static property with an instance property.
369+
`$this.psobject.properties.add([PSNoteProperty]::new('$itemName', `$convertedFromJson), `$true)
370+
return `$convertedFromJson
360371
"@)
361372
}
362373
}
@@ -366,9 +377,12 @@ $fileText
366377
.clixml {
367378
if (-not $noteProperty.Contains($itemName)) {
368379
$scriptPropertyGet[$itemName] = [ScriptBlock]::Create(@"
369-
[Management.Automation.PSSerializer]::Deserialize(@'
380+
`$deserialized = [Management.Automation.PSSerializer]::Deserialize(@'
370381
$fileText
371382
'@)
383+
# Overwrite our own static property with an instance property.
384+
`$this.psobject.properties.add([PSNoteProperty]::new('$itemName', `$deserialized), `$true)
385+
return `$deserialized
372386
"@)
373387
}
374388
}
@@ -472,7 +486,7 @@ $stream.Dispose()
472486
}
473487

474488
if ($WriteTypeViewSplat.Count -gt 1) {
475-
$WriteTypeViewSplat.HideProperty = $hideProperty
489+
$WriteTypeViewSplat.HideProperty = $hideProperty
476490
Write-TypeView @WriteTypeViewSplat
477491
}
478492
}

0 commit comments

Comments
 (0)