/// <summary>
        /// Strongly-typed version of <see cref="CompileTarget(ITargetCompiler, ITarget, ResolveContext, ITargetContainer)"/> when the
        /// <typeparamref name="TService"/> is known at compile-time.
        /// </summary>
        /// <typeparam name="TService">The type of object to be returned by the factory.S</typeparam>
        /// <param name="compiler">Required.  The compiler.</param>
        /// <param name="target">Required.  The target to be compiled.</param>
        /// <param name="resolveContext">Required.  The current <see cref="ResolveContext"/></param>
        /// <param name="targets">Required.  Used to locate dependencies during compilation.</param>
        /// <returns>A strongly-typed delegate representing the passed <paramref name="target"/>.
        /// Invoking this delegate should produce an instance of the type <typeparamref name="TService"/>.</returns>
        public static Func <ResolveContext, TService> CompileTarget <TService>(this ITargetCompiler compiler,
                                                                               ITarget target,
                                                                               ResolveContext resolveContext,
                                                                               ITargetContainer targets)
        {
            if (compiler == null)
            {
                throw new ArgumentNullException(nameof(compiler));
            }

            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (resolveContext == default)
            {
                throw new ArgumentNullException(nameof(resolveContext));
            }

            if (targets == null)
            {
                throw new ArgumentNullException(nameof(targets));
            }

            return(compiler.CompileTarget <TService>(target, compiler.CreateContext(resolveContext, targets)));
        }
        protected override void ReleaseCompiler(ITargetCompiler compiler)
        {
            TargetAssemblyCompiler compiler2 = compiler as TargetAssemblyCompiler;

            string assemblyFileName = compiler2.AssemblyBuilder.GetName().Name + ".dll";

            try
            {
                //TODO: Get this to create a folder.
                compiler2.AssemblyBuilder.Save(assemblyFileName);
                Debug.WriteLine("Saved {0}", (object)assemblyFileName);
            }
            catch (Exception)
            {
                Debug.WriteLine("Failed to save {0}", (object)assemblyFileName);
            }
        }
        /// <summary>
        /// Performs a late-bound compilation of a strongly-typed delegate for the <paramref name="target" />.
        /// The type of the returned delegate will be <see cref="Func{T, TResult}" />, with `TResult` equal to
        /// the <see cref="ICompileContext.TargetType" /> of the <paramref name="context" />.
        /// </summary>
        /// <param name="compiler">The compiler.</param>
        /// <param name="target">The target.</param>
        /// <param name="context">The context.</param>
        /// <returns>A compiled delegate</returns>
        /// <exception cref="ArgumentNullException">If any of <paramref name="compiler"/>, <paramref name="context"/> or <paramref name="target"/> are null.</exception>
        public static Delegate CompileTargetStrong(this ITargetCompiler compiler, ITarget target, ICompileContext context)
        {
            if (compiler == null)
            {
                throw new ArgumentNullException(nameof(compiler));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return((Delegate)CompileTargetGenericMethod.MakeGenericMethod(
                       context.TargetType)
                   .Invoke(compiler, new object[] { target, context }));
        }
Ejemplo n.º 4
0
        public bool Compile()
        {
            string newAssemblyNamespace = $"{_configurationSourceAssembly.GetName().Name.Replace("-", "_")}.Functions";
            IFunctionCompilerMetadata functionCompilerMetadata = null;

            IFunctionAppConfiguration configuration  = null;
            FunctionAppHostBuilder    appHostBuilder = null;
            IFunctionAppHost          appHost        = ConfigurationLocator.FindFunctionAppHost(_configurationSourceAssembly);

            if (appHost != null)
            {
                appHostBuilder = new FunctionAppHostBuilder();
                appHost.Build(appHostBuilder);
                if (appHostBuilder.FunctionAppConfiguration != null)
                {
                    configuration = (IFunctionAppConfiguration)Activator.CreateInstance(appHostBuilder.FunctionAppConfiguration);
                }
            }

            if (configuration == null)
            {
                configuration = ConfigurationLocator.FindConfiguration(_configurationSourceAssembly);
            }

            if (configuration == null)
            {
                functionCompilerMetadata = ConfigurationLocator.FindCompilerMetadata(_configurationSourceAssembly);
                if (functionCompilerMetadata == null)
                {
                    _compilerLog.Error($"The assembly {_configurationSourceAssembly.GetName().Name} does not contain a public class implementing the IFunctionAppConfiguration interface");
                    return(false);
                }
            }
            else
            {
                FunctionHostBuilder builder = new FunctionHostBuilder(_serviceCollection, _commandRegistry, false);
                if (appHostBuilder != null)
                {
                    builder.Options = appHostBuilder.Options;
                }
                configuration.Build(builder);
                DefaultMediatorSettings.SetDefaultsIfRequired(builder);
                if (!ValidateCommandTypes(builder))
                {
                    return(false);
                }
                IMediatorResultTypeExtractor extractor = CreateMediatorResultTypeExtractor(builder.Options.MediatorResultTypeExtractor);
                if (extractor == null)
                {
                    return(false);
                }
                new PostBuildPatcher(extractor).Patch(builder, newAssemblyNamespace);
                if (!VerifyCommandAndResponseTypes(builder))
                {
                    return(false);
                }

                if (!VerifyOutputBindings(builder))
                {
                    return(false);
                }

                functionCompilerMetadata = new FunctionCompilerMetadata
                {
                    FunctionDefinitions        = builder.FunctionDefinitions,
                    OpenApiConfiguration       = builder.OpenApiConfiguration,
                    OutputAuthoredSourceFolder = builder.Options.OutputSourceTo,
                    CompilerOptions            = builder.Options
                };
            }

            PostBuildPatcher.EnsureFunctionsHaveUniqueNames(functionCompilerMetadata.FunctionDefinitions);

            IReadOnlyCollection <string> externalAssemblies =
                GetExternalAssemblyLocations(functionCompilerMetadata.FunctionDefinitions);

            ITargetCompiler targetCompiler = functionCompilerMetadata.CompilerOptions.HttpTarget == CompileTargetEnum.AzureFunctions
                ? (ITargetCompiler) new AzureFunctionsCompiler(_compilerLog)
                : new AspNetCoreCompiler(_compilerLog);

            return(targetCompiler.CompileAssets(functionCompilerMetadata,
                                                newAssemblyNamespace,
                                                configuration,
                                                externalAssemblies,
                                                _outputBinaryFolder));
        }