Skip to content

Commit 2d79f0e

Browse files
author
FriedrichWeinmann
committed
new command
1 parent a0d8f79 commit 2d79f0e

5 files changed

Lines changed: 579 additions & 1 deletion

File tree

‎PSModuleDevelopment/PSModuleDevelopment.psd1‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262

6363
# Functions to export from this module
6464
FunctionsToExport = @(
65-
'Get-HelpEx',
65+
'Expand-PSMDTypeName',
66+
'Get-HelpEx',
6667
'Get-ModuleDebug',
6768
'Import-ModuleDebug',
6869
'Measure-CommandEx',

‎PSModuleDevelopment/PSModuleDevelopment.psproj‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<Folder>functions\wix</Folder>
1616
<Folder>internal</Folder>
1717
<Folder>internal\configurations</Folder>
18+
<Folder>internal\scripts\NewFolder1</Folder>
19+
<Folder>functions\format</Folder>
20+
<Folder>functions\refactor</Folder>
21+
<Folder>functions\assembly</Folder>
1822
</Folders>
1923
<Files>
2024
<File Build="2">PSModuleDevelopment.psd1</File>
@@ -38,6 +42,9 @@
3842
<File Build="2" Shared="False" ReferenceFunction="Invoke-wix_ps1">internal\configurations\wix.ps1</File>
3943
<File Build="2" Shared="False" ReferenceFunction="Invoke-debug_ps1">internal\configurations\debug.ps1</File>
4044
<File Build="2" Shared="False" ReferenceFunction="Invoke-license_ps1">internal\scripts\license.ps1</File>
45+
<File Build="2" Shared="True" ReferenceFunction="Invoke-New-PSMDFormatTableDefinition_ps1">functions\format\New-PSMDFormatTableDefinition.ps1</File>
46+
<File Build="2" Shared="True" ReferenceFunction="Invoke-Rename-PSMDParameter_ps1">functions\refactor\Rename-PSMDParameter.ps1</File>
47+
<File Build="2" Shared="True" ReferenceFunction="Invoke-Expand-PSMDTypeName_ps1">functions\assembly\Expand-PSMDTypeName.ps1</File>
4148
</Files>
4249
<StartupScript>F:\Synchronized Data\Scripte\Powershell Studio\Projects\PSModuleDevelopment\Test-Module.ps1</StartupScript>
4350
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function Expand-PSMDTypeName
2+
{
3+
<#
4+
.SYNOPSIS
5+
Returns the full name of the input object's type, as well as the name of the types it inherits from, recursively until System.Object.
6+
7+
.DESCRIPTION
8+
Returns the full name of the input object's type, as well as the name of the types it inherits from, recursively until System.Object.
9+
10+
.PARAMETER InputObject
11+
The object whose typename to expand.
12+
13+
.EXAMPLE
14+
PS C:\> Expand-PSMDTypeName -InputObject "test"
15+
16+
Returns the typenames for the string test ("System.String" and "System.Object")
17+
#>
18+
[CmdletBinding()]
19+
Param (
20+
[Parameter(ValueFromPipeline = $true)]
21+
$InputObject
22+
)
23+
24+
process
25+
{
26+
foreach ($item in $InputObject)
27+
{
28+
if ($item -eq $null) { continue }
29+
30+
$type = $item.GetType()
31+
if ($type.FullName -eq "System.RuntimeType") { $type = $item }
32+
33+
$type.FullName
34+
35+
while ($type.FullName -ne "System.Object")
36+
{
37+
$type = $type.BaseType
38+
$type.FullName
39+
}
40+
}
41+
}
42+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
function New-PSMDFormatTableDefinition
2+
{
3+
<#
4+
.SYNOPSIS
5+
Generates a format XML for the input type.
6+
7+
.DESCRIPTION
8+
Generates a format XML for the input type.
9+
Currently, only tables are supported.
10+
11+
Note:
12+
Loading format files has a measureable impact on module import PER FILE.
13+
For the sake of performance, you should only generate a single file for an entire module.
14+
15+
.PARAMETER InputObject
16+
The object that will be used to generate the format XML for.
17+
Will not duplicate its work if multiple object of the same type are passed.
18+
19+
.PARAMETER IncludeProperty
20+
Only properties in this list will be included.
21+
22+
.PARAMETER ExcludeProperty
23+
Only properties not in this list will be included.
24+
25+
.PARAMETER Fragment
26+
The function will only return a partial Format-XML object.
27+
This is useful when combining multiple objects into a single file:
28+
Simply create a full format XML for the first type, then use fragments on the subsequent ones and paste them into the first one's structure.
29+
30+
.EXAMPLE
31+
PS C:\> Get-ChildItem | New-PSMDFormatTableDefinition
32+
33+
Generates a format xml for the objects in the current path (files and folders in most cases)
34+
35+
.EXAMPLE
36+
PS C:\> Get-ChildItem | New-PSMDFormatTableDefinition -IncludeProperty LastWriteTime, FullName
37+
38+
Creates a format xml that only includes the columns LastWriteTime, FullName
39+
#>
40+
[CmdletBinding()]
41+
Param (
42+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
43+
$InputObject,
44+
45+
[string[]]
46+
$IncludeProperty,
47+
48+
[string[]]
49+
$ExcludeProperty,
50+
51+
[switch]
52+
$Fragment
53+
)
54+
55+
begin
56+
{
57+
$typeNames = @()
58+
59+
#region Xml Definitions
60+
$xmlFull = @"
61+
<?xml version="1.0" encoding="utf-16"?>
62+
<Configuration>
63+
<ViewDefinitions>
64+
<!-- %TypeName% -->
65+
<View>
66+
<Name>%TypeName%</Name>
67+
<ViewSelectedBy>
68+
<TypeName>%TypeName%</TypeName>
69+
</ViewSelectedBy>
70+
<TableControl>
71+
<AutoSize/>
72+
<TableHeaders>
73+
%Headers%
74+
</TableHeaders>
75+
<TableRowEntries>
76+
<TableRowEntry>
77+
<TableColumnItems>
78+
%Columns%
79+
</TableColumnItems>
80+
</TableRowEntry>
81+
</TableRowEntries>
82+
</TableControl>
83+
</View>
84+
</ViewDefinitions>
85+
<Configuration>
86+
"@
87+
$xmlFragment = @"
88+
<!-- %TypeName% -->
89+
<View>
90+
<Name>%TypeName%</Name>
91+
<ViewSelectedBy>
92+
<TypeName>%TypeName%</TypeName>
93+
</ViewSelectedBy>
94+
<TableControl>
95+
<AutoSize/>
96+
<TableHeaders>
97+
%Headers%
98+
</TableHeaders>
99+
<TableRowEntries>
100+
<TableRowEntry>
101+
<TableColumnItems>
102+
%Columns%
103+
</TableColumnItems>
104+
</TableRowEntry>
105+
</TableRowEntries>
106+
</TableControl>
107+
</View>
108+
"@
109+
$xmlColumnHeaderItem = " <TableColumnHeader/>"
110+
$xmlColumnItem = @"
111+
<TableColumnItem>
112+
<PropertyName>%PropertyName%</PropertyName>
113+
</TableColumnItem>
114+
"@
115+
#endregion Xml Definitions
116+
}
117+
process
118+
{
119+
foreach ($object in $InputObject)
120+
{
121+
if ($object.PSObject.TypeNames[0] -in $typeNames) { continue }
122+
else { $typeNames += $object.PSObject.TypeNames[0] }
123+
124+
$typeName = $object.PSObject.TypeNames[0]
125+
$propertyNames = $object.PSOBject.Properties.Name
126+
127+
if ($IncludeProperty)
128+
{
129+
$propertyNames = $propertyNames | Where-Object { $_ -in $IncludeProperty }
130+
}
131+
132+
if ($ExcludeProperty)
133+
{
134+
$propertyNames = $propertyNames | Where-Object { $_ -notin $ExcludeProperty }
135+
}
136+
137+
if ($Fragment) { $baseXml = $xmlFragment }
138+
else { $baseXml = $xmlFull }
139+
140+
$baseHeader = ( ,$xmlColumnHeaderItem * $propertyNames.Count) -join ([System.Environment]::NewLine)
141+
142+
$columns = @()
143+
foreach ($property in $propertyNames)
144+
{
145+
$columns += $xmlColumnItem -replace "%PropertyName%", $property
146+
}
147+
$baseColumn = $columns -join ([System.Environment]::NewLine)
148+
149+
$baseXml -replace "%TypeName%", "$typeName" -replace "%Headers%", $baseHeader -replace "%Columns%", $baseColumn
150+
}
151+
}
152+
end
153+
{
154+
155+
}
156+
}

0 commit comments

Comments
 (0)