internal/TEPP/input.Tepp.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
Register-PSFTeppScriptblock -Name PSUtil-Input-Object -ScriptBlock {
    [System.Management.Automation.Language.PipelineAst]$pipelineAst = $commandAst.parent
    $index = $pipelineAst.PipelineElements.IndexOf($commandAst)
    
    #region If it's the first command
    if ($index -lt 1)
    {
        return
    }
    #endregion If it's the first command
    
    $properties = @()
    $constraintsPositive = @()
    
    #region Process pre-commands
    $inputIndex = $index - 1
    :main while ($true)
    {
        if ($pipelineAst.PipelineElements[$inputIndex].CommandElements)
        {
            # Resolve command and fail if it breaks
            $command = Get-Command $pipelineAst.PipelineElements[$inputIndex].CommandElements[0].Value -ErrorAction Ignore
            if ($command -is [System.Management.Automation.AliasInfo]) { $command = $command.ResolvedCommand }
            if (-not $command) { break }
            
            switch ($command.Name)
            {
                'Where-Object' { $inputIndex = $inputIndex - 1; continue main }
                'Tee-Object' { $inputIndex = $inputIndex - 1; continue main }
                #region Select-Object
                'Select-Object'
                {
                    $firstAst = $pipelineAst.PipelineElements[$inputIndex].CommandElements | Where-Object { $_ -is [System.Management.Automation.Language.ArrayLiteralAst] } | Select-Object -First 1
                    
                    foreach ($element in $firstAst.Elements)
                    {
                        switch ($element.GetType().FullName)
                        {
                            'System.Management.Automation.Language.StringConstantExpressionAst'
                            {
                                $constraintsPositive += $element.Value
                                if ($element.Value -notmatch "\*") { $properties += $element.Value }
                            }
                            'System.Management.Automation.Language.HashtableAst'
                            {
                                $constraintsPositive += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
                                $properties += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
                            }
                        }
                    }
                    $inputIndex = $inputIndex - 1;
                    continue main
                }
                #endregion Select-Object
                #region Select-PSFObject
                'Select-PSFObject'
                {
                    $firstAst = $pipelineAst.PipelineElements[$inputIndex].CommandElements | Where-Object { $_ -is [System.Management.Automation.Language.ArrayLiteralAst] } | Select-Object -First 1
                    foreach ($element in $firstAst.Elements)
                    {
                        switch ($element.GetType().FullName)
                        {
                            "System.Management.Automation.Language.StringConstantExpressionAst"
                            {
                                $par = [PSFramework.Parameter.SelectParameter]$element.Value
                                if ($par.Value -match "\*") { $constraintsPositive += $par.Value }
                                else
                                {
                                    if ($par.Value -is [System.String])
                                    {
                                        $properties += $par.Value
                                        $constraintsPositive += $par.Value
                                    }
                                    else
                                    {
                                        $properties += $par.Value["Name"]
                                        $constraintsPositive += $par.Value["Name"]
                                    }
                                }
                            }
                            "System.Management.Automation.Language.HashtableAst"
                            {
                                $properties += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
                                $constraintsPositive += ($element.KeyValuePairs | Where-Object Item1 -Match '^N$|^Name$' | Select-Object -First 1).Item2.ToString().Trim('"')
                            }
                        }
                    }
                    $inputIndex = $inputIndex - 1;
                }
                #endregion Select-PSFObject
                default { break main }
            }
        }
        
        else
        {
            break
        }
    }
    
    # Catch moving through _all_ options in the pipeline
    if ($inputIndex -lt 0) { return $properties }
    #endregion Process pre-commands
    
    
    #region Input from command
    if ($pipelineAst.PipelineElements[$inputIndex].CommandElements)
    {
        if ($command = Get-Command $pipelineAst.PipelineElements[$inputIndex].CommandElements[0].Value -ErrorAction Ignore)
        {
            switch ($command.Name)
            {
                #region Default for commands
                default
                {
                    foreach ($type in $command.OutputType.Type)
                    {
                        switch ($type.GetType().FullName)
                        {
                            'System.IO.FileInfo'
                            {
                                $properties += ($type.GetMembers("Instance, Public") | Where-Object MemberType -match "Field|Property").Name
                                $properties += 'PSChildName', 'PSDrive', 'PSIsContainer', 'PSParentPath', 'PSPath', 'PSProvider', 'BaseName'
                                break
                            }
                            'System.IO.DirectoryInfo'
                            {
                                $properties += ($type.GetMembers("Instance, Public") | Where-Object MemberType -match "Field|Property").Name
                                $properties += 'PSChildName', 'PSDrive', 'PSIsContainer', 'PSParentPath', 'PSPath', 'PSProvider', 'BaseName', 'VersionInfo'
                                break
                            }
                            default { $properties += ($type.GetMembers("Instance, Public") | Where-Object MemberType -match "Field|Property").Name }
                        }
                    }
                }
                #endregion Default for commands
            }
        }
    }
    #endregion Input from command
    
    #region Input from Variable
    if ($pipelineAst.PipelineElements[$inputIndex].Expression -and $pipelineAst.PipelineElements[0].Expression[0].VariablePath)
    {
        $properties += ((Get-Variable -Name $pipelineAst.PipelineElements[0].Expression[0].VariablePath.UserPath -ValueOnly) | Select-Object -First 1 | Get-Member -MemberType Properties).Name
    }
    #endregion Input from Variable
    
    $properties | Select-Object -Unique | Sort-Object | ForEach-Object {
        if (-not $constraintsPositive) { $_ }
        foreach ($constraint in $constraintsPositive)
        {
            if ($_ -like $constraint)
            {
                $_
                break
            }
        }
    }
}