-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
41 lines (34 loc) · 1.24 KB
/
Program.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
using System.CommandLine;
using erd_dotnet;
var inputArgument = new Argument<string>("inputFile", "Text file (er format).");
var outputArgument = new Argument<string>("outputFile", "Output file (png or txt).");
var optionOutputFormat = new Option<string>(name: "--format", description: "Output format (png or txt)", getDefaultValue: () => "png");
optionOutputFormat.AddAlias("-f");
var rootCommand = new RootCommand("Dotnet Erd to png")
{
inputArgument,
outputArgument
};
rootCommand.AddGlobalOption(optionOutputFormat);
rootCommand.SetHandler((optionFormatValue, inputArgumentValue, outputArgumentValue) =>
{
GenerateFile(inputArgumentValue, outputArgumentValue, optionFormatValue);
}, optionOutputFormat, inputArgument, outputArgument);
await rootCommand.InvokeAsync(args);
void GenerateFile(string input, string output, string format)
{
var text = File.ReadAllLines(input);
var parser = new ErdParser();
var erd = parser.Parse(text);
var writer = new ErdDotWriter(erd);
if (format == "png")
{
var tempDot = Path.GetTempFileName();
writer.WriteFile(tempDot);
System.Diagnostics.Process.Start("dot", $"-Tpng {tempDot} -o {output}");
}
else
{
writer.WriteFile(output);
}
}