-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (160 loc) · 6.49 KB
/
Program.cs
File metadata and controls
200 lines (160 loc) · 6.49 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using PatternDetector.Analyzers;
using PatternDetector.Interface;
namespace PatternDetector
{
class Program
{
public static int allProjectsCount;
public static int allDocumentsCount;
public static int allClassesCount;
public static int detectedProjectsCount;
public static int detectedDocumentsCount;
public static int detectedClassesCount;
public static int detectedPatternsCount;
public static Dictionary<string, int> eachPatternCount;
static async Task Main(string[] args)
{
SetConsole();
SetMSBuild();
using (var workspace = MSBuildWorkspace.Create())
{
// Âûâîä ñîîáùåíèÿ äëÿ ñîáûòèÿ WorkspaceFailed, ÷òîáû ïîìî÷ü äèàãíîñòèðîâàòü ñáîè çàãðóçêè ïðîåêòà.
// workspace.WorkspaceFailed += (o, e) => Console.WriteLine(e.Diagnostic.Message);
while (true)
{
Solution solution = await GetSolution(workspace);
if (solution == null)
continue;
SolutionAnalyzer analyzeResult = AnalyzeSolution(solution);
Demonstration demonstration = new Demonstration(analyzeResult);
if (NotWantToContinue())
break;
}
}
}
private static void SetConsole()
{
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.White;
Console.Clear();
//Console.SetWindowSize(150, 25);
}
private static void SetMSBuild()
{
// Ïîïûòêà óñòàíîâèòü âåðñèþ MSBuild.
var visualStudioInstances = MSBuildLocator.QueryVisualStudioInstances().ToArray();
var instance = visualStudioInstances.Length == 1
// Åñëè íà ýòîì êîìïüþòåðå åñòü òîëüêî îäèí ýêçåìïëÿð MSBuild, îí è âûáèðàåòñÿ.
? visualStudioInstances[0]
// Âûáîð âåðñèè MSBuild.
: SelectVisualStudioInstance(visualStudioInstances);
Console.WriteLine("Äëÿ çàãðóçêè ïðîåêòîâ èñïîëüçóåòñÿ MSBuild ïî àäðåñó: ");
Console.WriteLine($"{instance.MSBuildPath}");
Console.WriteLine();
/*
ÂÍÈÌÀÍÈÅ:
Ïåðåä âûçîâîì MSBuildWorkspace.Create()
îáÿçàòåëüíî çàðãèñòðèðóéòå ýêçåìïëÿð â MSBuildLocator,
â ïðîòèâíîì ñëó÷àå MSBuildWorkspace íå áóäåò ñîñòàâëÿòü MEF.
*/
MSBuildLocator.RegisterInstance(instance);
}
private static VisualStudioInstance SelectVisualStudioInstance(VisualStudioInstance[] visualStudioInstances)
{
Console.WriteLine("Îáíàðóæåíî íåñêîëüêî óñòàíîâîê MSBuild, âûáåðèòå îäíó:");
for (int i = 0; i < visualStudioInstances.Length; i++)
{
Console.WriteLine($"Ýêçåìïëÿð {i + 1}");
Console.WriteLine($" Íàçâàíèå: {visualStudioInstances[i].Name}");
Console.WriteLine($" Âåðñèÿ: {visualStudioInstances[i].Version}");
Console.WriteLine($" Ïóòü: {visualStudioInstances[i].MSBuildPath}");
}
while (true)
{
var userResponse = Console.ReadLine();
if (int.TryParse(userResponse, out int instanceNumber) &&
instanceNumber > 0 &&
instanceNumber <= visualStudioInstances.Length)
{
return visualStudioInstances[instanceNumber - 1];
}
Console.WriteLine();
Console.WriteLine("Ââîä íå ïðèíÿò, ïîïðîáóéòå åùå ðàç.");
Console.WriteLine();
}
}
private static async Task<Solution> GetSolution(MSBuildWorkspace msWorkspace)
{
Solution solutionResult;
string solutionPath = GetPath();
try
{
solutionResult = await msWorkspace.OpenSolutionAsync(solutionPath);
}
catch
{
Console.WriteLine("Íå óäàëîñü çàãðóçèòü ðåøåíèå, ïîïðîáóéòå åùå ðàç. ");
Console.WriteLine();
return null;
}
allProjectsCount = solutionResult.Projects.Count();
allDocumentsCount = solutionResult.Projects.Sum(project => project.Documents.Count());
allClassesCount = 0;
eachPatternCount = new Dictionary<string, int>
{
{ "Composite", 0 },
{ "Iterator", 0 },
{ "Observer", 0 },
{ "Prototype", 0 },
{ "Singleton", 0 },
{ "Template Method", 0},
};
detectedProjectsCount = 0;
detectedDocumentsCount = 0;
detectedClassesCount = 0;
detectedPatternsCount = 0;
SolutionLoaded(solutionResult);
return solutionResult;
}
private static string GetPath()
{
// Ïîëó÷àåì ïóòü ê ðåøåíèþ èç êîíñîëè
Console.WriteLine("Óêàæèòå ïóòü ê ðåøåíèþ: ");
string path = Console.ReadLine().Trim();
Console.WriteLine();
Console.WriteLine("Ïîäîæäèòå, èä¸ò çàãðóçêà ðåøåíèÿ... ");
Console.WriteLine();
return path;
}
private static void SolutionLoaded(Solution loadedSolution)
{
Console.WriteLine($@"Çàâåðøåíà çàãðóçêà ðåøåíèÿ ïî àäðåñó:
{loadedSolution.FilePath}
Âñåãî ïðîåêòîâ â ðåøåíèè: {allProjectsCount}
Âñåãî äîêóìåíòîâ â ðåøåíèè: {allDocumentsCount}
Ïîäîæäèòå, èä¸ò ïîèñê...
");
}
private static SolutionAnalyzer AnalyzeSolution(Solution analyzedSolution)
{
SolutionAnalyzer solutionAnalyzer = new SolutionAnalyzer(analyzedSolution);
Console.WriteLine("Ïîèñê çàâåðøåí!");
Console.WriteLine();
return solutionAnalyzer;
}
private static bool NotWantToContinue()
{
Console.WriteLine("Íàæìèòå Enter, ÷òîáû ïîâòîðèòü àíàëèç, èëè ëþáóþ äðóãóþ êëàâèøó, ÷òîáû âûéòè");
var consoleKey = Console.ReadKey().Key;
Console.WriteLine();
return consoleKey != ConsoleKey.Enter;
}
}
}