Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (59 loc) · 2.83 KB
/
Copy pathProgram.cs
File metadata and controls
66 lines (59 loc) · 2.83 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
namespace FlatBufferEx
{
public static class Program
{
public static async Task<int> Main(string[] args)
{
var options = Options.Parse(args);
if (options == null)
return 1;
try
{
await Generate(options);
Console.WriteLine("FlatBuffer code generation completed successfully.");
return 0;
}
catch (Exception e)
{
Console.Error.WriteLine($"Error: {e.Message}");
Console.Error.WriteLine($"Stack trace: {e.StackTrace}");
return 1;
}
}
private static async Task Generate(Options options)
{
var outputPath = Path.GetFullPath(options.OutputPath);
if (Directory.Exists(outputPath))
Directory.Delete(outputPath, true);
Directory.CreateDirectory(outputPath);
await Flatc.Install();
Console.WriteLine($"Parsing schema files from: {options.InputPath}");
var context = Parser.Parse(options.InputPath);
Console.WriteLine($"Found {context.Scopes.Count} scopes with {context.Scopes.Sum(s => s.Tables.Count)} tables and {context.Scopes.Sum(s => s.Enums.Count)} enums");
var workPath = Directory.CreateTempSubdirectory("flatbuffer-ex-").FullName;
try
{
foreach (var language in options.Languages)
{
Console.WriteLine($"Generating code for language: {language.Name}");
// The raw schema is the flatc input. Its namespaces depend on the target language's keywords, so it is
// rendered again for every language.
var schema = Path.Combine(workPath, language.FlatcOption, "protocol_raw.fbs");
Directory.CreateDirectory(Path.GetDirectoryName(schema));
await File.WriteAllTextAsync(schema, new SchemaGenerator(context, language).Render());
await Flatc.Compile(language, schema, Path.Combine(outputPath, "raw", language.Name), context.Scopes.Select(x => x.Namespace));
Generator generator = language == Language.Cpp ? new CppGenerator(context, options.IncludePath) : new CSharpGenerator(context);
var languageOutputPath = Path.Combine(outputPath, language.Name);
Directory.CreateDirectory(languageOutputPath);
var file = Path.Combine(languageOutputPath, "protocol" + language.Extension);
await File.WriteAllTextAsync(file, generator.Render());
Console.WriteLine($" Generated: {file}");
}
}
finally
{
Directory.Delete(workPath, true);
}
}
}
}