public static async Task<IEnumerable<Type>> CompileTypes(MemoryFileSystem fileSystem) { // compile var result = await Compile(fileSystem); // filter the warnings var warnings = result.Messages.Where( each => each.Severity == DiagnosticSeverity.Warning && !SuppressWarnings.Contains(each.Id)).ToArray(); // filter the errors var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray(); if (!result.Succeeded) { throw new Exception("compilation failed: " + string.Join(", ", errors.Concat(warnings))); } // try to load the assembly var asm = Assembly.Load(result.Output.GetBuffer()); if (asm == null) { throw new Exception("could not load assembly"); } return asm.ExportedTypes; }
private static async Task TestGenerate(string apiVersion, string[] methodUrls, string expectedJsonString) { using (NewContext) { MemoryFileSystem fileSystem = new MemoryFileSystem(); Settings settings = new Settings(); settings.FileSystem = fileSystem; CodeModel serviceClient = New<CodeModel>(); serviceClient.ApiVersion = apiVersion; foreach (string methodUrl in methodUrls) { serviceClient.Add(New<Method>(new { Url = methodUrl, HttpMethod = HttpMethod.Put, })); } await CreatePlugin().CodeGenerator.Generate(serviceClient); Assert.Equal(2, fileSystem.VirtualStore.Count); string folderPath = fileSystem.VirtualStore.Keys.First(); Assert.Equal("Folder", fileSystem.VirtualStore[folderPath].ToString()); JObject expectedJSON = JObject.Parse(expectedJsonString); string fileContents = fileSystem.VirtualStore[fileSystem.VirtualStore.Keys.Skip(1).First()].ToString(); JObject actualJson = JObject.Parse(fileContents); Assert.Equal(expectedJSON, actualJson); } }
internal static MemoryFileSystem GenerateCodeInto(this string inputFile, MemoryFileSystem fileSystem) { var settings = new Settings { Modeler = "Swagger", CodeGenerator = "CSharp", FileSystem = fileSystem, OutputDirectory = "GeneratedCode", Namespace = "Test" }; return inputFile.GenerateCodeInto(fileSystem, settings); }
internal static MemoryFileSystem GenerateCodeInto(this string inputFile, MemoryFileSystem fileSystem, Settings settings) { string fileName = Path.GetFileName(inputFile); // If inputFile does not contain a path use the local Resource folder if (inputFile == fileName) { fileSystem.Copy(Path.Combine("Resource", inputFile)); } else { fileSystem.Copy(inputFile); } settings.Input = fileName; var plugin = ExtensionsLoader.GetPlugin(); var modeler = ExtensionsLoader.GetModeler(); var codeModel = modeler.Build(); // After swagger Parser codeModel = AutoRestController.RunExtensions(Trigger.AfterModelCreation, codeModel); // After swagger Parser codeModel = AutoRestController.RunExtensions(Trigger.BeforeLoadingLanguageSpecificModel, codeModel); using (plugin.Activate()) { // load model into language-specific code model codeModel = plugin.Serializer.Load(codeModel); // we've loaded the model, run the extensions for after it's loaded codeModel = AutoRestController.RunExtensions(Trigger.AfterLoadingLanguageSpecificModel, codeModel); // apply language-specific tranformation (more than just language-specific types) // used to be called "NormalizeClientModel" . codeModel = plugin.Transformer.TransformCodeModel(codeModel); // next set of extensions codeModel = AutoRestController.RunExtensions(Trigger.AfterLanguageSpecificTransform, codeModel); // next set of extensions codeModel = AutoRestController.RunExtensions(Trigger.BeforeGeneratingCode, codeModel); // Generate code from CodeModel. plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult(); } return fileSystem; }
internal static MemoryFileSystem GenerateCodeInto(this string inputDir, MemoryFileSystem fileSystem, string codeGenerator="CSharp", string modeler = "Swagger") { using (NewContext) { var settings = new Settings { Modeler = modeler, CodeGenerator =codeGenerator, FileSystem = fileSystem, OutputDirectory = "GeneratedCode", Namespace = "Test" }; return inputDir.GenerateCodeInto(fileSystem, settings); } }
public static MemoryFileSystem GenerateCodeForTest(string json, string codeGenerator, Action<IEnumerable<ValidationMessage>> processMessages) { using (NewContext) { var fs = new MemoryFileSystem(); var settings = new Settings { Modeler = "Swagger", CodeGenerator = codeGenerator, FileSystem = fs, OutputDirectory = "GeneratedCode", Namespace = "Test", Input = "input.json" }; fs.WriteFile(settings.Input, json); fs.WriteFile("AutoRest.json", autoRestJson); GenerateCodeInto(processMessages); return fs; } }
internal static MemoryFileSystem GenerateCodeInto(this string inputFile, MemoryFileSystem fileSystem, Settings settings) { string fileName = Path.GetFileName(inputFile); // If inputFile does not contain a path use the local Resource folder if (inputFile == fileName) { fileSystem.Copy(Path.Combine("Resource", inputFile)); } else { fileSystem.Copy(inputFile); } settings.Input = fileName; var codeGenerator = new CSharpCodeGenerator(settings); var modeler = ExtensionsLoader.GetModeler(settings); var sc = modeler.Build(); codeGenerator.NormalizeClientModel(sc); codeGenerator.Generate(sc).GetAwaiter().GetResult(); return fileSystem; }
protected void Write(IEnumerable<Diagnostic> messages, MemoryFileSystem fileSystem) { if (messages.Any()) { foreach (var file in messages.GroupBy(each => each.Location?.SourceTree?.FilePath, each => each)) { var text = file.Key != null ? fileSystem.VirtualStore[file.Key].ToString() : string.Empty; foreach (var error in file) { WriteLine(error.ToString()); // WriteLine(text.Substring(error.Location.SourceSpan.Start, error.Location.SourceSpan.Length)); } } } }
protected virtual MemoryFileSystem CreateMockFilesystem() { var fs = new MemoryFileSystem(); fs.Copy(Path.Combine("Resource", "AutoRest.json")); return fs; }
internal static MemoryFileSystem GenerateCodeInto(this string inputDir, MemoryFileSystem fileSystem, Settings settings) { fileSystem.Copy(Path.Combine("Resource", inputDir)); var fileExt = (File.Exists(Path.Combine("Resource", Path.Combine(inputDir, inputDir + ".yaml"))) ? ".yaml" : ".json"); settings.Input = Path.Combine("Resource", Path.Combine(inputDir, inputDir + fileExt)); var plugin = ExtensionsLoader.GetPlugin(); var modeler = ExtensionsLoader.GetModeler(); var codeModel = modeler.Build(); // After swagger Parser codeModel = AutoRestController.RunExtensions(Trigger.AfterModelCreation, codeModel); // After swagger Parser codeModel = AutoRestController.RunExtensions(Trigger.BeforeLoadingLanguageSpecificModel, codeModel); using (plugin.Activate()) { // load model into language-specific code model codeModel = plugin.Serializer.Load(codeModel); // we've loaded the model, run the extensions for after it's loaded codeModel = AutoRestController.RunExtensions(Trigger.AfterLoadingLanguageSpecificModel, codeModel); // apply language-specific tranformation (more than just language-specific types) // used to be called "NormalizeClientModel" . codeModel = plugin.Transformer.TransformCodeModel(codeModel); // next set of extensions codeModel = AutoRestController.RunExtensions(Trigger.AfterLanguageSpecificTransform, codeModel); // next set of extensions codeModel = AutoRestController.RunExtensions(Trigger.BeforeGeneratingCode, codeModel); // Generate code from CodeModel. plugin.CodeGenerator.Generate(codeModel).GetAwaiter().GetResult(); } return fileSystem; }