-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathScriptPacksCommand.cs
More file actions
183 lines (157 loc) · 6.36 KB
/
ScriptPacksCommand.cs
File metadata and controls
183 lines (157 loc) · 6.36 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using ScriptCs.Contracts;
using ScriptCs.Extensions;
namespace ScriptCs.ReplCommands
{
public class ScriptPacksCommand : IReplCommand
{
private readonly IConsole _console;
public ScriptPacksCommand(IConsole console)
{
_console = console;
}
public string Description => "Displays information about script packs available in the REPL session";
public string CommandName => "scriptpacks";
public object Execute(IRepl repl, object[] args)
{
Guard.AgainstNullArgument("repl", repl);
var packContexts = repl.ScriptPackSession.Contexts;
if (packContexts.IsNullOrEmpty())
{
PrintInColor("There are no script packs available in this REPL session");
return null;
}
var importedNamespaces = repl.ScriptPackSession.Namespaces.IsNullOrEmpty() ? repl.Namespaces.ToArray() : repl.Namespaces.Union(repl.ScriptPackSession.Namespaces).ToArray();
foreach (var packContext in packContexts)
{
var contextType = packContext.GetType();
PrintInColor(contextType.ToString());
var methods = contextType.GetAllMethods();
var properties = contextType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
PrintMethods(methods, importedNamespaces);
PrintProperties(properties, importedNamespaces);
_console.WriteLine();
}
return null;
}
private void PrintInColor(string text)
{
var originalColor = _console.ForegroundColor;
_console.ForegroundColor = ConsoleColor.Yellow;
_console.WriteLine(text);
_console.ForegroundColor = originalColor;
}
private void PrintMethods(IEnumerable<MethodInfo> methods, string[] importedNamespaces)
{
if (!methods.IsNullOrEmpty())
{
_console.WriteLine("** Methods **");
foreach (var method in methods)
{
var methodParams = method.GetParametersWithoutExtensions()
.Select(p => string.Format("{0} {1}", GetPrintableType(p.ParameterType, importedNamespaces), p.Name));
var methodSignature = string.Format(" - {0} {1}({2})", GetPrintableType(method.ReturnType, importedNamespaces), method.Name,
string.Join(", ", methodParams));
_console.WriteLine(methodSignature);
}
_console.WriteLine();
}
}
private void PrintProperties(IEnumerable<PropertyInfo> properties, string[] importedNamespaces)
{
if (!properties.IsNullOrEmpty())
{
_console.WriteLine("** Properties **");
foreach (var property in properties)
{
var propertyBuilder = new StringBuilder(string.Format(" - {0} {1}", GetPrintableType(property.PropertyType, importedNamespaces), property.Name));
propertyBuilder.Append(" {");
if (property.GetGetMethod() != null)
{
propertyBuilder.Append(" get;");
}
if (property.GetSetMethod() != null)
{
propertyBuilder.Append(" set;");
}
propertyBuilder.Append(" }");
_console.WriteLine(propertyBuilder.ToString());
}
}
}
private string GetPrintableType(Type type, string[] importedNamespaces)
{
if (type.Name == "Void")
{
return "void";
}
if (type.Name == "Object")
{
return "object";
}
if (type.IsGenericType)
{
return BuildGeneric(type, importedNamespaces);
}
var nullableType = Nullable.GetUnderlyingType(type);
if (nullableType != null)
{
return string.Format("{0}?", GetPrintableType(nullableType, importedNamespaces));
}
switch (Type.GetTypeCode(type))
{
case TypeCode.Boolean:
return "bool";
case TypeCode.Byte:
return "byte";
case TypeCode.Char:
return "char";
case TypeCode.Decimal:
return "decimal";
case TypeCode.Double:
return "double";
case TypeCode.Int16:
return "short";
case TypeCode.Int32:
return "int";
case TypeCode.Int64:
return "long";
case TypeCode.SByte:
return "sbyte";
case TypeCode.Single:
return "Single";
case TypeCode.String:
return "string";
case TypeCode.UInt16:
return "UInt16";
case TypeCode.UInt32:
return "UInt32";
case TypeCode.UInt64:
return "UInt64";
default:
return string.IsNullOrEmpty(type.FullName) || importedNamespaces.Contains(type.Namespace) ? type.Name : type.FullName;
}
}
private string BuildGeneric(Type type, string[] importedNamespaces)
{
var baseName = type.Name.Substring(0, type.Name.IndexOf("`", StringComparison.Ordinal));
var genericDefinition = new StringBuilder(string.Format("{0}<", baseName));
var firstArgument = true;
foreach (var t in type.GetGenericArguments())
{
if (!firstArgument)
{
genericDefinition.Append(", ");
}
genericDefinition.Append(GetPrintableType(t, importedNamespaces));
firstArgument = false;
}
genericDefinition.Append(">");
return genericDefinition.ToString();
}
}
}