private OpenApiFileReference[] CopySwaggerUserInterfaceFilesToWebFolder() { const string prefix = "FunctionMonkey.Compiler.node_modules.swagger_ui_dist."; Assembly sourceAssembly = GetType().Assembly; string[] files = sourceAssembly .GetManifestResourceNames() .Where(x => x.StartsWith(prefix)) .ToArray(); OpenApiFileReference[] result = new OpenApiFileReference[files.Length]; int index = 0; foreach (string swaggerFile in files) { byte[] input = new byte[0]; using (Stream inputStream = sourceAssembly.GetManifestResourceStream(swaggerFile)) { if (inputStream != null) { input = new byte[inputStream.Length]; inputStream.Read(input, 0, input.Length); } } string content = Encoding.UTF8.GetString(input); if (swaggerFile.EndsWith(".index.html")) { content = content.Replace("http://petstore.swagger.io/v2/swagger.json", "./openapi/openapi.yaml"); content = content.Replace("https://petstore.swagger.io/v2/swagger.json", "./openapi/openapi.yaml"); content = content.Replace("=\"./swagger", $"=\"./openapi/swagger"); } result[index] = new OpenApiFileReference { Content = content, Filename = swaggerFile.Substring(prefix.Length) }; index++; } return(result); }
private void CompileAssembly(IReadOnlyCollection <SyntaxTree> syntaxTrees, IReadOnlyCollection <string> externalAssemblyLocations, OpenApiOutputModel openApiOutputModel, string outputBinaryFolder, string outputAssemblyName, string assemblyNamespace) { IReadOnlyCollection <string> locations = BuildCandidateReferenceList(externalAssemblyLocations); const string manifestResourcePrefix = "FunctionMonkey.Compiler.references.netstandard2._0."; // For each assembly we've found we need to check and see if it is already included in the output binary folder // If it is then its referenced already by the function host and so we add a reference to that version. List <string> resolvedLocations = ResolveLocationsWithExistingReferences(outputBinaryFolder, locations); string[] manifestResoureNames = GetType().Assembly.GetManifestResourceNames() .Where(x => x.StartsWith(manifestResourcePrefix)) .Select(x => x.Substring(manifestResourcePrefix.Length)) .ToArray(); List <PortableExecutableReference> references = BuildReferenceSet(resolvedLocations, manifestResoureNames, manifestResourcePrefix); CSharpCompilation compilation = CSharpCompilation.Create(outputAssemblyName, syntaxTrees, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); List <ResourceDescription> resources = null; if (openApiOutputModel != null) { resources = new List <ResourceDescription>(); Debug.Assert(openApiOutputModel.OpenApiSpecification != null); resources.Add(new ResourceDescription($"{assemblyNamespace}.OpenApi.{openApiOutputModel.OpenApiSpecification.Filename}", () => new MemoryStream(Encoding.UTF8.GetBytes(openApiOutputModel.OpenApiSpecification.Content)), true)); if (openApiOutputModel.SwaggerUserInterface != null) { foreach (OpenApiFileReference fileReference in openApiOutputModel.SwaggerUserInterface) { OpenApiFileReference closureCapturedFileReference = fileReference; resources.Add(new ResourceDescription($"{assemblyNamespace}.OpenApi.{closureCapturedFileReference.Filename}", () => new MemoryStream(Encoding.UTF8.GetBytes(closureCapturedFileReference.Content)), true)); } } } using (Stream stream = new FileStream(Path.Combine(outputBinaryFolder, outputAssemblyName), FileMode.Create)) { EmitResult result = compilation.Emit(stream, manifestResources: resources); if (!result.Success) { IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error); StringBuilder messageBuilder = new StringBuilder(); foreach (Diagnostic diagnostic in failures) { messageBuilder.AppendFormat("{0}:{1} {2}", diagnostic.Id, diagnostic.GetMessage(), diagnostic.Location.ToString()); } throw new ConfigurationException(messageBuilder.ToString()); } } }