Example #1
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));
        }
Example #2
0
        public RuntimeInstance(Assembly functionAppConfigurationAssembly,
                               Action <IServiceCollection, ICommandRegistry> beforeServiceProviderBuild,
                               IServiceCollection serviceCollection)
        {
            if (serviceCollection == null)
            {
                System.Console.WriteLine("No service collection supplied by runtime");
            }
            ServiceCollection    = serviceCollection ?? new ServiceCollection();
            BuiltServiceProvider = new Lazy <IServiceProvider>(() => ServiceCollection.BuildServiceProvider());

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

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

            CommandingDependencyResolverAdapter adapter = new CommandingDependencyResolverAdapter(
                (fromType, toInstance) => ServiceCollection.AddSingleton(fromType, toInstance),
                (fromType, toType) => ServiceCollection.AddTransient(fromType, toType),
                resolveType => ServiceProvider.GetService(resolveType)
                );

            ICommandRegistry commandRegistry;

            // ReSharper disable once SuspiciousTypeConversion.Global - externally provided
            if (configuration != null && configuration is ICommandingConfigurator commandingConfigurator)
            {
                commandRegistry = commandingConfigurator.AddCommanding(adapter);
            }
            else
            {
                CommandingRuntime commandingRuntime = new CommandingRuntime();
                commandRegistry = commandingRuntime.AddCommanding(adapter);
            }

            // Register internal implementations
            RegisterInternalImplementations();

            FunctionHostBuilder       builder = null;
            IFunctionCompilerMetadata functionCompilerMetadata = null;
            CompileTargetEnum         compileTarget;

            if (configuration != null)
            {
                // Invoke the builder process
                builder = CreateBuilderFromConfiguration(commandRegistry, configuration);
                if (appHostBuilder != null)
                {
                    builder.Options = appHostBuilder.Options;
                }
                FunctionBuilder functionBuilder = (FunctionBuilder)builder.FunctionBuilder;
                FunctionDefinitions = builder.FunctionDefinitions;
                compileTarget       = builder.Options.HttpTarget;
                SetupAuthorization(builder, functionBuilder);
            }
            else
            {
                functionCompilerMetadata = LocateFunctionCompilerMetadata(functionAppConfigurationAssembly);
                FunctionDefinitions      = functionCompilerMetadata.FunctionDefinitions;
                compileTarget            = functionCompilerMetadata.CompilerOptions.HttpTarget;
            }

            PostBuildPatcher.EnsureFunctionsHaveUniqueNames(FunctionDefinitions);

            RegisterCoreDependencies(builder?.MediatorType ?? typeof(DefaultMediatorDecorator), FunctionDefinitions, compileTarget);

            RegisterTimerCommandFactories(FunctionDefinitions);

            RegisterHttpDependencies(FunctionDefinitions);

            RegisterCosmosDependencies(FunctionDefinitions);

            RegisterOutputBindingDependencies(FunctionDefinitions);


            CreatePluginFunctions(functionCompilerMetadata?.ClaimsMappings, FunctionDefinitions);

            RegisterLoggerIfRequired();
        }