-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.cs
343 lines (291 loc) · 11.5 KB
/
Functions.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
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
using System;
using Artech.Architecture.Common.Services;
using Artech.Genexus.Common;
using Artech.Genexus.Common.Parts;
using System.IO;
using System.Linq;
using Artech.Architecture.UI.Framework.Services;
using System.Text.RegularExpressions;
using Artech.Genexus.Common.Objects;
using Artech.Architecture.Common.Objects;
namespace GUG.Packages.KBCodeReview
{
class Functions
{
public static string StripHTML(string HTMLText)
{
Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
return reg.Replace(HTMLText, " ");
}
public static string LinkFile(string file)
{
return "<a href=\"file:///" + file + "\"" + ">" + file + "</a" + ">";
}
public static string ReturnPicture(Artech.Genexus.Common.Objects.Attribute a)
{
string Picture = "";
Picture = a.Type.ToString() + "(" + a.Length.ToString() + (a.Decimals > 0 ? "." + a.Decimals.ToString() : "") + ")" + (a.Signed ? "-" : "");
return Picture;
}
public static string ReturnVariableDefinitionAlert(Variable v)
{
string chrAlert = "";
if (v.IsAutoDefined)
{
chrAlert = "!";
}
else
{
if (v.AttributeBasedOn == null & v.DomainBasedOn == null & v.Type.ToString() != "GX_USRDEFTYP")
{
chrAlert = "*";
}
}
return chrAlert;
}
public static bool CheckObjUsesVariable(Variable v, KBObject obj)
{
bool varused = true;
if (!v.IsStandard)
{
varused = false;
ProcedurePart pp = obj.Parts.Get<ProcedurePart>();
if (pp != null)
{
varused = VariableIsUsedInText(pp.Source, v.Name);
}
if (!varused)
{
RulesPart rp = obj.Parts.Get<RulesPart>();
if (rp != null)
{
varused = VariableIsUsedInText(rp.Source, v.Name);
}
}
if (!varused)
{
ConditionsPart cp = obj.Parts.Get<ConditionsPart>();
if (cp != null)
{
varused = VariableIsUsedInText(cp.Source, v.Name);
}
}
if (!varused)
{
EventsPart ep = obj.Parts.Get<EventsPart>();
if (ep != null)
{
varused = VariableIsUsedInText(ep.Source, v.Name);
}
}
if (!varused)
{
WebFormPart fp = obj.Parts.Get<WebFormPart>();
if (fp != null)
{
varused = VariableIsUsedInWebForm(fp, v.Id); ;
}
}
}
return varused;
}
public static void ObjStats_Comments(KBObject obj, out int linesSource, out int linesComment, out float PercentComment)
{
linesSource = 0;
linesComment = 0;
PercentComment = 0;
if (obj is Transaction || obj is WebPanel || obj is Procedure || obj is WorkPanel)
{
if (ObjectsHelper.isGenerated(obj) && !ObjectsHelper.isGeneratedbyPattern(obj))
{
string sourceWOComments = ObjectsHelper.GetSourceCodeWithoutComments(obj);
string source = ObjectsHelper.GetSourceCode(obj);
CountCommentsLines(source, sourceWOComments, out linesSource, out linesComment, out PercentComment);
}
}
}
public static int LineCount(string s)
{
int n = 0;
foreach (var c in s)
{
if (c == '\n') n++;
}
return n;
}
public static string RemoveEmptyLines(string lines)
{
return Regex.Replace(lines, @"^\s*$\n|\r", "", RegexOptions.Multiline);
}
public static string ExtractComments(string source)
{
var blockComments = @"/\*(.*?)\*/";
var lineComments = @"//(.*?)\r?\n";
var strings = @"""((\\[^\n]|[^""\n])*)""";
var verbatimStrings = @"@(""[^""]*"")+";
string noComments = Regex.Replace(source, blockComments + "|" + lineComments + "|" + strings + "|" + verbatimStrings,
me =>
{
if (me.Value.StartsWith("/*") || me.Value.StartsWith("//"))
return me.Value.StartsWith("//") ? Environment.NewLine : "";
// Keep the literal strings
return me.Value;
},
RegexOptions.Singleline);
noComments = noComments.Replace("(", " (");
noComments = noComments.Replace(")", ") ");
noComments = noComments.Replace("\"", "\'");
noComments = noComments.Replace("\t", " ");
//saco blancos
string aux = noComments.Replace(" ", " ");
do
{
noComments = aux;
aux = noComments.Replace(" ", " ");
} while (noComments != aux);
//noComments = noComments.ToUpper();
return noComments;
}
public static string ReturnPictureVariable(Variable v)
{
string Length = "";
if (v.Type.ToString() == "GX_SDT" || v.Type.ToString() == "Boolean" || v.Type.ToString() == "GX_USRDEFTYP")
Length = "";
else
Length = "(" + v.Length.ToString() + (v.Decimals > 0 ? "." + v.Decimals.ToString() : "") + ")" + (v.Signed ? "-" : "");
string Picture = "";
if (v.Type.ToString() == "GX_USRDEFTYP")
{
Picture = v.GetPropertyValue<string>(Properties.ATT.DataTypeString);
}
else
{
Picture = v.Type.ToString() + Length;
}
return Picture;
}
public static void SaveObject(IOutputService output, KBObject obj)
{
try
{
obj.Save();
}
catch (Exception e)
{
output.AddErrorLine(e.Message + " - " + e.InnerException);
}
}
public static string ReturnPictureDomain(Domain d)
{
string Picture = "";
Picture = d.Type.ToString() + "(" + d.Length.ToString() + (d.Decimals > 0 ? "." + d.Decimals.ToString() : "") + ")" + (d.Signed ? "-" : "");
return Picture;
}
public static string ReplaceInvalidCharacterInFileName(string name)
{
var invalidChars = Path.GetInvalidFileNameChars();
string invalidCharsRemoved = new string(name
.Where(x => !invalidChars.Contains(x))
.ToArray());
name = name.Replace("'", "");
name = name.Replace(":", "_");
name = name.Replace(" ", "");
name = name.Replace(@"\", "_");
name = name.Replace("/", "_");
return name;
}
public static string SpcDirectory(IKBService kbserv)
{
GxModel gxModel = kbserv.CurrentKB.DesignModel.Environment.TargetModel.GetAs<GxModel>();
return kbserv.CurrentKB.Location + string.Format(@"\GXSPC{0:D3}\", gxModel.Model.Id);
}
public static string CreateOutputFile(IKBService kbserv, string title)
{
string outputFile = kbserv.CurrentKB.UserDirectory + @"\kbdoctor." + Functions.CleanFileName(title) + ".html";
if (File.Exists(outputFile))
{
File.Delete(outputFile);
}
return outputFile;
}
public static string CleanFileName(string filename)
{
return string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));
}
public static string ExtractRuleParm(KBObject obj)
{
RulesPart rulesPart = obj.Parts.Get<RulesPart>();
string aux = "";
if (rulesPart != null)
{
Regex myReg = new Regex("//.*", RegexOptions.None);
Regex paramReg = new Regex(@"parm\(.*\)", RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);
string reglas = rulesPart.Source;
reglas = myReg.Replace(reglas, "");
Match match = paramReg.Match(reglas);
if (match != null)
aux = match.ToString();
else
aux = "";
}
return aux;
}
public static string LinkObject(KBObject obj)
{
return "<a href=\"gx://?Command=fa2c542d-cd46-4df2-9317-bd5899a536eb;OpenObject&name=" + obj.Guid.ToString() + "\">" + obj.Name + "</a>";
}
private static bool VariableIsUsedInText(string sourceCode, string varName)
{
bool usedvar = false;
if (sourceCode != null)
{
Regex myReg = new Regex("//.*", RegexOptions.None);
Regex myReg2 = new Regex(@"/\*.*\*/", RegexOptions.Singleline);
Regex paramReg = new Regex(varName + @"\W+", RegexOptions.Singleline | RegexOptions.IgnoreCase);
sourceCode = myReg.Replace(sourceCode, "");
sourceCode = myReg2.Replace(sourceCode, "");
Match match = paramReg.Match(sourceCode);
if (match.Success)
{
usedvar = true;
}
return usedvar;
}
else
{
return false;
}
}
private static bool VariableIsUsedInWebForm(WebFormPart wF, int varId)
{
return (wF.GetVariable(varId) != null);
}
private static void CountCommentsLines(string source, string sourceWOComments, out int linesSource, out int linesComment, out float PercentComment)
{
linesSource = Functions.LineCount(source);
int linesWOComment = Functions.LineCount(sourceWOComments);
linesComment = linesSource - linesWOComment;
PercentComment = (linesSource == 0) ? 0 : (linesComment * 100) / linesSource;
}
internal static void AddLineSummary(string fileName, string texto)
{
IKBService kbserv = UIServices.KB;
string outputFile = kbserv.CurrentKB.UserDirectory + @"\" + fileName;
using (FileStream fs = new FileStream(outputFile, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(DateTime.Now.ToString() + "," + texto);
}
}
internal static KBCategory MainCategory(KBModel model)
{
return KBCategory.Get(model, "Main Programs");
}
public static void WriteXSLTtoDir()
{
IKBService kbserv = UIServices.KB;
string outputFile = kbserv.CurrentKB.UserDirectory + @"\NavigationHtmlToText.xslt";
File.WriteAllText(outputFile, Resources.NavigationHTMLToText);
}
}
}