-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainParser.cs
More file actions
143 lines (128 loc) · 5.79 KB
/
MainParser.cs
File metadata and controls
143 lines (128 loc) · 5.79 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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Parser.Parsers;
using Parser.Parsers.MethodParsers;
namespace Parser.MainParser;
public class MainParser
{
private Dictionary<string, ClassDeclarationSyntax> _trees;
public MainParser()
{
_trees = new Dictionary<string, ClassDeclarationSyntax>();
}
public void Distribute(string dirToPath, string path)
{
foreach (var file in Directory.GetFiles(path))
{
var fileName = file.Substring(file.LastIndexOf('\\') + 1,
file.LastIndexOf('.') - file.LastIndexOf('\\') - 1);
if (fileName.Contains("Controller"))
fileName = fileName.Replace("Controller", "") + "Client";
using (var sr = new StreamReader(file, System.Text.Encoding.Default))
{
string line;
var classFlag = false;
var fieldFlag = true;
while ((line = sr.ReadLine()) != null)
{
if (line == string.Empty || line.Equals("}"))
continue;
if (line.Contains("class"))
{
var classDeclaration = ClassParser.ClassDeclaration(line);
_trees.Add(fileName, classDeclaration);
classFlag = true;
if (fileName.Contains("Client"))
{
var type = "HttpClient";
var name = "_client";
var mod = "private static readonly";
_trees[fileName] = _trees[fileName]
.AddMembers(
FieldParser.HttpClientField(type, name, mod));
}
continue;
}
if (line.Contains('('))
fieldFlag = false;
if (fieldFlag && !fileName.Contains("Client") && classFlag && line.Contains('=') | !line.Contains(')'))
{
var fieldDeclaration = FieldParser.GetField(line);
_trees[fileName] = _trees[fileName]
.AddMembers(fieldDeclaration);
continue;
}
if (classFlag && line.Contains('@') && line.Contains('"'))
{
var methodLine = sr.ReadLine();
var methodDeclaration = MethodDistributer.Distribute(methodLine, line);
_trees[fileName] = _trees[fileName]
.AddMembers(methodDeclaration);
}
}
}
if (fileName.Contains("Client"))
ParseControllers(dirToPath, fileName);
else
{
ParseDtos(dirToPath, fileName);
}
}
}
private void toFile(CompilationUnitSyntax comp, string path)
{
if (File.Exists(path))
File.Delete(path);
FileStream file = File.Create(path);
file.Close();
File.WriteAllText(path, comp.NormalizeWhitespace().ToString());
}
private void ParseControllers(string dirToPath, string fileName)
{
toFile(SyntaxFactory.CompilationUnit()
.AddUsings(
SyntaxFactory.UsingDirective(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(
SyntaxFactory.IdentifierName("System"),
SyntaxFactory.IdentifierName("Net")),
SyntaxFactory.IdentifierName("Http")),
SyntaxFactory.IdentifierName("Json"))))
.AddUsings(
SyntaxFactory.UsingDirective(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(
SyntaxFactory.IdentifierName("System"),
SyntaxFactory.IdentifierName("Text")),
SyntaxFactory.IdentifierName("Json"))))
.AddUsings(
SyntaxFactory.UsingDirective(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(
SyntaxFactory.IdentifierName("Parser"),
SyntaxFactory.IdentifierName("ParserResults")),
SyntaxFactory.IdentifierName("Entities"))))
.AddMembers(
SyntaxFactory.NamespaceDeclaration(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(
SyntaxFactory.IdentifierName("Parser"),
SyntaxFactory.IdentifierName("ParserResults")),
SyntaxFactory.IdentifierName("ClientGens")))
.AddMembers(_trees[fileName])), Path.Combine(dirToPath, fileName + ".cs"));
}
private void ParseDtos(string dirToPath, string fileName)
{
toFile(SyntaxFactory.CompilationUnit()
.AddMembers(
SyntaxFactory.NamespaceDeclaration(
SyntaxFactory.QualifiedName(
SyntaxFactory.QualifiedName(
SyntaxFactory.IdentifierName("Parser"),
SyntaxFactory.IdentifierName("ParserResults")),
SyntaxFactory.IdentifierName("Entities")))
.AddMembers(_trees[fileName])), Path.Combine(dirToPath, fileName + ".cs"));
}
}