Пример #1
0
        protected List <ResourceDescription> CreateResources(string assemblyNamespace)
        {
            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));
                    }
                }
            }

            return(resources);
        }
Пример #2
0
        private OpenApiFileReference[] CopySwaggerUserInterfaceFilesToWebFolder()
        {
            const string prefix         = "FunctionMonkey.Compiler.Core.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);
        }
Пример #3
0
        private bool CompileAssembly(IReadOnlyCollection <SyntaxTree> syntaxTrees,
                                     IReadOnlyCollection <string> externalAssemblyLocations,
                                     OpenApiOutputModel openApiOutputModel,
                                     string outputBinaryFolder,
                                     string outputAssemblyName,
                                     string assemblyNamespace,
                                     CompileTargetEnum compileTarget,
                                     bool isFSharpProject)
        {
            IReadOnlyCollection <string> locations = BuildCandidateReferenceList(externalAssemblyLocations, compileTarget, isFSharpProject);
            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, compileTarget);

            CSharpCompilation compilation = CSharpCompilation.Create(outputAssemblyName)
                                            .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                                            .AddReferences(references)
                                            .AddSyntaxTrees(syntaxTrees)
            ;

            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));
                    }
                }
            }

            string     outputFilename    = Path.Combine(outputBinaryFolder, outputAssemblyName);
            EmitResult compilationResult = compilation.Emit(outputFilename, manifestResources: resources);

            if (!compilationResult.Success)
            {
                IEnumerable <Diagnostic> failures = compilationResult.Diagnostics.Where(diagnostic =>
                                                                                        diagnostic.IsWarningAsError ||
                                                                                        diagnostic.Severity == DiagnosticSeverity.Error);

                foreach (Diagnostic diagnostic in failures)
                {
                    _compilerLog.Error($"Error compiling function: {diagnostic.ToString()}");
                }
            }

            return(compilationResult.Success);
        }