-
Notifications
You must be signed in to change notification settings - Fork 52
/
TypeGetter.cs
191 lines (156 loc) · 6.69 KB
/
TypeGetter.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
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.PowerShell.Commands;
using OutGridView.Models;
namespace OutGridView.Cmdlet
{
public class TypeGetter
{
private PowerShell _powerShell;
public TypeGetter(PowerShell powerShellInstance)
{
_powerShell = powerShellInstance;
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
_powerShell.Runspace = rs;
Runspace.DefaultRunspace = rs;
}
public FormatViewDefinition GetFormatViewDefinitonForObject(PSObject obj)
{
var typeName = obj.BaseObject.GetType().FullName;
var types = _powerShell.AddScript("Get-FormatData " + typeName)
.Invoke<PSObject>();
//No custom type definitions found
if (types == null || types.Count == 0) return null;
var extendedTypeDefiniton = types[0].BaseObject as ExtendedTypeDefinition;
return extendedTypeDefiniton.FormatViewDefinition[0];
}
public static DataTableRow CastObjectToDataTableRow(PSObject ps, FormatViewDefinition fvd, int objectIndex)
{
var expressions = new List<PSPropertyExpression>();
//Just iterate properties if no type def is found
if (fvd == null)
{
foreach (var property in ps.Properties)
{
expressions.Add(new PSPropertyExpression(property.Name));
}
}
else
{
var tableControl = fvd.Control as TableControl;
var columns = tableControl.Rows[0].Columns;
foreach (var column in columns)
{
var displayEntry = column.DisplayEntry;
if (displayEntry.ValueType == DisplayEntryValueType.Property)
{
expressions.Add(new PSPropertyExpression(displayEntry.Value));
}
if (displayEntry.ValueType == DisplayEntryValueType.ScriptBlock)
{
expressions.Add(new PSPropertyExpression(ScriptBlock.Create(displayEntry.Value)));
}
}
}
// Stringify expression's results because we don't get the types from ExpressionResult
var stringData = expressions.Select(x =>
{
var result = x.GetValues(ps).FirstOrDefault().Result;
if (result == null)
{
return string.Empty;
}
return result.ToString();
});
var data = stringData
.Select<string, IValue>(x =>
{
var isDecimal = decimal.TryParse(x, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var decimalValue);
if (isDecimal)
{
return new DecimalValue
{
Value = x,
SortValue = decimalValue
};
}
return new StringValue
{
Value = x
};
}).ToList();
return new DataTableRow(data, objectIndex);
}
public static List<Type> GetTypesForColumns(List<DataTableRow> dataTableRows)
{
var dataRows = dataTableRows.Select(x => x.Values);
var types = dataRows.FirstOrDefault().Select(x => typeof(decimal)).ToList();
//If every value in a column could be a decimal, assume that it is supposed to be a decimal
foreach (var dataRow in dataRows)
{
for (var i = 0; i < dataRow.Count; i++)
{
var isNumber = dataRow.ElementAt(i) is DecimalValue;
if (!isNumber) types[i] = typeof(string);
}
}
return types;
}
public static List<DataTableColumn> GetDataColumnsForObject(PSObject ps, FormatViewDefinition fvd, List<Type> types)
{
var labels = new List<string>();
var propertyAccesors = new List<string>();
if (fvd == null)
{
labels = ps.Properties.Select(x => x.Name).ToList();
propertyAccesors = ps.Properties.Select(x => $"$_.{x.Name}").ToList();
}
else
{
var tableControl = fvd.Control as TableControl;
var definedColumnLabels = tableControl.Headers.Select(x => x.Label);
var displayEntries = tableControl.Rows[0].Columns.Select(x => x.DisplayEntry);
var propertyLabels = displayEntries.Select(x => x.Value);
//Use the TypeDefinition Label if availble otherwise just use the property name as a label
labels = definedColumnLabels.Zip(propertyLabels, (definedColumnLabel, propertyLabel) =>
{
if (String.IsNullOrEmpty(definedColumnLabel))
{
return propertyLabel;
}
return definedColumnLabel;
}).ToList();
propertyAccesors = displayEntries.Select(x =>
{
//If it's a propety access directly
if (x.ValueType == DisplayEntryValueType.Property)
{
return $"$_.{x.Value}";
}
//Otherwise return access script
return x.Value;
}).ToList();
}
var dataColumns = new List<DataTableColumn>();
for (var i = 0; i < labels.Count; i++)
{
dataColumns.Add(new DataTableColumn(labels[i], i, types[i].Name, propertyAccesors[i]));
}
return dataColumns;
}
public static DataTable CastObjectsToTableView(List<PSObject> psObjects, FormatViewDefinition fvd)
{
var formattedObjects = psObjects.Select((ps, idx) => CastObjectToDataTableRow(ps, fvd, idx)).ToList();
var columnTypes = GetTypesForColumns(formattedObjects);
var columnHeaders = GetDataColumnsForObject(psObjects.First(), fvd, columnTypes);
return new DataTable(columnHeaders, formattedObjects);
}
}
}