-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsap.csx
More file actions
375 lines (319 loc) · 9.14 KB
/
Copy pathsap.csx
File metadata and controls
375 lines (319 loc) · 9.14 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// VS Code: Disable C# Dev Kit, in the C# extension enable useOmniSharp
// dotnet tool install -g csharprepl
// csharprepl
// #r "nuget: YamlDotNet, 17.0.1"
// #load "sap.csx"
// using sapfewse;
#r "robosapiens/lib/sapfewse.dll"
#r "robosapiens/lib/saprotwr.net.dll"
#r "nuget: YamlDotNet, 17.0.1"
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using sapfewse;
using saprotwr.net;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
object? ConvertJsonNode(JsonNode? node)
{
return node switch
{
JsonValue value => value.GetValue<object>().ToString(),
JsonArray array => array.Select(ConvertJsonNode).ToList(),
JsonObject obj => obj.ToDictionary(pair => pair.Key, pair => ConvertJsonNode(pair.Value)),
_ => null
};
}
string JsonToYaml(string json)
{
var jsonNode = JsonNode.Parse(json) ?? throw new InvalidOperationException("Invalid JSON");
var obj = ConvertJsonNode(jsonNode);
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
return serializer.Serialize(obj);
}
GuiSession getSession()
{
var rot = new CSapROTWrapper();
var sapGui = rot.GetROTEntry("SAPGUI") ?? throw new Exception("SAP Logon is not running.");
var sap = (GuiApplication)sapGui.GetType().InvokeMember(
"GetScriptingEngine",
BindingFlags.InvokeMethod,
null,
sapGui,
null
);
try
{
var connection = (GuiConnection)sap.Connections.ElementAt(0);
return (GuiSession)connection.Sessions.ElementAt(0);
}
catch (Exception)
{
throw new Exception("Not connected to any SAP system.");
}
}
var session = getSession();
static string capitalize(string s)
{
return char.ToUpper(s[0]) + s[1..];
}
record Event(string window, string componentId, string componentType, Locator? locator, string type, string name, List<object> values) {
public string serialize()
{
var formatValue = (object val) => val switch
{
bool b => val.ToString().ToLower(),
string s => $"\"{s}\"",
_ => val.ToString()
};
var target = $"""(({componentType})session.FindById("{componentId}")).{name}""";
return type switch
{
"Method" => target + "(" + string.Join(", ", values.Select(formatValue)) + ")",
"Property" => target + " = " + formatValue(values[0]),
_ => throw new Exception("Unknown event type")
};
}
public override string ToString()
{
return $"Type: {type} | Name: {name} | Values: {string.Join(", ", values)}";
}
}
var eventLog = new List<Event>();
void handleChange(GuiSession session, GuiComponent component, object commmandArray)
{
Console.WriteLine("============");
Console.WriteLine($"Id: {component.Id}");
Console.WriteLine($"Type: {component.Type}");
try
{
var commands = (object[])commmandArray;
var events = commands.Select(command => toEvent((object[])command, component)).ToList();
eventLog.AddRange(events);
Console.WriteLine("~~ Events ~~");
events.ForEach(Console.WriteLine);
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("============");
}
record Locator(string hLabel, string? vLabel=null, string? contents=null)
{
public override string ToString()
{
return $"{{ hLabel = {hLabel}, vlabel = {vLabel}, contents = {contents} }}";
}
}
record CellLocator(string row, string col)
{
public override string ToString()
{
return $"{{ row = {row}, col = {col} }}";
}
}
string getTooltip(GuiVComponent component)
{
if (component.DefaultTooltip != "")
{
return component.DefaultTooltip.Trim();
}
if (component.Tooltip != "")
{
return component.Tooltip.Trim();
}
return $"Component has no tooltip: {component.Id}";
}
string getButtonLabel(GuiButton button)
{
if (button.Text != "")
{
return button.Text.Trim();
}
return button.Tooltip.Trim();
}
string getTabLabel(GuiTab tab)
{
if (tab.Text != "")
{
return tab.Text.Trim();
}
return getTooltip((GuiVComponent)tab);
}
string getTextFieldLabel(GuiTextField textField)
{
if (textField.LeftLabel != null)
{
return textField.LeftLabel.Text.Trim();
}
return getTooltip((GuiVComponent)textField);
}
Locator? getLocator(GuiVComponent component)
{
return component switch
{
GuiButton button => new Locator(getButtonLabel(button)),
GuiTab tab => new Locator(getTabLabel(tab)),
GuiTextField textField => new Locator(getTextFieldLabel(textField)),
_ => null
};
}
public enum TreeType
{
Simple,
List,
Column
}
string getText(GuiTree tree, string nodeKey)
{
var treeType = (TreeType)tree.GetTreeType();
if (treeType == TreeType.List)
{
var texts = new List<string>();
for (int i = 1; i < tree.GetListTreeNodeItemCount(nodeKey)+1; i++)
{
var itemText = tree.GetItemText(nodeKey, i.ToString());
if (itemText != null && itemText.Trim() != "") {
texts.Add(itemText.Replace("/", "|"));
}
}
return string.Join(" ", texts);
}
return tree.GetNodeTextByKey(nodeKey).Replace("/", "|");
}
string getParentPath(string path)
{
var pathParts = path.Split("\\");
var parent_path = string.Join("\\", pathParts[0..^1]);
if (parent_path != "") {
return parent_path;
}
return "ROOT";
}
string getTextPath(GuiTree guiTree, string path, string textPath)
{
var parentPath = getParentPath(path);
if (parentPath == "ROOT") {
return textPath;
}
else {
var parentText = getText(guiTree, guiTree.GetNodeKeyByPath(parentPath));
return getTextPath(guiTree, parentPath, $"{parentText}/{textPath}");
}
}
string getTreeElementPath(GuiTree tree, string nodeKey)
{
var nodePath = tree.GetNodePathByKey(nodeKey);
var text = getText(tree, nodeKey);
return getTextPath(tree, nodePath, text);
}
Event toEvent(object[] command, GuiComponent component)
{
var type = command[0].ToString() switch {
"M" => "Method",
"SP" => "Property",
_ => throw new Exception("Unknown type")
};
var name = capitalize(command[1].ToString());
var values = command[2..].ToList();
var componentType = component.Type switch
{
"GuiDockShell" => "GuiContainerShell",
"GuiShell" => "Gui" + ((GuiShell)component).SubType,
_ => component.Type
};
var window = session.ActiveWindow.Text;
var locator = componentType switch
{
"GuiTree" => name switch
{
"DoubleClickItem" => new Locator(getTreeElementPath((GuiTree)component, values[0].ToString())),
_ => null
},
_ => getLocator((GuiVComponent)component)
};
return new Event(window, component.Id, componentType, locator, type, name, values);
}
void handleDestroy(GuiSession session)
{
recordStop();
}
void recordStart()
{
session.Change += handleChange;
session.Destroy += handleDestroy;
session.Record = true;
}
void recordStop()
{
session.Record = false;
session.Change -= handleChange;
session.Destroy -= handleDestroy;
}
void refresh()
{
session = getSession();
}
string getObjectTree(string componentId)
{
return session.GetObjectTree(
componentId,
new string[] { "Id", "Type", "SubType", "Top", "Left", "Width", "Height", "Text", "Tooltip"}
).Replace("\\", "");
}
void saveEventLog(string filename)
{
var json = JsonSerializer.Serialize(eventLog);
File.WriteAllText(
Path.Combine(Directory.GetCurrentDirectory(), filename),
JsonToYaml(json)
);
}
void saveRecording(string filename)
{
var preamble = """
// dotnet tool install -g dotnet-script
// dotnet script script.csx
#r "robosapiens/lib/sapfewse.dll"
#r "robosapiens/lib/saprotwr.net.dll"
using System.Reflection;
using sapfewse;
using saprotwr.net;
GuiSession getSession()
{
var rot = new CSapROTWrapper();
var sapGui = rot.GetROTEntry("SAPGUI") ?? throw new Exception("SAP Logon is not running.");
var sap = (GuiApplication)sapGui.GetType().InvokeMember(
"GetScriptingEngine",
BindingFlags.InvokeMethod,
null,
sapGui,
null
);
try
{
var connection = (GuiConnection)sap.Connections.ElementAt(0);
return (GuiSession)connection.Sessions.ElementAt(0);
}
catch (Exception)
{
throw new Exception("Not connected to any SAP system.");
}
}
var session = getSession();
""";
File.WriteAllText(
Path.Combine(Directory.GetCurrentDirectory(), filename),
preamble + string.Join(";" + Environment.NewLine, eventLog.Select(e => e.serialize()))
);
}
void saveWindowTree()
{
File.WriteAllText(
Path.Combine(Directory.GetCurrentDirectory(), "sap.json"),
getObjectTree(session.ActiveWindow.Id)
);
}