Skip to content

Commit 4a90450

Browse files
feat: Import-TypeView -Command ( Fixes #236 )
1 parent a06fa38 commit 4a90450

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

Commands/Types.PS1XML/Import-TypeView.ps1

+27-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
[Alias('Prefix')]
2828
[string]$Namespace,
2929

30+
# Any number of commands to import.
31+
# These commands will converted into ScriptMethods.
32+
# Each ScriptMethod will it's original command name, within the typename `Commands`.
33+
# (if -Namespace is provided, the namespace will be prepended to the typename)
34+
# (only functions are currently supported)
35+
[Parameter(ValueFromPipelineByPropertyName)]
36+
[Alias('Commands')]
37+
[PSObject[]]
38+
$Command,
39+
3040
# Any file paths to exclude.
3141
[Parameter(ValueFromPipelineByPropertyName)]
3242
[SupportsWildcards()]
@@ -43,6 +53,21 @@
4353

4454
process {
4555

56+
# Commands are converted into ScriptMethods first.
57+
if ($Command) {
58+
$writeTypeViewSplat = @{
59+
TypeName = if ($Namespace) { "$Namespace.Commands" } else { 'Commands' }
60+
ScriptMethod = [Ordered]@{}
61+
}
62+
foreach ($cmd in $command) {
63+
if ($cmd -is [Management.Automation.FunctionInfo]) {
64+
$writeTypeViewSplat.ScriptMethod[$cmd.Name] = $cmd.ScriptBlock
65+
}
66+
}
67+
Write-TypeView @writeTypeViewSplat
68+
}
69+
70+
4671
<#
4772
In order to make something like inheritance work,
4873
we want to be able to define properties and methods a few places:
@@ -132,8 +157,7 @@
132157
}
133158
}
134159
$membersByType.Remove('*') # then remove it (so we don't do it twice).
135-
}
136-
160+
}
137161
:nextMember foreach ($mt in $membersByType.GetEnumerator() | Sort-Object Key) { # Walk thru the members by type
138162
$WriteTypeViewSplat = @{ # and create a hashtable to splat.
139163
TypeName = if ($Namespace) { "$Namespace.$($mt.Key)"} else {$mt.Key}
@@ -156,7 +180,7 @@
156180
157181
Starting a file with . will hide the property.
158182
159-
It should be noted that hidden ScriptMethods are not "private" Methods
183+
It should be noted that hidden ScriptMethods are not "private" Methods, they are just hidden.
160184
(though neither are C# private Methods, if you use Reflection).
161185
162186
#>

0 commit comments

Comments
 (0)