Exemple #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();
        }
Exemple #2
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();
        }
Exemple #3
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);
        }