-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Write-FormatSelectionSet ( Fixes #228 )
- Loading branch information
1 parent
82ea997
commit 44b9c3c
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
function Write-FormatSelectionSet | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Writes a selection set. | ||
.DESCRIPTION | ||
Writes a format selection set. | ||
.NOTES | ||
Formatting is commonly selected by a TypeName. | ||
It can also be selected by a group of type names, which is called a SelectionSet. | ||
A few built-in formatters use selection sets, most notably the filesystem formatters. | ||
.EXAMPLE | ||
Write-FormatSelectionSet -Name 'FileSystemTypes' -TypeName 'System.IO.DirectoryInfo', 'System.IO.FileInfo' | ||
#> | ||
param( | ||
# The name of the selection set. | ||
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] | ||
[Alias('SelectionSetName','SelectionSet')] | ||
[string] | ||
$Name, | ||
|
||
# The type names in the selection set. | ||
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] | ||
[string[]] | ||
$TypeName | ||
) | ||
|
||
begin { | ||
# First, create a queue to hold the selection sets. | ||
$eachSelectionSet = [Collections.Queue]::new() | ||
} | ||
|
||
process { | ||
# Make sure the name and type names are XML-safe | ||
$NameAsXml = "<Name>$Name</Name>" -as [xml] | ||
if (-not $NameAsXml) { | ||
$Name = [Security.SecurityElement]::Escape($Name) | ||
} | ||
$TypeName = foreach ($tn in $TypeName) { | ||
if (-not ("<TypeName>$tn</TypeName>" -as [xml])) { | ||
[Security.SecurityElement]::Escape($tn) | ||
} else { | ||
$tn | ||
} | ||
} | ||
|
||
# Create the selection set XML | ||
$selectionSetXml = [xml](@( | ||
"<SelectionSet>" | ||
# Consisting of a `<Name>` | ||
"<Name>$Name</Name>" | ||
"<Types>" # and a `<Types>` element | ||
foreach ($tn in $TypeName) { | ||
# (containing each `<TypeName>`) | ||
"<TypeName>$tn</TypeName>" | ||
} | ||
"</Types>" | ||
"</SelectionSet>" | ||
) -join '') | ||
if (-not $selectionSetXml) { return } | ||
$xOut=[IO.StringWriter]::new() | ||
$selectionSetXml.Save($xOut) | ||
$eachSelectionSet.Enqueue("$xOut" -replace '\<\?xml.+?\?>[\s\r\n]+') | ||
$xOut.Dispose() | ||
} | ||
|
||
end { | ||
# Create the `<SelectionSets>` element | ||
$selectionSetXml = @( | ||
"<SelectionSets>" | ||
# (containing each `<SelectionSet>`) | ||
$eachSelectionSet.ToArray() | ||
"</SelectionSets>" | ||
) -join '' -as [xml] | ||
$xOut=[IO.StringWriter]::new() | ||
$selectionSetXml.Save($xOut) | ||
# Write the XML to the pipeline as a string, removing the XML declaration. | ||
"$xOut" -replace '\<\?xml.+?\?>[\s\r\n]+' | ||
$xOut.Dispose() | ||
} | ||
} |