private async Task OnExecuteAsync(DTE dte, AsyncPackage package) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var result = EnterOpenApiSpecDialog.GetResult(); if (result == null) { return; } var selectedItem = ProjectExtensions.GetSelectedItem(); var folder = FindFolder(selectedItem, dte); if (string.IsNullOrWhiteSpace(folder)) { Trace.WriteLine("Unable to get folder name"); return; } var contents = result.OpenApiSpecification; var filename = result.OutputFilename + ".json"; if (CodeGenerator == SupportedCodeGenerator.NSwagStudio) { var outputNamespace = ProjectExtensions.GetActiveProject(dte)?.GetTopLevelNamespace(); contents = await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( result, new NSwagStudioOptions(), outputNamespace); filename = filename.Replace(".json", ".nswag"); } var filePath = Path.Combine(folder, filename); File.WriteAllText(filePath, contents); var fileInfo = new FileInfo(filePath); var project = ProjectExtensions.GetActiveProject(dte); var projectItem = project.AddFileToProject(dte, fileInfo, "None"); projectItem.Properties.Item("BuildAction").Value = prjBuildAction.prjBuildActionNone; if (CodeGenerator != SupportedCodeGenerator.NSwagStudio) { var customTool = CodeGenerator.GetCustomToolName(); projectItem.Properties.Item("CustomTool").Value = customTool; } else { var generator = new NSwagStudioCodeGenerator(filePath, new CustomPathOptions(), new ProcessLauncher()); generator.GenerateCode(null); dynamic nswag = JsonConvert.DeserializeObject(contents); var nswagOutput = nswag.codeGenerators.swaggerToCSharpClient.output.ToString(); project.AddFileToProject(dte, new FileInfo(Path.Combine(folder, nswagOutput))); } await project.InstallMissingPackagesAsync(package, CodeGenerator); }
public async Task NSwagStudio_Generate_Code_Using_NSwagStudio() { var contents = await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( File.ReadAllText("Swagger.json"), "https://petstore.swagger.io/v2/swagger.json"); File.WriteAllText("Petstore.nswag", contents); new NSwagStudioCodeGenerator(Path.GetFullPath("Petstore.nswag"), options) .GenerateCode(new Mock <IProgressReporter>().Object) .Should() .BeNull(); var outputFile = Path.GetFullPath("PetstoreClient.cs"); File.Exists(outputFile) .Should() .BeTrue(); var csharp = File.ReadAllText(outputFile); var result = await CodeConverter.Convert(new CodeWithOptions(csharp)); result.ConvertedCode .Should() .NotBeNullOrWhiteSpace(); }
protected override Task OnInitializeAsync() => NSwagStudioFileHelper.CreateNSwagStudioFileAsync( new EnterOpenApiSpecDialogResult( ReadAllText(SwaggerYaml), "Swagger", "https://petstore.swagger.io/v2/swagger.yaml"), mock.Object);
public async Task InitAsync() { mock = new Mock <INSwagStudioOptions>(); await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( new EnterOpenApiSpecDialogResult(File.ReadAllText("Swagger.json"), "Swagger", "https://petstore.swagger.io/v2/swagger.json"), mock.Object); }
public async Task InitAsync() { mock = new Mock <INSwagStudioOptions>(); await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( File.ReadAllText("Swagger.json"), "https://petstore.swagger.io/v2/swagger.json", mock.Object); }
protected override async Task OnInitializeAsync() { mock = new Mock <INSwagStudioOptions>(); await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( new EnterOpenApiSpecDialogResult( ReadAllText(SwaggerYaml), "Swagger", "https://petstore.swagger.io/v2/swagger.yaml"), mock.Object); }
protected override async Task AddFile(string itemPath, string url) { var filename = Path.Combine(itemPath, "Swagger.nswag"); var swaggerJson = await DownloadTextAsync(url); var contents = await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( swaggerJson, url); File.WriteAllText(filename, contents); IdeApp.ProjectOperations.CurrentSelectedProject.AddFile(filename, BuildAction.None); command.Run(filename); }
public NSwagStudioFileHelperTests() { mock = new Mock <INSwagStudioOptions>(); NSwagStudioFileHelper.CreateNSwagStudioFileAsync( new EnterOpenApiSpecDialogResult( File.ReadAllText("Swagger.json"), "Swagger", "https://petstore.swagger.io/v2/swagger.json"), mock.Object) .GetAwaiter() .GetResult(); }
public async Task NSwagStudio_Generate_Code_Using_NSwagStudio_From_SwaggerSpec() { var contents = await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( new EnterOpenApiSpecDialogResult(File.ReadAllText("Swagger_v3.json"), "Swagger", "https://petstore.swagger.io/v2/swagger.json"), new Mock <INSwagStudioOptions>().Object); File.WriteAllText("Petstore.nswag", contents); new NSwagStudioCodeGenerator(Path.GetFullPath("Petstore.nswag"), options, new ProcessLauncher()) .GenerateCode(new Mock <IProgressReporter>().Object) .Should() .BeNull(); File.Exists(Path.GetFullPath("PetstoreClient.cs")) .Should() .BeTrue(); }
public async Task NSwagStudio_Generate_Code_Using_NSwagStudio_From_SwaggerSpec() { var contents = await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( File.ReadAllText(Path.GetFullPath("Swagger.json")), "https://petstore.swagger.io/v2/swagger.json"); File.WriteAllText("Petstore.nswag", contents); new NSwagStudioCodeGenerator(Path.GetFullPath("Petstore.nswag"), options) .GenerateCode(new Mock <IVsGeneratorProgress>().Object) .Should() .BeNull(); File.Exists(Path.GetFullPath("PetstoreClient.cs")) .Should() .BeTrue(); }
protected override async Task OnInitializeAsync() { var generalOptions = new Mock <IGeneralOptions>(); generalOptions.Setup(c => c.NSwagPath).Returns(PathProvider.GetNSwagPath()); var options = new Mock <INSwagStudioOptions>(); options.Setup(c => c.UseDocumentTitle).Returns(false); options.Setup(c => c.GenerateDtoTypes).Returns(true); var outputFilename = $"PetstoreClient{Guid.NewGuid():N}"; var contents = await NSwagStudioFileHelper.CreateNSwagStudioFileAsync( new EnterOpenApiSpecDialogResult( ReadAllText(SwaggerYaml), outputFilename, "https://petstore.swagger.io/v2/swagger.yaml"), options.Object); var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); Directory.CreateDirectory(folder); var tempFile = Path.Combine(folder, "Petstore.nswag"); File.WriteAllText(tempFile, contents); new NSwagStudioCodeGenerator( tempFile, generalOptions.Object, new ProcessLauncher(), new DependencyInstaller( new NpmInstaller(new ProcessLauncher()), new FileDownloader(new WebDownloader()))) .GenerateCode(new Mock <IProgressReporter>().Object) .Should() .BeNull(); Code = File.ReadAllText( Path.Combine( Path.GetDirectoryName(tempFile) ?? throw new InvalidOperationException(), $"{outputFilename}.cs")); Code .Should() .NotBeNullOrWhiteSpace(); }
public static void Init(TestContext testContext) { optionsMock = new Mock <IGeneralOptions>(); optionsMock.Setup(c => c.NSwagPath).Returns(PathProvider.GetNSwagPath()); options = optionsMock.Object; var contents = NSwagStudioFileHelper.CreateNSwagStudioFileAsync( new EnterOpenApiSpecDialogResult(File.ReadAllText("Swagger.json"), "Swagger", "https://petstore.swagger.io/v2/swagger.json")) .GetAwaiter() .GetResult(); File.WriteAllText("Swagger.nswag", contents); new NSwagStudioCodeGenerator(Path.GetFullPath("Swagger.nswag"), options, new ProcessLauncher()) .GenerateCode(new Mock <IProgressReporter>().Object) .Should() .BeNull(); code = File.ReadAllText(Path.GetFullPath("PetstoreClient.cs")); }