From 4a904502070f3e6280e16843e48ed073a86dadf2 Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Sun, 7 Jul 2024 13:11:19 -0700 Subject: [PATCH] feat: Import-TypeView -Command ( Fixes #236 ) --- Commands/Types.PS1XML/Import-TypeView.ps1 | 30 ++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Commands/Types.PS1XML/Import-TypeView.ps1 b/Commands/Types.PS1XML/Import-TypeView.ps1 index 3ba7ed9..18381d9 100644 --- a/Commands/Types.PS1XML/Import-TypeView.ps1 +++ b/Commands/Types.PS1XML/Import-TypeView.ps1 @@ -27,6 +27,16 @@ [Alias('Prefix')] [string]$Namespace, + # Any number of commands to import. + # These commands will converted into ScriptMethods. + # Each ScriptMethod will it's original command name, within the typename `Commands`. + # (if -Namespace is provided, the namespace will be prepended to the typename) + # (only functions are currently supported) + [Parameter(ValueFromPipelineByPropertyName)] + [Alias('Commands')] + [PSObject[]] + $Command, + # Any file paths to exclude. [Parameter(ValueFromPipelineByPropertyName)] [SupportsWildcards()] @@ -43,6 +53,21 @@ process { + # Commands are converted into ScriptMethods first. + if ($Command) { + $writeTypeViewSplat = @{ + TypeName = if ($Namespace) { "$Namespace.Commands" } else { 'Commands' } + ScriptMethod = [Ordered]@{} + } + foreach ($cmd in $command) { + if ($cmd -is [Management.Automation.FunctionInfo]) { + $writeTypeViewSplat.ScriptMethod[$cmd.Name] = $cmd.ScriptBlock + } + } + Write-TypeView @writeTypeViewSplat + } + + <# In order to make something like inheritance work, we want to be able to define properties and methods a few places: @@ -132,8 +157,7 @@ } } $membersByType.Remove('*') # then remove it (so we don't do it twice). - } - + } :nextMember foreach ($mt in $membersByType.GetEnumerator() | Sort-Object Key) { # Walk thru the members by type $WriteTypeViewSplat = @{ # and create a hashtable to splat. TypeName = if ($Namespace) { "$Namespace.$($mt.Key)"} else {$mt.Key} @@ -156,7 +180,7 @@ Starting a file with . will hide the property. - It should be noted that hidden ScriptMethods are not "private" Methods + It should be noted that hidden ScriptMethods are not "private" Methods, they are just hidden. (though neither are C# private Methods, if you use Reflection). #>