forked from florentbr/SeleniumBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.cs
More file actions
142 lines (119 loc) · 6.08 KB
/
Script.cs
File metadata and controls
142 lines (119 loc) · 6.08 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
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace vbsc {
class Script {
public readonly string Path;
public readonly string[] Arguments;
public readonly string Param;
public readonly string Directory;
public readonly string TextOriginal;
public readonly int LineCount;
public bool Succeed = true;
public string TextFormated;
public List<Script> ChildenScripts;
public Script ParentScript;
public int ParentLineNumber;
public Dictionary<string, WithParams> ScriptWithParams;
public Script(string script_path, string[] arguments, string param = null)
: this(script_path) {
this.Arguments = arguments;
this.Param = param;
FormatScript(null, 0);
}
private Script(string script_path, Script parent_script, int parent_line_number)
: this(script_path) {
FormatScript(parent_script, parent_line_number);
}
private Script(string script_path) {
this.Path = script_path;
this.Directory = System.IO.Path.GetDirectoryName(this.Path);
this.TextOriginal = (System.IO.File.ReadAllText(this.Path)).ToString();
this.LineCount = TextOriginal.CountLines();
}
public override string ToString() {
StringBuilder buffer = new StringBuilder();
buffer.Append(System.IO.Path.GetFileName(this.Path));
if (this.Param != null) {
buffer.Append('(');
buffer.Append(this.Param);
buffer.Append(')');
}
return buffer.ToString();
}
/// <summary>Recursive methode to read and format a script and the refered scripts defined in the #Include statement.</summary>
/// <param name="par_script">Parent script used in recurtion call</param>
/// <param name="par_line_number">Parent line number used in recurtion call</param>
/// <param name="par_lien_of_code">Parent line of code used in recurtion call</param>
/// <returns>A list of scripts</returns>
private void FormatScript(Script par_script, int par_line_number) {
this.TextFormated = this.TextOriginal;
this.ParentScript = par_script;
this.ParentLineNumber = par_line_number;
//Handle includes
Match match_inc = Regex.Match(this.TextFormated, @"^#Include ""([^""]+)""", RegexOptions.Multiline);
while (match_inc.Success) {
this.ChildenScripts = new List<Script>();
var inc_path = match_inc.Groups[1].Value;
var inc_line_number = this.TextFormated.CountLines(0, match_inc.Index);
foreach (var child_script_path in Utils.ExpandFilePaths(new string[] { inc_path }, @"\.js$|\.vbs$", this.Directory)) {
this.ChildenScripts.Add(new Script(child_script_path, this, inc_line_number));
}
match_inc = match_inc.NextMatch();
}
this.TextFormated = this.TextFormated.RemoveAll(@"^#Include[^\r\n]*");
//Handle Console prints
this.TextFormated = Regex.Replace(this.TextFormated, @"Debug\.Print", @"Wscript.Echo", RegexOptions.Multiline | RegexOptions.IgnoreCase);
if (par_script == null) {
//Replace param
if (this.Param != null)
this.TextFormated = Regex.Replace(this.TextFormated, @"\@\bparam\b", this.Param, RegexOptions.IgnoreCase);
//Replace the wrapper instantiation if it's called using "New"
//this.TextFormated = Regex.Replace(this.TextFormated, @"Set ([\w_-]+) = New (Selenium\.\w+)", @"Set $1 = CreateObject(""$2"")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
//this.TextFormated = Regex.Replace(this.TextFormated, @"Dim ([\w_-]+) As New (Selenium\.\w+)", @"Set $1 = CreateObject(""$2"")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
//Handles With
var matches_with = Regex.Matches(this.TextFormated, (@"^\[With\((.*)\)\]\s+(Private |Public )?Sub ([\w_]+)"), RegexOptions.IgnoreCase | RegexOptions.Multiline);
this.ScriptWithParams = new Dictionary<string, WithParams>(matches_with.Count);
foreach (Match match in matches_with) {
string procName = match.Groups[3].Value;
string procParams = match.Groups[1].Value.Trim('\r', '\n', ' ');
int procLine = this.TextFormated.CountLines(0, match.Index);
ScriptWithParams.Add(procName, new WithParams {
Params = procParams,
Line = procLine
});
}
this.TextFormated = this.TextFormated.ReplaceAll(@"^\[With\((.*)\)\]", "");
}
}
public string GetCode() {
var buffer = new StringBuilder();
this.WriteTextFormated(buffer);
return buffer.ToString();
}
public TraceLine GetTraceLineAt(int line_number) {
return GetTraceLineAt(new []{line_number});
}
private void WriteTextFormated(StringBuilder buffer) {
if (this.ChildenScripts != null) {
foreach (var child_script in this.ChildenScripts) {
child_script.WriteTextFormated(buffer);
}
}
buffer.AppendLine(this.TextFormated);
}
private TraceLine GetTraceLineAt(int[] lineNumber) {
if (this.ChildenScripts != null) {
foreach (var child_script in this.ChildenScripts) {
TraceLine traceline = child_script.GetTraceLineAt(lineNumber);
if (traceline != null)
return traceline;
}
}
if (lineNumber[0] <= this.LineCount)
return new TraceLine(this, lineNumber[0]);
lineNumber[0] -= this.LineCount + 1;
return null;
}
}
}