functions/other/Select-PSUFunctionCode.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function Select-PSUFunctionCode
{
<#
    .SYNOPSIS
        Function that shows you the definition of a function and allows you to select lines to copy to your clipboard.
     
    .DESCRIPTION
        Function that shows you the definition of a function and allows you to select lines to copy to your clipboard.
        After running this command you will see a GridView pop up. Select as many lines of code as you would like and select
        ok to copy them to your clipboard.
     
    .PARAMETER Function
        A description of the Function parameter.
     
    .PARAMETER NoWait
        Shows function code in gridview and returns control without waiting for the window to close
     
    .PARAMETER PassThru
        Presents input command(s) in gridview, selected lines (if any) get returned as output
     
    .PARAMETER NoTrim
        If enabled, the white space will not be trimmed.
     
    .PARAMETER EnableException
        Replaces user friendly yellow warnings with bloody red exceptions of doom!
        Use this if you want the function to throw terminating errors you want to catch.
     
    .EXAMPLE
        PS C:\> Select-PSUFunctionCode -function 'Start-PSUTimer'
         
        This will open up the code for the function Start-PSUTimer in a GridView window.
     
    .EXAMPLE
        PS C:\> Get-Command timer | Select-PSUFunctionCode
         
        You can also pipe functions in.
     
    .NOTES
        Author: Andrew Pla
#>

    
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $Function,
        
        [Alias('w')]
        [Parameter(ParameterSetName = 'NoWait')]
        [switch]
        $NoWait,
        
        [Alias('p')]
        [Parameter(ParameterSetName = 'PassThru')]
        [switch]
        $PassThru,
        
        [Alias('t')]
        [switch]
        $NoTrim,
        
        [switch]
        $EnableException
    )
    
    begin
    {
        Write-PSFMessage -Level InternalComment -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")" -Tag 'debug', 'start', 'param'
        
        if ($PSVersionTable.PSVersion.Major -ge 6)
        {
            Stop-PSFFunction -Message "This command is not supported on PowerShell v6 or later!" -Category NotEnabled -Tag fail, ps6
            return
        }
        
        $FinalArray = [System.Collections.Arraylist]@()
    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }
        
        #region Process Items
        :main foreach ($item in $Function)
        {
            #region Resolve Command
            try { $command = Get-Command $item -ErrorAction Stop }
            catch { Stop-PSFFunction -Message "Failed to resolve command $item" -Tag fail -ErrorRecord $_ -Target $item -Continue -ContinueLabel main -EnableException $EnableException }
            
            switch ($command.CommandType)
            {
                'Alias'
                {
                    if ($command.ResolvedCommand.CommandType -eq "Function")
                    {
                        $functionText = $command.ResolvedCommand | Expand-PSUObject | Split-PSUString "`n"
                    }
                    else
                    {
                        Stop-PSFFunction -Message "$($command.ResolvedCommand.CommandType) not supported: The alias $item resolves to $($command.ResolvedCommand). Please supply a function or an alias for a function." -Tag fail -Target $item -Continue -ContinueLabel main -EnableException $EnableException
                    }
                }
                'Function'
                {
                    $functionText = $command | Expand-PSUObject | Split-PSUString "`n"
                }
                default
                {
                    Stop-PSFFunction -Message "$($command.CommandType) not supported: $item. Please supply a function or an alias for a function." -Tag fail -Target $item -Continue -ContinueLabel main -EnableException $EnableException
                }
            }
            #endregion Resolve Command
            
            #region Process Definition content
            $count = 1
            foreach ($line in $functionText)
            {
                $Object = [PSCustomObject]@{
                    LineNumber       = $Count
                    Text           = $line
                    Function       = $item
                }
                
                $count++
                
                if ($NoTrim)
                {
                    $null = $FinalArray.add($Object)
                }
                else
                {
                    if (-not ([string]::IsNullOrWhiteSpace($line)))
                    {
                        $null = $FinalArray.add($Object)
                    }
                }
            }
            #endregion Process Definition content
        }
        #endregion Process Items
    }
    
    end
    {
        if (Test-PSFFunctionInterrupt) { return }
        
        # This is the default behavior with no params
        if (-not ($NoWait -or $PassThru))
        {
            $data = $FinalArray | Out-GridView -PassThru -Title 'Select-PSFunctionCode' | Expand-PSUObject text
            if ($data) { $data | Set-Clipboard }
        }
        
        if ($NoWait)
        {
            $FinalArray | Out-GridView -Title 'Select-PSFunctionCode'
        }
        if ($PassThru)
        {
            $FinalArray | Out-GridView -PassThru -Title 'Select-PSFunctionCode'
        }
    }
}

Import-PSUAlias -Name "inspect" -Command "Select-PSUFunctionCode"