-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate Script Context.xml
More file actions
263 lines (217 loc) · 6.33 KB
/
Create Script Context.xml
File metadata and controls
263 lines (217 loc) · 6.33 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?xml version="1.0" encoding="utf-8" ?>
<DisMacro xmlns="http://www.skyline.be/dis">
<Description>Add a ScriptContext class that parses you script parameters.</Description>
<Author>SKYLINE2\AMA</Author>
<Script>
<Code>
<![CDATA[using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml.Linq;
using Create_Script_Context_1;
using SLDisMacros.Interfaces;
public class Script
{
public static void Run(IEngine engine)
{
// Load XML Document
string script = engine.Input.FileContent;
XDocument xDoc = XDocument.Parse(script);
XNamespace ns = "http://www.skyline.be/automation";
// Get the ScriptParameters
var parameters = xDoc.Descendants(ns + "ScriptParameter")
.Select(param => new ScriptParameter
{
Id = param.Attribute("id")?.Value,
Type = param.Attribute("type")?.Value,
Values = param.Attribute("values")?.Value,
Description = param.Element(ns + "Description")?.Value,
});
engine.LogToOutputWindow(String.Join(Environment.NewLine, parameters.Select(x => $"{x.Id},\t{x.Type},\t{x.Values},\t{x.Description}")));
// Create ScriptContext class
var builder = new TabbedStringBuilder();
builder.AppendLine("namespace Skyline.DataMiner.Automation");
builder.OpenCurlyBraces();
// Using namespaces
builder.AppendLine("using System;");
builder.AppendLine("using System.Linq;");
builder.AppendLine();
builder.AppendLine("using Newtonsoft.Json;");
builder.AppendLine();
builder.AppendLine("using Skyline.DataMiner.Automation;");
builder.AppendLine();
// ScriptContext
builder.AppendLine("public class ScriptContext");
builder.OpenCurlyBraces();
// Constructor
builder.AppendLine("public ScriptContext(IEngine engine)");
builder.OpenCurlyBraces();
builder.AppendLine("Engine = engine;");
builder.AppendLine();
foreach (var parameter in parameters)
{
builder.AppendLine($"{parameter.Description.Sanitize()} = GetScriptParam(\"{parameter.Description}\").Single();");
}
builder.CloseCurlyBraces();
// Properties
builder.AppendLine();
builder.AppendLine("public IEngine Engine { get; }");
foreach (var parameter in parameters)
{
builder.AppendLine();
builder.AppendLine($"public string {parameter.Description.Sanitize()} {{ get; }}");
}
// Methods
// GetScriptParam
builder.AppendLine();
builder.AppendLine("private string[] GetScriptParam(string name)");
builder.OpenCurlyBraces();
builder.AppendLine("var rawValue = Engine.GetScriptParam(name).Value;");
builder.AppendLine("if (String.IsNullOrEmpty(rawValue))");
builder.OpenCurlyBraces();
builder.AppendLine("throw new ArgumentException($\"Script Param '{name}' cannot be left empty.\");");
builder.CloseCurlyBraces();
builder.AppendLine();
builder.AppendLine("if (IsJsonArray(rawValue))");
builder.OpenCurlyBraces();
builder.AppendLine("return JsonConvert.DeserializeObject<string[]>(rawValue);");
builder.CloseCurlyBraces();
builder.AppendLine("else");
builder.OpenCurlyBraces();
builder.AppendLine("return new[] { rawValue };");
builder.CloseCurlyBraces();
builder.CloseCurlyBraces();
// IsJsonArray
builder.AppendLine();
builder.AppendLine("private static bool IsJsonArray(string json)");
builder.OpenCurlyBraces();
builder.AppendLine("try");
builder.OpenCurlyBraces();
builder.AppendLine("JArray.Parse(json);");
builder.AppendLine("return true;");
builder.CloseCurlyBraces();
builder.AppendLine("catch");
builder.OpenCurlyBraces();
builder.AppendLine("return false;");
builder.CloseCurlyBraces();
builder.CloseCurlyBraces();
builder.CloseCurlyBraces();
builder.CloseCurlyBraces();
// Copy to Clipboard
Clipboard.SetText(builder.ToString());
}
}
namespace Create_Script_Context_1
{
public static class StringExtensions
{
public static string Filtered(this string str)
{
return Regex.Replace(str, @"\W+", ""); // trim non-word characters
}
public static string FirstLetterToLower(this string input)
{
switch (input)
{
case null:
throw new ArgumentNullException(nameof(input));
case "":
throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
default:
return input.First().ToString().ToLower() + input.Substring(1);
}
}
public static string FirstLetterToUpper(this string input)
{
switch (input)
{
case null:
throw new ArgumentNullException(nameof(input));
case "":
throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
default:
return input.First().ToString().ToUpper() + input.Substring(1);
}
}
public static string Sanitize(this string input)
{
return input
.Replace(" ", String.Empty)
.Replace('-', '_')
.FirstLetterToUpper();
}
}
public class ScriptParameter
{
public string Id { get; set; }
public string Type { get; set; }
public string Values { get; set; }
public string Description { get; set; }
}
public class TabbedStringBuilder
{
private int tabIndex = 0;
private readonly StringBuilder sb = new StringBuilder();
public int Length { get => sb.Length; }
public TabbedStringBuilder RawAppend(string line)
{
sb.Append(line);
return this;
}
public TabbedStringBuilder Append(string line)
{
sb.Append(new String('\t', tabIndex));
sb.Append(line);
return this;
}
public TabbedStringBuilder RawAppendLine(string line)
{
sb.Append(line);
sb.Append(Environment.NewLine);
return this;
}
public TabbedStringBuilder AppendLine()
{
sb.Append(Environment.NewLine);
return this;
}
public TabbedStringBuilder AppendLine(string line)
{
sb.Append(new String('\t', tabIndex));
sb.Append(line);
sb.Append(Environment.NewLine);
return this;
}
public TabbedStringBuilder OpenCurlyBraces()
{
sb.Append(new String('\t', tabIndex));
sb.Append("{");
sb.Append(Environment.NewLine);
tabIndex++;
return this;
}
public TabbedStringBuilder CloseCurlyBraces(bool newLine = true)
{
tabIndex--;
sb.Append(new String('\t', tabIndex));
sb.Append("}");
if (newLine)
sb.Append(Environment.NewLine);
return this;
}
public TabbedStringBuilder Remove(int startIndex, int length)
{
sb.Remove(startIndex, length);
return this;
}
public override string ToString()
{
return sb.ToString();
}
}
}]]>
</Code>
</Script>
</DisMacro>