forked from MatteoBosco89/UnityCodeSmellAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
176 lines (163 loc) · 6.82 KB
/
Program.cs
File metadata and controls
176 lines (163 loc) · 6.82 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Xml.Linq;
using CommandLine;
using StarterModel;
namespace Starter
{
/// <summary>
/// Main
/// </summary>
internal class Program
{
public static void Main(string[] args)
{
UnityCodeSmellAnalyzer ucsa = new UnityCodeSmellAnalyzer();
Parser.Default.ParseArguments<Options>(args).WithParsed(o => ucsa.Init(o));
}
}
/// <summary>
/// Starter. Handles one thread for code analysis and one thread for data analysis, one repository at time.
/// </summary>
public class UnityCodeSmellAnalyzer
{
protected List<string> repos = new List<string>();
protected ThreadHandler codeAnalysis;
protected ThreadHandler dataAnalysis;
protected enum OS { Unix, Windows };
protected OS os = OS.Windows;
public UnityCodeSmellAnalyzer()
{
Logger.SetLogLevel(1);
Logger.Start();
}
/// <summary>
/// Init the main program with Opt Provided
/// </summary>
/// <param name="o">Opt provided</param>
public void Init(Options o)
{
Logger.Verbose = o.Verbose;
string[] directories = Directory.GetDirectories(o.Directory);
foreach (string d in directories) repos.Add(d);
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
os = OS.Unix;
WriteOutput("Running on " + RuntimeInformation.OSDescription + " " + RuntimeInformation.OSArchitecture);
WriteOutput(os.ToString());
WriteOutput("Starting ......");
WriteOutput("Found " + repos.Count + " Directories");
foreach (string d in repos)
{
StartThread(d);
while(!codeAnalysis.Finished || !dataAnalysis.Finished) { }
WriteOutput("---------------------------------------------");
}
WriteOutput("ShellStarter Exited");
}
/// <summary>
/// Starts analysis threads. Two threadhandlers are started: one for Code Analysis, one for Data Analysis.
/// </summary>
/// <param name="repo">The repository to analyze</param>
public void StartThread(string repo)
{
codeAnalysis = new ThreadHandler(CodeAnalysisProcesses(), CodeAnalysisCommands(repo), "Results" + Path.DirectorySeparatorChar + ProjectName(repo) + Path.DirectorySeparatorChar + "Code" + Path.DirectorySeparatorChar);
dataAnalysis = new ThreadHandler(DataAnalysisProcesses(), DataAnalysisCommands(repo), "Results" + Path.DirectorySeparatorChar + ProjectName(repo) + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar);
}
/// <summary>
/// Fetch the project name from the repository path.
/// </summary>
/// <param name="path">The repository path</param>
/// <returns></returns>
protected string ProjectName(string path)
{
string[] rep = path.Split(Path.DirectorySeparatorChar);
return rep.ElementAt(rep.Length - 1);
}
/// <summary>
/// Construct process list to run based on OS. CodeSmell case
/// </summary>
/// <returns>List of process</returns>
protected List<string> CodeAnalysisProcesses()
{
if(os == OS.Unix) return new List<string> { "mono", "mono" };
return new List<string> { "CSharpAnalyzer.exe", "CodeSmellAnalyzer.exe" };
}
/// <summary>
/// Construct process list to run based on OS. DataSmell Case
/// </summary>
/// <returns>List of process</returns>
protected List<string> DataAnalysisProcesses()
{
if (os == OS.Unix) return new List<string> { "mono", "mono" };
return new List<string> { "UnityDataAnalyzer.exe", "MetaSmellAnalyzer.exe" };
}
/// <summary>
/// Construct code analysis commands.
/// </summary>
/// <param name="repo">Repository to analyze</param>
/// <returns></returns>
protected List<string> CodeAnalysisCommands(string repo)
{
List<string> commands = new List<string>();
string path = Path.GetFullPath(repo);
string name = ProjectName(repo);
WriteOutput(DateTime.Now + " Analyzing Code " + name + " Repository");
if(os == OS.Windows)
{
commands.Add("-n " + name + " -p " + path + " -r Results/" + name + "/Code -v");
commands.Add("-d Results/" + name + "/Code/CodeAnalysis.json -r Results/" + name + "/Code -c -v");
}
else
{
commands.Add("CSharpAnalyzer.exe -n " + name + " -p " + path + " -r Results/" + name + "/Code -v");
commands.Add("CodeSmellAnalyzer.exe -d Results/" + name + "/Code/CodeAnalysis.json -r Results/" + name + "/Code -c -v");
}
return commands;
}
/// <summary>
/// Construct data analysis commands.
/// </summary>
/// <param name="repo">Repository to analyze</param>
/// <returns></returns>
protected List<string> DataAnalysisCommands(string repo)
{
List<string> commands = new List<string>();
string path = Path.GetFullPath(repo);
string name = ProjectName(repo);
WriteOutput(DateTime.Now + " Analyzing Data " + name + " Repository");
if(os == OS.Windows)
{
commands.Add("-n " + name + " -d " + path + " -r Results/" + name + "/Data -v");
commands.Add("-d Results/" + name + "/Data -r Results/" + name + "/Data -c -v");
}
else
{
commands.Add("UnityDataAnalyzer.exe -n " + name + " -d " + path + " -r Results/" + name + "/Data -v");
commands.Add("MetaSmellAnalyzer.exe -d Results/" + name + "/Data -r Results/" + name + "/Data -c -v");
}
return commands;
}
/// <summary>
/// Invoke Log Operations.
/// </summary>
/// <param name="s">String to log</param>
public static void WriteOutput(string s)
{
Logger.Log(Logger.LogLevel.Debug, s);
}
}
/// <summary>
/// Command Line Options Wrapper
/// </summary>
public class Options
{
[Option('d', "directory", Required = true, HelpText = "Directory containing the repository to analyze")]
public string Directory { get; set; }
[Option('v', "verbose", Required = false, HelpText = "Displays the log on the standard output.")]
public bool Verbose { get; set; }
}
}