public string GenerateCode(IProgressReporter pGenerateProgress)
        {
            try
            {
                pGenerateProgress.Progress(10);

                var jarFile = options.OpenApiGeneratorPath;
                if (!File.Exists(jarFile))
                {
                    Trace.WriteLine(jarFile + " does not exist");
                    jarFile = dependencyInstaller.InstallOpenApiGenerator();
                }

                pGenerateProgress.Progress(30);

                var output = Path.Combine(
                    Path.GetDirectoryName(swaggerFile) ?? throw new InvalidOperationException(),
                    Guid.NewGuid().ToString("N"),
                    "TempApiClient");

                Directory.CreateDirectory(output);
                pGenerateProgress.Progress(40);

                var arguments =
                    $"-jar \"{jarFile}\" generate " +
                    "--generator-name csharp-netcore " +
                    $"--input-spec \"{Path.GetFileName(swaggerFile)}\" " +
                    $"--output \"{output}\" " +
                    $"--package-name \"{defaultNamespace}\" " +
                    "--global-property apiTests=false,modelTests=false " +
                    "--skip-overwrite " +
                    $"--additional-properties optionalEmitDefaultValues={openApiGeneratorOptions.EmitDefaultValue} ";

                processLauncher.Start(
                    javaPathProvider.GetJavaExePath(),
                    arguments,
                    Path.GetDirectoryName(swaggerFile));

                pGenerateProgress.Progress(80);

                return(CSharpFileMerger.MergeFilesAndDeleteSource(output));
            }
            finally
            {
                pGenerateProgress.Progress(90);
            }
        }
Exemplo n.º 2
0
        public string GenerateCode(IProgressReporter pGenerateProgress)
        {
            try
            {
                pGenerateProgress.Progress(10);

                var jarFile = options.OpenApiGeneratorPath;
                if (!File.Exists(jarFile))
                {
                    Trace.WriteLine(jarFile + " does not exist");
                    jarFile = DependencyDownloader.InstallOpenApiGenerator();
                }

                pGenerateProgress.Progress(30);

                var output = Path.Combine(
                    Path.GetDirectoryName(swaggerFile) ?? throw new InvalidOperationException(),
                    "TempApiClient");

                Directory.CreateDirectory(output);
                pGenerateProgress.Progress(40);

                var arguments =
                    $"-jar \"{jarFile}\" generate " +
                    "-g csharp " +
                    $"--input-spec \"{swaggerFile}\" " +
                    $"--output \"{output}\" " +
                    "-DapiTests=false -DmodelTests=false " +
                    $"-DpackageName={defaultNamespace} " +
                    "--skip-overwrite ";

                processLauncher.Start(javaPathProvider.GetJavaExePath(), arguments);
                pGenerateProgress.Progress(80);

                return(CSharpFileMerger.MergeFilesAndDeleteSource(output));
            }
            finally
            {
                pGenerateProgress.Progress(90);
            }
        }
Exemplo n.º 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);
            }
        }