Ejemplo n.º 1
0
        private static string GenerateModelClass(CodeGenerationOptions options, Kind kind)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, kind);

            WriteUsings(options, kind);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string typeName = NameHelper.MakeModelClassName(kind);

                WriteLine($"public class {typeName}");
                using (OpenBlock())
                {
                    if (kind == Kind.Context || kind == Kind.Supervisor)
                    {
                        WriteLine($"public ScaffoldedFile File {{ get; set; }}");
                        WriteLine();
                        WriteLine($"public ScaffoldedFile Interface {{ get; set; }}");
                    }
                    else
                    {
                        WriteLine($"public IList<ScaffoldedFile> Files {{ get; }} = new List<ScaffoldedFile>();");
                    }
                }
            }
            return(GetText());
        }
Ejemplo n.º 2
0
        private static string GenerateGeneratorInterface(CodeGenerationOptions options, Kind kind)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, kind);

            WriteUsings(options, kind);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string typeName = NameHelper.MakeGeneratorInterfaceName(kind);

                WriteLine($"public interface {typeName}");
                using (OpenBlock())
                {
                    if (kind == Kind.Context || kind == Kind.Supervisor)
                    {
                        WriteLine($"string WriteClassCode(OpenApiDocument document, string @namespace);");
                        WriteLine();
                        WriteLine($"string WriteInterfaceCode(OpenApiDocument document, string @namespace);");
                    }
                    else
                    {
                        WriteLine($"string WriteCode(OpenApiSchema schema, string name, string @namespace);");
                    }
                }
            }
            return(GetText());
        }
Ejemplo n.º 3
0
        private static string GenerateContextInterface(CodeGenerationOptions options)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, Kind.Context);

            WriteUsings(options, Kind.Context);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string typeName = NameHelper.MakeContextInterfaceName();

                WriteLine($"public interface {typeName}");
                using (OpenBlock())
                {
                }
            }
            return(GetText());
        }
Ejemplo n.º 4
0
        private static string GenerateSupervisorClass(CodeGenerationOptions options)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, Kind.Supervisor);

            WriteUsings(options, Kind.Supervisor);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string typeName     = NameHelper.MakeSupervisorClassName();
                string baseTypeName = NameHelper.MakeSupervisorInterfaceName();

                WriteLine($"public class {typeName} : {baseTypeName}");
                using (OpenBlock())
                {
                }
            }
            return(GetText());
        }
Ejemplo n.º 5
0
 public static void WriteUsings(CodeGenerationOptions options, Kind skip)
 {
     WriteLine("using System;");
     WriteLine("using System.IO;");
     WriteLine("using System.Collections.Generic;");
     WriteLine("using Microsoft.OpenApi.Models;");
     WriteLine("using Microsoft.Extensions.DependencyInjection;");
     WriteLine($"using {options.RootNamespace}.Utilities;");
     WriteLine($"using {options.RootNamespace}.Scaffolding;");
     foreach (var kind in options.Kinds)
     {
         if (kind == skip)
         {
             continue;
         }
         string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, kind);
         WriteLine($"using {ns};");
     }
     WriteLine();
 }
Ejemplo n.º 6
0
        private static string GenerateContextModelClass(CodeGenerationOptions options)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, Kind.Context);

            WriteUsings(options, Kind.Context);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string typeName = NameHelper.MakeSupervisorModelClassName();

                WriteLine($"public class {typeName}");
                using (OpenBlock())
                {
                    WriteLine($"public ScaffoldedFile ContextClass {{ get; set; }}");
                    WriteLine();
                    WriteLine($"public ScaffoldedFile ContextInterface {{ get; set; }}");
                }
            }
            return(GetText());
        }
Ejemplo n.º 7
0
        private static string GenerateScaffolderInterface(CodeGenerationOptions options, Kind kind)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, kind);

            WriteUsings(options, kind);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string modelName = NameHelper.MakeModelClassName(kind);
                string typeName  = NameHelper.MakeScaffolderInterfaceName(kind);

                WriteLine($"public interface {typeName}");
                using (OpenBlock())
                {
                    WriteLine($"void Save({modelName} model);");
                    WriteLine();
                    WriteLine($"{modelName} ScaffoldModel(OpenApiOptions options);");
                }
            }
            return(GetText());
        }
Ejemplo n.º 8
0
        private static string GenerateGeneratorClass(CodeGenerationOptions options, Kind kind)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, kind);

            WriteUsings(options, kind);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string typeName     = NameHelper.MakeGeneratorClassName(kind);
                string baseTypeName = NameHelper.MakeGeneratorInterfaceName(kind);

                WriteLine($"public class {typeName} : AbstractGenerator, {baseTypeName}");
                using (OpenBlock())
                {
                    WriteLine($"public {typeName}(GeneratorDependencies dependencies) : base(dependencies)");
                    using (OpenBlock())
                    {
                    }

                    if (kind == Kind.Context || kind == Kind.Supervisor)
                    {
                        WriteLine($"public string WriteClassCode(OpenApiDocument document, string @namespace)");
                        using (OpenBlock())
                        {
                            WriteLine("Clear();");
                            WriteLine("GenerateFileHeader();");
                            WriteLine("WriteLine($\"namespace {@namespace}\");");
                            WriteLine("using (OpenBlock())");
                            using (OpenBlock())
                            {
                            }
                            WriteLine("return GetText();");
                        }
                        WriteLine();
                        WriteLine($"public string WriteInterfaceCode(OpenApiDocument document, string @namespace)");
                        using (OpenBlock())
                        {
                            WriteLine("Clear();");
                            WriteLine("GenerateFileHeader();");
                            WriteLine("WriteLine($\"namespace {@namespace}\");");
                            WriteLine("using (OpenBlock())");
                            using (OpenBlock())
                            {
                            }
                            WriteLine("return GetText();");
                        }
                    }
                    else
                    {
                        WriteLine($"public string WriteCode(OpenApiSchema schema, string name, string @namespace)");
                        using (OpenBlock())
                        {
                            WriteLine("Clear();");
                            WriteLine("GenerateFileHeader();");
                            WriteLine("WriteLine($\"namespace {@namespace}\");");
                            WriteLine("using (OpenBlock())");
                            using (OpenBlock())
                            {
                                WriteLine("");
                            }
                            WriteLine("return GetText();");
                        }
                    }
                }
            }
            return(GetText());
        }
Ejemplo n.º 9
0
        private static string GenerateScaffolderClass(CodeGenerationOptions options, Kind kind)
        {
            Clear();
            string ns = NamespaceHelper.MakeNamespace(options.RootNamespace, kind);

            WriteUsings(options, kind);
            WriteLine($"namespace {ns}");
            using (OpenBlock())
            {
                string modelName              = NameHelper.MakeModelClassName(kind);
                string typeName               = NameHelper.MakeScaffolderClassName(kind);
                string baseTypeName           = NameHelper.MakeScaffolderInterfaceName(kind);
                string generatorInterfaceName = NameHelper.MakeGeneratorInterfaceName(kind);

                WriteLine($"public class {typeName} : AbstractScaffolder, {baseTypeName}");
                using (OpenBlock())
                {
                    WriteLine($"public {typeName}(ScaffolderDependencies dependencies, {generatorInterfaceName} generator) : base(dependencies)");
                    using (OpenBlock())
                    {
                        WriteLine("Generator = generator;");
                    }

                    WriteLine();
                    WriteLine($"protected {generatorInterfaceName} Generator {{ get; }} ");

                    WriteLine();
                    WriteLine($"public void Save({modelName} model)");
                    using (OpenBlock())
                    {
                        if (kind == Kind.Context || kind == Kind.Supervisor)
                        {
                            WriteLine($"Dependencies.FileWriter.WriteFile(model.File);");
                            WriteLine($"Dependencies.FileWriter.WriteFile(model.Interface);");
                        }
                        else
                        {
                            WriteLine($"Dependencies.FileWriter.WriteFiles(model.Files);");
                        }
                    }
                    WriteLine();
                    WriteLine($"public {modelName} ScaffoldModel(OpenApiOptions options)");
                    using (OpenBlock())
                    {
                        WriteLine($"var model = new {modelName}();");

                        if (kind == Kind.Context)
                        {
                            WriteLine("var classFile = new ScaffoldedFile();");
                            WriteLine("var interfaceFile = new ScaffoldedFile();");
                            WriteLine("classFile.Code = Generator.WriteClassCode(options.Document, options.RootNamespace);");
                            WriteLine($"classFile.Path = Dependencies.PathHelper.Context(options.OutputDir, options.ContextClassName);");
                            WriteLine("interfaceFile.Code = Generator.WriteInterfaceCode(options.Document, options.RootNamespace);");
                            WriteLine($"interfaceFile.Path = Dependencies.PathHelper.Context(options.OutputDir, options.ContextInterfaceName);");
                            WriteLine("model.File = classFile;");
                            WriteLine("model.Interface = interfaceFile;");
                        }
                        else if (kind == Kind.Supervisor)
                        {
                            WriteLine("var classFile = new ScaffoldedFile();");
                            WriteLine("var interfaceFile = new ScaffoldedFile();");
                            WriteLine("classFile.Code = Generator.WriteClassCode(options.Document, options.RootNamespace);");
                            WriteLine($"classFile.Path = Dependencies.PathHelper.Supervisor(options.OutputDir, options.SupervisorClassName);");
                            WriteLine("interfaceFile.Code = Generator.WriteInterfaceCode(options.Document, options.RootNamespace);");
                            WriteLine($"interfaceFile.Path = Dependencies.PathHelper.Supervisor(options.OutputDir, options.SupervisorInterfaceName);");
                            WriteLine("model.File = classFile;");
                            WriteLine("model.Interface = interfaceFile;");
                        }
                        else
                        {
                            WriteLine("foreach(var kvp in options.Document.Components.Schemas)");
                            using (OpenBlock())
                            {
                                WriteLine("var name = kvp.Key;");
                                WriteLine("var schema = kvp.Value;");
                                WriteLine($"var code = Generator.WriteCode(schema, name, Dependencies.Namespace.{kind}(options.RootNamespace));");
                                WriteLine($"var path = Dependencies.PathHelper.{kind}(options.OutputDir, name);");
                                WriteLine($"var file = new ScaffoldedFile {{ Code = code, Path = path }};");
                                WriteLine($"model.Files.Add(file);");
                            }
                        }

                        WriteLine("return model;");
                    }
                }
            }
            return(GetText());
        }