forked from MatteoBosco89/UnityCodeSmellAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaDetector.cs
More file actions
219 lines (208 loc) · 8.5 KB
/
MetaDetector.cs
File metadata and controls
219 lines (208 loc) · 8.5 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
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace MetaSmellDetector
{
public static class MetaDetector
{
private static JArray mainData = new JArray();
private static JArray metaData = new JArray();
private static bool isDataLoaded = false;
private static string smellFile="smell.txt";
private static JArray smells = new JArray();
private static string[] DIR = { ".json" };
private static int logLevel = 1;
private static bool expose= false;
private static IEnumerable<string> dataPath;
private static string saveDir = "";
private static bool saveOnMultipleFile = false;
private static bool saveCsv = false;
public static void Init(Options opt)
{
if (opt.DataPath.Count() > 0) dataPath = opt.DataPath;
if (opt.SmellPath != null) smellFile = opt.SmellPath;
if (opt.Verbose) Logger.Verbose = true;
if (opt.Expose) expose = true;
if (opt.SaveDir != null) saveDir = opt.SaveDir;
if (opt.SaveOnMultipleFile) saveOnMultipleFile = true;
if (opt.SaveResultCsv) saveCsv = true;
Logger.SetLogLevel(logLevel);
if(saveDir == "") saveDir = Directory.GetCurrentDirectory();
Logger.LogFile = Path.Combine(saveDir, "metaLinter.log");
Logger.Start();
}
public static void LoadData()
{
Logger.Log(Logger.LogLevel.Debug, "Loading Dataset...");
try
{
Logger.Log(Logger.LogLevel.Debug, "Loading main data...");
List<string> files = Directory.GetFiles(dataPath.ElementAt(0), "*.*", SearchOption.AllDirectories).Where(f => DIR.Any(f.ToLower().EndsWith)).ToList();
foreach (string file in files)
{
string text = File.ReadAllText(file);
try
{
JObject j = JObject.Parse(text);
mainData.Add(j);
Logger.Log(Logger.LogLevel.Debug, $"File: {file}");
}
catch (JsonReaderException)
{
Logger.Log(Logger.LogLevel.Debug, $"File: {file} is not json");
}
}
Logger.Log(Logger.LogLevel.Debug, "Maindata loaded!");
if (dataPath.Count() > 1)
{
Logger.Log(Logger.LogLevel.Debug, "Loading metadata...");
files = Directory.GetFiles(dataPath.ElementAt(1), "*.*", SearchOption.AllDirectories).Where(f => DIR.Any(f.ToLower().EndsWith)).ToList();
foreach (string file in files)
{
string text = File.ReadAllText(file);
try
{
metaData.Add(JObject.Parse(text));
}
catch (JsonReaderException)
{
Logger.Log(Logger.LogLevel.Debug, $"File: {file} is not json");
}
}
Logger.Log(Logger.LogLevel.Debug, "Metadata loaded!");
}
isDataLoaded = true;
Logger.Log(Logger.LogLevel.Debug, "Dataset Loaded!");
}
catch (FileNotFoundException)
{
Logger.Log(Logger.LogLevel.Debug, "Dataset Not Found");
isDataLoaded = false;
}
}
public static void Analyze()
{
Logger.Log(Logger.LogLevel.Debug, "Start analysis...");
if (expose)
{
ExposeSmellMethod();
return;
}
if(dataPath != null)LoadData();
if (!isDataLoaded)
{
return;
}
JArray data = mainData;
if (metaData != null)
{
data.Union(metaData);
}
SearchSmell(data);
Logger.Log(Logger.LogLevel.Debug, "Analysis Done!");
if (saveOnMultipleFile) SaveMultipleFile();
if (saveCsv) SaveOnCsv();
if(!saveOnMultipleFile && !saveCsv) SaveResult();
}
public static void ExposeSmellMethod()
{
Logger.Log(Logger.LogLevel.Debug, "Exposing names of smell methods...");
List<string> methods = MetaExtractor.SmellsMethods();
string text = "";
foreach(string s in methods)
{
text += s + "\n";
}
File.WriteAllText("smellsmethods.txt", text);
Logger.Log(Logger.LogLevel.Debug, "Saved results in smellsmethods.txt");
}
public static void SearchSmell(JArray data)
{
Logger.Log(Logger.LogLevel.Debug, "Searching for smells...");
try
{
string[] lines = File.ReadAllLines(smellFile);
foreach(string c in lines)
{
JObject result = MetaExtractor.InvokeMethods(c, data);
if(result!= null)
{
smells.Add(result);
}
}
}
catch (FileNotFoundException)
{
Logger.Log(Logger.LogLevel.Debug, "Smell File Not Found");
isDataLoaded = false;
}
Logger.Log(Logger.LogLevel.Debug, "Search completed!");
}
public static void SaveResult()
{
if (saveDir == "") saveDir = Directory.GetCurrentDirectory();
Logger.Log(Logger.LogLevel.Debug, "Saving results...");
JObject result = new JObject();
result.Add("ProjectName", mainData.First()["ProjectName"]);
result.Add("ProjectPath", mainData.First()["ProjectPath"]);
result.Add("SmellList", smells);
File.WriteAllText(Path.Combine(saveDir, "results.json"), result.ToString());
Logger.Log(Logger.LogLevel.Debug, "Results saved in results.json");
}
public static void SaveMultipleFile()
{
Logger.Log(Logger.LogLevel.Debug, "Saving results...");
if (saveDir == "") saveDir = Directory.GetCurrentDirectory();
saveDir = Path.Combine(saveDir, "MetaSmellResults");
if(Directory.Exists(saveDir))Directory.Delete(saveDir, true);
Directory.CreateDirectory(saveDir);
foreach(JToken smell in smells)
{
string fileName = smell["Name"].ToString().Replace(" ", "_")+".json";
Logger.Log(Logger.LogLevel.Debug, "Saving: " + fileName);
fileName = Path.Combine(saveDir, fileName);
File.WriteAllText(fileName, smell.ToString());
}
Logger.Log(Logger.LogLevel.Debug, "Results saved in " + saveDir);
}
public static void SaveOnCsv()
{
if (mainData.Count <= 0) return;
Logger.Log(Logger.LogLevel.Debug, "Saving Num Smell For Project...");
string name = Directory.GetCurrentDirectory();
if (saveDir == "") name = Path.Combine(name, "metaSmellOccurrencyProject.csv");
else name = Path.Combine(saveDir, "metaSmellOccurrencyProject.csv");
using (var file = File.CreateText(name))
{
Dictionary<string, string> csv = new Dictionary<string, string>();
csv.Add("ProjectName", mainData.First()["ProjectName"].ToString());
csv.Add("ProjectPath", mainData.First()["ProjectPath"].ToString());
foreach (JToken r in smells)
{
csv.Add(r["Name"].ToString(), r["Occurrency"].ToString());
}
string h = "";
string v = "";
int i = 0;
foreach (string k in csv.Keys)
{
h += k;
v += csv[k];
if (i < csv.Count())
{
h += ";";
v += ";";
}
i++;
}
file.WriteLine(h);
file.WriteLine(v);
file.Close();
}
Logger.Log(Logger.LogLevel.Debug, "Done!");
}
}
}