-
Notifications
You must be signed in to change notification settings - Fork 52
/
OutGridviewCmdletCommand.cs
179 lines (145 loc) · 5.83 KB
/
OutGridviewCmdletCommand.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Internal;
using OutGridView.Models;
using System.Linq;
namespace OutGridView.Cmdlet
{
/// Enum for SelectionMode parameter.
/// </summary>
[Cmdlet(VerbsData.Out, "GridView", DefaultParameterSetName = "PassThru")]
[Alias("ogv")]
public class OutGridViewCmdletCommand : PSCmdlet
{
#region Properties
private const string DataNotQualifiedForGridView = "DataNotQualifiedForGridView";
private List<PSObject> PSObjects = new List<PSObject>();
#endregion Properties
#region Input Parameters
/// <summary>
/// This parameter specifies the current pipeline object.
/// </summary>
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject { get; set; } = AutomationNull.Value;
/// <summary>
/// Gets/sets the title of the Out-GridView window.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string Title { get; set; }
/// <summary>
/// Get or sets a value indicating whether the cmdlet should wait for the window to be closed.
/// </summary>
[Parameter(ParameterSetName = "Wait")]
public SwitchParameter Wait { get; set; }
/// <summary>
/// Get or sets a value indicating whether the selected items should be written to the pipeline
/// and if it should be possible to select multiple or single list items.
/// </summary>
[Parameter(ParameterSetName = "OutputMode")]
public OutputModeOption OutputMode { set; get; }
/// <summary>
/// Gets or sets a value indicating whether the selected items should be written to the pipeline.
/// Setting this to true is the same as setting the OutputMode to Multiple.
/// </summary>
[Parameter(ParameterSetName = "PassThru")]
public SwitchParameter PassThru
{
set { this.OutputMode = value.IsPresent ? OutputModeOption.Multiple : OutputModeOption.None; }
get { return OutputMode == OutputModeOption.Multiple ? new SwitchParameter(true) : new SwitchParameter(false); }
}
#endregion Input Parameters
// This method gets called once for each cmdlet in the pipeline when the pipeline starts executing
protected override void BeginProcessing()
{
}
// This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called
protected override void ProcessRecord()
{
if (InputObject == null || InputObject == AutomationNull.Value)
{
return;
}
IDictionary dictionary = InputObject.BaseObject as IDictionary;
if (dictionary != null)
{
// Dictionaries should be enumerated through because the pipeline does not enumerate through them.
foreach (DictionaryEntry entry in dictionary)
{
ProcessObject(PSObject.AsPSObject(entry));
}
}
else
{
ProcessObject(InputObject);
}
}
protected override void StopProcessing()
{
if (this.Wait || this.OutputMode != OutputModeOption.None)
{
AvaloniaProcessBridge.CloseProcess();
}
}
private void ProcessObject(PSObject input)
{
object baseObject = input.BaseObject;
// Throw a terminating error for types that are not supported.
if (baseObject is ScriptBlock ||
baseObject is SwitchParameter ||
baseObject is PSReference ||
baseObject is PSObject)
{
ErrorRecord error = new ErrorRecord(
new FormatException("Invalid data type for Out-GridView"),
DataNotQualifiedForGridView,
ErrorCategory.InvalidType,
null);
this.ThrowTerminatingError(error);
}
PSObjects.Add(input);
}
// This method will be called once at the end of pipeline execution; if no input is received, this method is not called
protected override void EndProcessing()
{
base.EndProcessing();
//Return if no objects
if (PSObjects.Count == 0)
{
return;
}
//TODO: Inject current powershell runtime
var TG = new TypeGetter(PowerShell.Create());
FormatViewDefinition fvd = TG.GetFormatViewDefinitonForObject(PSObjects.First());
var dataTable = TypeGetter.CastObjectsToTableView(PSObjects, fvd);
var applicationData = new ApplicationData
{
Title = Title,
OutputMode = OutputMode,
PassThru = PassThru,
DataTable = dataTable
};
AvaloniaProcessBridge.Start(applicationData);
if (this.Wait || this.OutputMode != OutputModeOption.None)
{
AvaloniaProcessBridge.WaitForExit();
}
var selectedIndexes = AvaloniaProcessBridge.SelectedIndexes;
if (selectedIndexes == null)
return;
foreach (int idx in selectedIndexes)
{
var selectedObject = PSObjects[idx];
if (selectedObject == null)
{
continue;
}
this.WriteObject(selectedObject, false);
}
}
}
}