Beispiel #1
0
 public string GenerateCode(IProgressReporter pGenerateProgress)
 {
     try
     {
         pGenerateProgress?.Progress(10);
         var document = documentFactory.GetDocumentAsync(swaggerFile).GetAwaiter().GetResult();
         pGenerateProgress?.Progress(20);
         var settings = generatorSettingsFactory.GetGeneratorSettings(document);
         pGenerateProgress?.Progress(50);
         var generator = new CSharpClientGenerator(document, settings);
         return(generator.GenerateFile());
     }
     finally
     {
         pGenerateProgress?.Progress(90);
     }
 }
Beispiel #2
0
        protected override string GetArguments(string outputFile)
        {
            var args = "--csharp " +
                       $"--input-file=\"{SwaggerFile}\" " +
                       $"--output-file=\"{outputFile}\" " +
                       $"--namespace=\"{DefaultNamespace}\" ";

            var document = documentFactory.GetDocumentAsync(SwaggerFile).GetAwaiter().GetResult();

            if (!string.IsNullOrEmpty(document.OpenApi) &&
                Version.TryParse(document.OpenApi, out var openApiVersion) &&
                openApiVersion > Version.Parse("3.0.0"))
            {
                args += "--v3 ";
            }

            if (options.AddCredentials)
            {
                args += "--add-credentials ";
            }

            args += $"--client-side-validation=\"{options.ClientSideValidation}\" ";
            args += $"--sync-methods=\"{options.SyncMethods}\" ";

            if (options.UseDateTimeOffset)
            {
                args += "--use-datetimeoffset ";
            }

            if (options.UseInternalConstructors)
            {
                args += " --use-internal-constructors ";
            }

            if (options.OverrideClientName)
            {
                var file = new FileInfo(SwaggerFile);
                var name = file.Name
                           .Replace(" ", string.Empty)
                           .Replace(file.Extension, string.Empty);

                args += $" --override-client-name=\"{name}\"";
            }

            return(args);
        }
Beispiel #3
0
        private string OnGenerateCode(IProgressReporter pGenerateProgress)
        {
            try
            {
                pGenerateProgress.Progress(10);

                var command = PathProvider.GetAutoRestPath();
                pGenerateProgress.Progress(30);

                dependencyInstaller.InstallAutoRest();
                pGenerateProgress.Progress(50);

                var document = documentFactory.GetDocumentAsync(SwaggerFile).GetAwaiter().GetResult();
                if (!string.IsNullOrEmpty(document.OpenApi) &&
                    Version.TryParse(document.OpenApi, out var openApiVersion) &&
                    openApiVersion > Version.Parse("3.0.0"))
                {
                    var outputFolder = Path.Combine(
                        Path.GetDirectoryName(SwaggerFile) ?? throw new InvalidOperationException(),
                        Guid.NewGuid().ToString("N"),
                        "TempApiClient");

                    if (!Directory.Exists(outputFolder))
                    {
                        Directory.CreateDirectory(outputFolder);
                    }

                    processLauncher.Start(
                        command,
                        argumentProvider.GetArguments(
                            outputFolder,
                            SwaggerFile,
                            DefaultNamespace),
                        Path.GetDirectoryName(SwaggerFile));

                    pGenerateProgress.Progress(80);

                    return(CSharpFileMerger.MergeFilesAndDeleteSource(outputFolder));
                }
                else
                {
                    var outputFile = FileHelper.CreateRandomFile();
                    var arguments  = argumentProvider.GetLegacyArguments(
                        outputFile,
                        SwaggerFile,
                        DefaultNamespace);

                    try
                    {
                        processLauncher.Start(
                            command,
                            arguments,
                            Path.GetDirectoryName(SwaggerFile));
                    }
                    catch (ProcessLaunchException)
                    {
                        processLauncher.Start(
                            command,
                            arguments.Replace("--version=", "--version "),
                            Path.GetDirectoryName(SwaggerFile));
                    }
                    finally
                    {
                        pGenerateProgress.Progress(80);
                    }

                    return(FileHelper.ReadThenDelete(outputFile));
                }
            }
            finally
            {
                pGenerateProgress.Progress(90);
            }
        }