/
AddCalculatorCommand.cs
170 lines (137 loc) · 5.59 KB
/
AddCalculatorCommand.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using McMaster.Extensions.CommandLineUtils;
using YaccLexTools.Utilities;
namespace DotnetYaccLexTools.Commands
{
[Command("add-calculator", Description = "Add a calculator example.")]
public class AddCalculatorCommand : CommandBase
{
[Option("-n|--namespace", Description = "Namespace in which the parse will be included.")]
public string? Namespace { get; set; }
public async Task<int> OnExecute()
{
Console.WriteLine($"add-calculator {Versions.VersionString}\n");
const string parserName = "Calculator";
string? projectFile = GetProjectFile();
if (projectFile == null)
{
return 1;
}
if (Namespace != null)
{
Namespace = Namespace.Replace('/', '.').Replace('\\', '.');
if (!ValidateNamespace(Namespace))
{
return 1;
}
}
XDocument xProj = XDocument.Load(projectFile);
if (xProj.Root == null)
{
Console.WriteLine("Invalid project file.");
return 1;
}
XElement root = xProj.Root!;
string parserKey = GetParserKey(parserName, Namespace);
string rootNamespace = GetRootNamespace(projectFile, root);
if (Namespace == null)
{
Namespace = rootNamespace;
}
else if (Namespace != rootNamespace
&& !Namespace.StartsWith(rootNamespace + "."))
{
Namespace = rootNamespace + "." + Namespace;
}
// Search for existing parser with the same key.
var itemGroup = root
.Elements(XName.Get("ItemGroup"))
.Where(e => e.Attribute(XName.Get("Label"))?.Value == parserKey)
.FirstOrDefault();
if (itemGroup != null)
{
// Parser already exists.
Console.WriteLine($"Parser {parserName} already exists.");
return 1;
}
Console.WriteLine("Adding Calculator example...");
string relativePath = Namespace;
if (Namespace.StartsWith(rootNamespace))
{
relativePath = relativePath.Substring(rootNamespace.Length);
if (relativePath.StartsWith('.')) relativePath = relativePath.Substring(1);
}
if (relativePath.Length > 0)
{
relativePath = relativePath.Replace('.', '\\') + '\\';
}
relativePath += parserName + '\\';
Dictionary<string, string> tokens = new Dictionary<string, string>
{
{ "parserLabel", parserKey },
{ "parserName", parserName },
{ "namespace", Namespace },
{ "relativePath", relativePath }
};
// Add item group to the project file.
bool enabledDefaultCompileItems = IsEnabledDefaultCompileItems(root);
string itemGroupXml = enabledDefaultCompileItems ?
new Template().GetContent("project-ItemGroup1-fragment.xml", tokens) :
new Template().GetContent("project-ItemGroup2-fragment.xml", tokens);
itemGroup = root.Elements(XName.Get("ItemGroup")).LastOrDefault();
if (itemGroup != null)
{
itemGroup.AddAfterSelf(
XElement.Parse(itemGroupXml)
);
}
else
{
var propertyGroup = root.Elements(XName.Get("PropertyGroup")).LastOrDefault();
propertyGroup?.AddAfterSelf(
XElement.Parse(itemGroupXml)
);
}
// Check if the PackageReference exists.
bool hasReference = root
.Elements(XName.Get("ItemGroup"))
.Elements(XName.Get("PackageReference"))
.Any(x => x.Attribute(XName.Get("Include"))?.Value == "YaccLexTools");
if (!hasReference)
{
string packageReferenceXml = new Template().GetContent("project-PackageReference-fragment.xml", tokens);
var packageReference = root
.Elements(XName.Get("ItemGroup"))
.Elements(XName.Get("PackageReference"))
.LastOrDefault();
if (packageReference != null)
{
packageReference.AddAfterSelf(
XElement.Parse(packageReferenceXml)
);
}
else
{
root.Add(
new XElement("ItemGroup",
XElement.Parse(packageReferenceXml)
)
);
}
}
// Save the project file.
await File.WriteAllTextAsync(projectFile, xProj.ToString());
string prefix = relativePath + parserName;
AddFile(prefix + ".Language.analyzer.lex", "Calculator.Language.analyzer.lex", tokens);
AddFile(prefix + ".Language.grammar.y", "Calculator.Language.grammar.y", tokens);
AddFile(prefix + ".Parser.cs", "Calculator.Parser.cs", tokens);
AddFile(prefix + ".Scanner.cs", "Calculator.Scanner.cs", tokens);
return 0;
}
}
}