Пример #1
0
        private List <PortableExecutableReference> BuildReferenceSet(List <string> resolvedLocations,
                                                                     string[] manifestResoureNames,
                                                                     string manifestResourcePrefix,
                                                                     FunctionCompiler.TargetEnum target)
        {
            // Add our references - if the reference is to a library that forms part of NET Standard 2.0 then make sure we add
            // the reference from the embedded NET Standard reference set - although our target is NET Standard the assemblies
            // in the output folder of the Function App may be NET Core assemblies.
            List <PortableExecutableReference> references = resolvedLocations.Select(x =>
            {
                if (target == FunctionCompiler.TargetEnum.NETStandard20)
                {
                    string assemblyFilename     = Path.GetFileName(x);
                    string manifestResourceName =
                        manifestResoureNames.SingleOrDefault(m =>
                                                             String.Equals(assemblyFilename, m, StringComparison.CurrentCultureIgnoreCase));
                    if (manifestResourceName != null)
                    {
                        using (Stream lib = GetType().Assembly
                                            .GetManifestResourceStream(String.Concat(manifestResourcePrefix, manifestResourceName)))
                        {
                            return(MetadataReference.CreateFromStream(lib));
                        }
                    }
                }

                return(MetadataReference.CreateFromFile(x));
            }).ToList();

            if (target == FunctionCompiler.TargetEnum.NETStandard20)
            {
                using (Stream netStandard = GetType().Assembly
                                            .GetManifestResourceStream("FunctionMonkey.Compiler.references.netstandard2._0.netstandard.dll"))
                {
                    references.Add(MetadataReference.CreateFromStream(netStandard));
                }

                using (Stream netStandard = GetType().Assembly
                                            .GetManifestResourceStream("FunctionMonkey.Compiler.references.netstandard2._0.System.Runtime.dll"))
                {
                    references.Add(MetadataReference.CreateFromStream(netStandard));
                }

                using (Stream systemIo = GetType().Assembly
                                         .GetManifestResourceStream(String.Concat(manifestResourcePrefix, "System.IO.dll")))
                {
                    references.Add(MetadataReference.CreateFromStream(systemIo));
                }
            }

            return(references);
        }
Пример #2
0
        public void Compile(IReadOnlyCollection <AbstractFunctionDefinition> functionDefinitions,
                            Type functionAppConfigurationType,
                            string newAssemblyNamespace,
                            IReadOnlyCollection <string> externalAssemblyLocations,
                            string outputBinaryFolder,
                            string assemblyName,
                            OpenApiOutputModel openApiOutputModel,
                            FunctionCompiler.TargetEnum target,
                            string outputAuthoredSourceFolder = null)
        {
            HandlebarsHelperRegistration.RegisterHelpers();
            IReadOnlyCollection <SyntaxTree> syntaxTrees = CompileSource(functionDefinitions,
                                                                         openApiOutputModel,
                                                                         functionAppConfigurationType,
                                                                         newAssemblyNamespace,
                                                                         outputAuthoredSourceFolder);

            CompileAssembly(syntaxTrees, externalAssemblyLocations, openApiOutputModel, outputBinaryFolder, assemblyName, newAssemblyNamespace, target);
        }
Пример #3
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                throw new ArgumentException("Must specify the assembly file to build the functions from");
            }

            string inputAssemblyFile = args[0];

            FunctionCompiler.TargetEnum target = FunctionCompiler.TargetEnum.NETStandard20;
            if (args.Any(x => x.ToLower() == "--netcore21"))
            {
                target = FunctionCompiler.TargetEnum.NETCore21;
            }

            // TODO: convert the input to an absolute path if necessary
            Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(inputAssemblyFile);
            string   outputBinaryDirectory = Path.GetDirectoryName(assembly.Location);

            // Not sure why the AssemblyLoadContext doesn't deal with the below. I thought it did. Clearly not.
            // TODO: Have a chat with someone who knows a bit more about this.
            AssemblyLoadContext.Default.Resolving += (context, name) =>
            {
                string path = Path.Combine(outputBinaryDirectory, $"{name.Name}.dll");
                //string path = $"{outputBinaryDirectory}\\{name.Name}.dll";
                if (File.Exists(path))
                {
                    Assembly referencedAssembly = context.LoadFromAssemblyPath(path);
                    return(referencedAssembly);
                }
                return(null);
            };

            FunctionCompiler compiler = new FunctionCompiler(assembly, outputBinaryDirectory, target);

            compiler.Compile();
        }
Пример #4
0
        private static IReadOnlyCollection <string> BuildCandidateReferenceList(IReadOnlyCollection <string> externalAssemblyLocations, FunctionCompiler.TargetEnum target)
        {
            // These are assemblies that Roslyn requires from usage within the template
            HashSet <string> locations = new HashSet <string>
            {
                typeof(Runtime).GetTypeInfo().Assembly.Location,
                typeof(IStreamCommand).Assembly.Location,
                typeof(AzureFromTheTrenches.Commanding.Abstractions.ICommand).GetTypeInfo().Assembly.Location,
                typeof(Abstractions.ISerializer).GetTypeInfo().Assembly.Location,
                typeof(System.Net.Http.HttpMethod).GetTypeInfo().Assembly.Location,
                typeof(System.Net.HttpStatusCode).GetTypeInfo().Assembly.Location,
                typeof(HttpRequest).Assembly.Location,
                typeof(JsonConvert).GetTypeInfo().Assembly.Location,
                typeof(OkObjectResult).GetTypeInfo().Assembly.Location,
                typeof(IActionResult).GetTypeInfo().Assembly.Location,
                typeof(FunctionNameAttribute).GetTypeInfo().Assembly.Location,
                typeof(ILogger).GetTypeInfo().Assembly.Location,
                typeof(IServiceProvider).GetTypeInfo().Assembly.Location,
                typeof(IHeaderDictionary).GetTypeInfo().Assembly.Location,
                typeof(StringValues).GetTypeInfo().Assembly.Location,
                typeof(ExecutionContext).GetTypeInfo().Assembly.Location,
                typeof(Document).GetTypeInfo().Assembly.Location,
                typeof(Message).GetTypeInfo().Assembly.Location,
                typeof(ChangeFeedProcessorBuilder).Assembly.Location,
                typeof(CosmosDBAttribute).Assembly.Location,
                typeof(TimerInfo).Assembly.Location,
                typeof(DbConnectionStringBuilder).Assembly.Location,
                typeof(AzureSignalRAuthClient).Assembly.Location,
                typeof(System.Environment).Assembly.Location,
                typeof(HttpTriggerAttribute).Assembly.Location,
                typeof(ServiceBusAttribute).Assembly.Location,
                typeof(QueueAttribute).Assembly.Location,
                typeof(Microsoft.IdentityModel.Protocols.HttpDocumentRetriever).Assembly.Location
            };

            if (target == FunctionCompiler.TargetEnum.NETCore21)
            {
                // we're a 2.1 assembly so we can use our assemblies
                Assembly[] currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();

                locations.Add(currentAssemblies.Single(x => x.GetName().Name == "netstandard").Location);
                locations.Add(currentAssemblies.Single(x => x.GetName().Name == "System.Runtime").Location); // System.Runtime
                locations.Add(typeof(TargetFrameworkAttribute).Assembly.Location);                           // NetCoreLib
                locations.Add(typeof(System.Linq.Enumerable).Assembly.Location);                             // System.Linq
                locations.Add(typeof(System.Security.Claims.ClaimsPrincipal).Assembly.Location);
                locations.Add(typeof(System.Uri).Assembly.Location);
                locations.Add(currentAssemblies.Single(x => x.GetName().Name == "System.Collections").Location);
                locations.Add(currentAssemblies.Single(x => x.GetName().Name == "System.Threading").Location);
            }

            foreach (string externalAssemblyLocation in externalAssemblyLocations)
            {
                locations.Add(externalAssemblyLocation);
            }

            return(locations);
        }
Пример #5
0
        private void CompileAssembly(IReadOnlyCollection <SyntaxTree> syntaxTrees,
                                     IReadOnlyCollection <string> externalAssemblyLocations,
                                     OpenApiOutputModel openApiOutputModel,
                                     string outputBinaryFolder,
                                     string outputAssemblyName,
                                     string assemblyNamespace,
                                     FunctionCompiler.TargetEnum target)
        {
            IReadOnlyCollection <string> locations = BuildCandidateReferenceList(externalAssemblyLocations, target);
            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, target);
            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());
                }
            }
        }