Ejemplo n.º 1
0
        private async System.Threading.Tasks.Task ExecuteAsync()
        {
            var options = new GeneratorOptions
            {
                LanguageName = "csharp",
                Namespace    = RootNamespace,
                ClientName   = ClientName
            };

            SwaggerDocument document = await GetSwaggerDocument(SwaggerDocumentUri);

            var generator            = new ServiceClientModelFactory(options);
            ServiceClientModel model = generator.Create(document);

            var             codeFactory = new ServiceClientCodeFactory();
            List <CodeFile> code        = codeFactory.GenerateCode(model, options);

            var outputDirectory = new DirectoryInfo(OutputDirectory);

            outputDirectory.Create();

            var generatedFiles = new List <ITaskItem>();

            foreach ((string path, string contents) in code)
            {
                string fullPath = Path.Combine(outputDirectory.FullName, path);
                var    file     = new FileInfo(fullPath);
                file.Directory.Create();
                Log.LogMessage(MessageImportance.Normal, $"Generating file '{file.FullName}'");
                File.WriteAllText(file.FullName, contents);
                generatedFiles.Add(new TaskItem(file.FullName));
            }

            GeneratedFiles = generatedFiles.ToArray();
        }
Ejemplo n.º 2
0
 public GenerateCodeContext(
     ServiceClientModel clientModel,
     GeneratorOptions options,
     Templates templates,
     Language language)
 {
     _language   = language;
     ClientModel = clientModel;
     Options     = options;
     Templates   = templates;
 }
Ejemplo n.º 3
0
        private async System.Threading.Tasks.Task ExecuteAsync()
        {
            var options = new GeneratorOptions
            {
                LanguageName = "csharp",
                Namespace    = RootNamespace,
                ClientName   = ClientName
            };

            Log.LogMessage(MessageImportance.Low, $"Reading swagger document {SwaggerDocumentUri}");
            var(diagnostic, document) = await GetSwaggerDocument(SwaggerDocumentUri);

            if (diagnostic.Errors.Any())
            {
                foreach (var error in diagnostic.Errors)
                {
                    Log.LogWarning(null, null, null, error.Pointer, 0, 0, 0, 0, error.Message);
                }
            }


            Log.LogMessage(MessageImportance.Low, $"Generating client code model");
            var generator            = new ServiceClientModelFactory(options);
            ServiceClientModel model = generator.Create(document);

            Log.LogMessage(MessageImportance.Low, $"Generating code files for language '{options.LanguageName}'");
            var             codeFactory = new ServiceClientCodeFactory();
            List <CodeFile> code        = codeFactory.GenerateCode(model, options);

            Log.LogMessage(MessageImportance.High, $"Generating {SwaggerDocumentUri} -> {OutputDirectory}");
            var outputDirectory = new DirectoryInfo(OutputDirectory);

            outputDirectory.Create();

            var generatedFiles = new List <ITaskItem>();

            foreach ((string path, string contents) in code)
            {
                string fullPath = Path.Combine(outputDirectory.FullName, path);
                var    file     = new FileInfo(fullPath);
                file.Directory.Create();
                Log.LogMessage(MessageImportance.Normal, $"Writing file '{file.FullName}'");
                File.WriteAllText(file.FullName, contents);
                generatedFiles.Add(new TaskItem(file.FullName));
            }

            GeneratedFiles = generatedFiles.ToArray();
        }
Ejemplo n.º 4
0
        public List <CodeFile> GenerateCode(ServiceClientModel model, GeneratorOptions options)
        {
            Language language = Language.Get(options.LanguageName);

            IHandlebars hb = Handlebars.Create();

            RegisterHelpers(hb, language, options);

            Templates templates = language.GetTemplates(hb);

            RegisterTemplates(hb, templates);

            var context = new GenerateCodeContext(model, options, templates, language);

            language.GenerateCode(context);

            return(context.Files.Values.ToList());
        }
Ejemplo n.º 5
0
        public List <CodeFile> GenerateCode(ServiceClientModel model, GeneratorOptions options)
        {
            Language language = Language.Get(options.LanguageName);

            IHandlebars hb = Handlebars.Create();

            RegisterHelpers(hb, language, options);

            Templates templates = language.GetTemplates(hb);

            var result = new List <CodeFile>();

            using (var writer = new StringWriter())
            {
                templates.ServiceClient(writer, model);
                result.Add(new CodeFile(options.ClientName + language.Extension, writer.ToString()));
            }

            foreach (TypeModel type in model.Types)
            {
                using (var writer = new StringWriter())
                {
                    templates.Model(writer, type);
                    result.Add(new CodeFile($"Models/{type.Name}{language.Extension}", writer.ToString()));
                }
            }

            foreach (MethodGroupModel group in model.MethodGroups)
            {
                using (var writer = new StringWriter())
                {
                    templates.MethodGroup(writer, group);
                    result.Add(new CodeFile(group.Name + language.Extension, writer.ToString()));
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        private static async Task <int> Main(string[] args)
        {
            string input            = null;
            string output           = null;
            var    version          = false;
            var    showHelp         = false;
            var    generatorOptions = new GeneratorOptions
            {
                LanguageName = "csharp",
                Namespace    = "Generated",
                ClientName   = "ApiClient",
            };

            var options = new OptionSet
            {
                { "i|input=", "The input swagger spec uri", s => input = s },
                { "o|output=", "The output directory for generated code", o => output = o },
                { "n|ns|namespace=", "The namespace for generated code", n => generatorOptions.Namespace = n },
                { "l|language=", "The language to generate code for", l => generatorOptions.LanguageName = l },
                { "c|client-name=", "The name of the generated client", c => generatorOptions.ClientName = c },
                { "version", "Display the version of this program.", v => version = v != null },
                { "h|?|help", "Display this help message.", h => showHelp = h != null },
            };

            List <string> arguments = options.Parse(args);

            if (version)
            {
                string versionString = Assembly.GetEntryAssembly()
                                       .GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                                       .InformationalVersion;
                Console.WriteLine(versionString);
                return(0);
            }

            if (showHelp)
            {
                options.WriteOptionDescriptions(Console.Out);
                return(0);
            }

            if (string.IsNullOrEmpty(input))
            {
                MissingArgument(nameof(input));
            }

            if (string.IsNullOrEmpty(output))
            {
                MissingArgument(nameof(output));
            }

            ILogger logger = new LoggerFactory().AddConsole().CreateLogger("dotnet-swaggergen");

            SwaggerDocument document = await GetSwaggerDocument(input);

            var generator            = new ServiceClientModelFactory(generatorOptions);
            ServiceClientModel model = generator.Create(document);

            var             codeFactory = new ServiceClientCodeFactory();
            List <CodeFile> code        = codeFactory.GenerateCode(model, generatorOptions, logger);

            var outputDirectory = new DirectoryInfo(output);

            outputDirectory.Create();

            foreach ((string path, string contents) in code)
            {
                string fullPath = Path.Combine(outputDirectory.FullName, path);
                var    file     = new FileInfo(fullPath);
                file.Directory.Create();
                File.WriteAllText(file.FullName, contents);
            }

            return(0);
        }