예제 #1
0
        private static void Run(Options options)
        {
            try
            {
                var bundle = SchemaBundleLoader.LoadBundle(options.InputBundle);

                ValidateBundle(bundle);

                var generators = new List <ICodeGenerator>
                {
                    new UnrealGenerator()
                };

                var output = new List <GeneratedFile>();

                generators.ForEach((ICodeGenerator g) => output.AddRange(g.GenerateFiles(bundle)));

                foreach (var generatedFile in output)
                {
                    WriteFile(options.OutputDir, generatedFile);
                }
            }
            catch (Exception exception)
            {
                Console.Error.WriteLine(exception);
                Environment.ExitCode = 1;
            }
        }
예제 #2
0
        private static void Run(Options options)
        {
            Log.Information(Parser.Default.FormatCommandLine(options));

            var timer = new Stopwatch();

            timer.Start();

            try
            {
                try
                {
                    File.Delete(options.OutputMarker);
                }
                catch
                {
                    // Nothing interesting to do here.
                }

                var bundle = SchemaBundleLoader.LoadBundle(options.InputBundle);

                // Sort the types by the depth of their declaration, so that nested types are generated first so they can be used by their declaring type.
                var types = bundle.Types.Select(kv => new TypeDescription(kv.Key, bundle))
                            .Union(bundle.Components.Select(kv => new TypeDescription(kv.Key, bundle)))
                            .OrderByDescending(t => t.QualifiedName.Count(c => c == '.'))
                            .ToList();

                foreach (var w in types.SelectMany(t => t.Warnings))
                {
                    Log.Warning(w);
                }

                var baseGenerator = new Generator(bundle);
                baseGenerator.FieldDecorators.Add(DatabaseSyncGenerator.JsonPropertyDecorator);
                var generators = new List <ICodeGenerator>
                {
                    baseGenerator,
                    new StdlibGenerator(bundle),
                    new ComponentGenerator(),
                    new SchemaObjectGenerator(bundle),
                    new DatabaseSyncGenerator()
                };

                var allContent = new Dictionary <string, StringBuilder>();

                // Pass 1: generate all type content.
                foreach (var t in types)
                {
                    if (!allContent.TryGetValue(t.QualifiedName, out var builder))
                    {
                        builder = allContent[t.QualifiedName] = new StringBuilder();
                    }

                    // Embed nested enums.
                    builder.AppendJoin(Environment.NewLine, t.NestedEnums.Select(e => GenerateEnum(e, bundle).TrimEnd()));

                    // Class implementation.
                    builder.AppendJoin(Environment.NewLine, generators.Select(g =>
                    {
                        var result = g.Generate(t).TrimEnd();
                        if (string.IsNullOrWhiteSpace(result))
                        {
                            return(string.Empty);
                        }

                        return($@"
#region {g.GetType().FullName}

{result}

#endregion {g.GetType().FullName}
");
                    }).Where(s => !string.IsNullOrEmpty(s)));

                    // Nested types.
                    builder.AppendJoin(Environment.NewLine, t.NestedTypes.Select(e => GenerateType(e, allContent[e.QualifiedName].ToString(), bundle)));
                }

                // Pass 2: Generate final content.
                foreach (var t in types.Where(type => string.IsNullOrEmpty(type.OuterType)))
                {
                    var content = allContent[t.QualifiedName];

                    WriteFile(options, Types.TypeToFilename(t.QualifiedName), $@"
namespace {t.Namespace()}
{{
{Indent(1, GenerateType(t, content.ToString().TrimEnd(), bundle))}
}}");
                }

                // Enums.
                foreach (var(key, value) in bundle.Enums.Where(type => string.IsNullOrEmpty(type.Value.OuterType)))
                {
                    WriteFile(options, Types.TypeToFilename(key), $@"
namespace {value.Namespace()}
{{
{Indent(1, GenerateEnum(value, bundle))}
}}");
                }

                File.WriteAllText(options.OutputMarker, string.Empty);
            }
            catch (Exception exception)
            {
                Log.Error(exception, "While running");
                Environment.ExitCode = 1;
            }
            finally
            {
                timer.Stop();

                Log.Information($"Processed schema bundle in {timer.Elapsed}.");
            }
        }