Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TProfile"></typeparam>
        /// <param name="validate"></param>
        public void AddProfile <TProfile>(bool validate = false)
            where TProfile : Profile, new()
        {
            Configurators.Add(context => context.MapperConfiguration.AddProfile <TProfile>());

            if (validate)
            {
                ValidateProfile(typeof(TProfile));
            }
        }
Beispiel #2
0
        public void AddProfiles(Assembly assembly)
        {
            var profileTypes = assembly
                               .GetTypes()
                               .Where(type => typeof(Profile).IsAssignableFrom(type) && !type.IsAbstract && !type.IsGenericType);

            foreach (var profileType in profileTypes)
            {
                Configurators.Add(mapper => mapper.AddProfile(profileType));
            }
        }
Beispiel #3
0
        /// <summary>
        /// 添加模块
        /// </summary>
        /// <typeparam name="TModule"></typeparam>
        /// <param name="func">委托返回所有的枚举项值</param>
        /// <param name="func2">委托返回所有的枚举类型</param>
        public void Add <TModule>(Func <Assembly, Dictionary <string, Dictionary <int, string> > > func,
                                  Func <Assembly, Dictionary <string, string> > func2)
        {
            var assembly = typeof(TModule).Assembly;

            Configurators.Add(context =>
            {
                Dictionary <string, Dictionary <int, string> > dic = new Dictionary <string, Dictionary <int, string> >();
                Dictionary <string, string> dicType = new Dictionary <string, string>();
                if (func != null)
                {
                    dic = func(assembly);
                    if (func2 == null)
                    {
                        throw new Exception("返回枚举类型的委托不能为空");
                    }
                    dicType = func2(assembly);
                }
                else
                {
                    var types = assembly.GetTypes().Where(p => p.IsEnum);
                    foreach (var enumType in types)
                    {
                        FieldInfo[] fields = enumType.GetFields(BindingFlags.Static | BindingFlags.Public);
                        var rs             = new Dictionary <int, string>();
                        foreach (var item in fields)
                        {
                            var fieldValue       = item.GetRawConstantValue();
                            var fieldDescription = item.GetDescription();
                            rs.Add((int)fieldValue, fieldDescription);
                        }

                        dic.Add(enumType.Name, rs);
                        dicType.Add(enumType.Name, enumType.GetDescription());
                    }
                }
                foreach (var item in dic)
                {
                    if (!context.Enums.ContainsKey(item.Key))
                    {
                        context.Enums.Add(item.Key, item.Value);
                    }
                }
                foreach (var item in dicType)
                {
                    if (!context.EnumTypes.ContainsKey(item.Key))
                    {
                        context.EnumTypes.Add(item.Key, item.Value);
                    }
                }
            });
        }
Beispiel #4
0
        public void AddProfile <TProfile>(bool validate = false)
            where TProfile : Profile, new()
        {
            Configurators.Add(context =>
            {
                context.MapperConfiguration.AddProfile <TProfile>();
            });

            if (validate)
            {
                ValidatingProfiles.Add <TProfile>();
            }
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TModule"></typeparam>
        /// <param name="validate"></param>
        public void AddMaps <TModule>(bool validate = false)
        {
            var assembly = typeof(TModule).Assembly;

            Configurators.Add(context => context.MapperConfiguration.AddMaps(assembly));

            if (validate)
            {
                var profileTypes = assembly
                                   .DefinedTypes
                                   .Where(type => typeof(Profile).IsAssignableFrom(type) && !type.IsAbstract && !type.IsGenericType);

                foreach (var profileType in profileTypes)
                {
                    ValidatingProfiles.Add(profileType);
                }
            }
        }
Beispiel #6
0
        /// <inheritdoc/>
        public async Task <int> RunAsync()
        {
            // Remove the synchronization context
            await default(SynchronizationContextRemover);

            // Populate the class catalog (if we haven't already)
            _classCatalog.Populate();

            // Run bootstrapper configurators first
            Configurators.Configure <IConfigurableBootstrapper>(this);
            Configurators.Configure <IBootstrapper>(this);

            // Create the service collection
            IServiceCollection serviceCollection = CreateServiceCollection() ?? new ServiceCollection();

            serviceCollection.TryAddSingleton <IConfigurableBootstrapper>(this);
            serviceCollection.TryAddSingleton <IBootstrapper>(this);
            serviceCollection.TryAddSingleton(_classCatalog);  // The class catalog is retrieved later for deferred logging once a service provider is built

            // Run configurators on the service collection
            ConfigurableServices configurableServices = new ConfigurableServices(serviceCollection);

            Configurators.Configure(configurableServices);

            // Add simple logging to make sure it's available in commands before the engine adds in
            serviceCollection.AddLogging();

            // Create the stand-alone command line service container and register a few types needed for the CLI
            CommandServiceTypeRegistrar registrar = new CommandServiceTypeRegistrar();

            registrar.RegisterInstance(typeof(IServiceCollection), serviceCollection);
            registrar.RegisterInstance(typeof(IConfiguratorCollection), Configurators);

            // Create the command line parser and run the command
            ICommandApp app = _getCommandApp(registrar);

            app.Configure(x =>
            {
                x.ValidateExamples();
                ConfigurableCommands configurableCommands = new ConfigurableCommands(x);
                Configurators.Configure(configurableCommands);
            });
            return(await app.RunAsync(Args));
        }
Beispiel #7
0
        public sealed override async Task <int> ExecuteAsync(CommandContext context, TSettings commandSettings)
        {
            // Set verbose tracing
            if (commandSettings.LogLevel != LogLevel.Information)
            {
                ServiceCollection.Configure <LoggerFilterOptions>(options => options.MinLevel = commandSettings.LogLevel);
            }

            // File logging
            if (!string.IsNullOrEmpty(commandSettings.LogFile))
            {
                // Add the log provider (adding it to the service collection will get picked up by the logger factory)
                ServiceCollection.AddSingleton <ILoggerProvider, FileLoggerProvider>();
                ServiceCollection.Configure <FileLoggerOptions>(options =>
                {
                    options.FileName     = commandSettings.LogFile;
                    options.LogDirectory = string.Empty;
                });
            }

            // Build a temporary service provider so we can log
            // Make sure to place it in it's own scope so transient services get correctly disposed
            IServiceProvider services     = ServiceCollection.BuildServiceProvider();
            ClassCatalog     classCatalog = services.GetService <ClassCatalog>();

            using (IServiceScope serviceScope = services.CreateScope())
            {
                // Log pending messages
                ILogger logger = serviceScope.ServiceProvider.GetRequiredService <ILogger <Bootstrapper> >();
                logger.LogInformation($"Statiq version {Engine.Version}");
                classCatalog?.LogDebugMessages(logger);

                // Attach
                if (commandSettings.Attach)
                {
                    logger.LogInformation($"Waiting for a debugger to attach to process {Process.GetCurrentProcess().Id} (or press a key to continue)...");
                    while (!Debugger.IsAttached && !Console.KeyAvailable)
                    {
                        Thread.Sleep(100);
                    }
                    if (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                        logger.LogInformation("Key pressed, continuing execution");
                    }
                    else
                    {
                        logger.LogInformation("Debugger attached, continuing execution");
                    }
                }
            }

            // Add settings
            if (commandSettings.Settings?.Length > 0)
            {
                foreach (KeyValuePair <string, string> setting in SettingsParser.Parse(commandSettings.Settings))
                {
                    ConfigurationSettings[setting.Key] = setting.Value;
                }
            }

            // Configure settings after other configuration so they can use the values
            ConfigurableSettings configurableSettings = new ConfigurableSettings(ConfigurationSettings);

            Configurators.Configure(configurableSettings);

            return(await ExecuteCommandAsync(context, commandSettings));
        }
Beispiel #8
0
        /// <inheritdoc/>
        public async Task <int> RunAsync()
        {
            // Remove the synchronization context
            await default(SynchronizationContextRemover);

            // Populate the class catalog (if we haven't already)
            _classCatalog.Populate();

            // Run bootstrapper configurators first
            Configurators.Configure <IConfigurableBootstrapper>(this);
            Configurators.Configure <IBootstrapper>(this);

            // Run the configuration configurator and get the configuration root
            SettingsConfigurationProvider settingsProvider          = new SettingsConfigurationProvider();
            IConfigurationBuilder         configurationBuilder      = new ConfigurationBuilder();
            ConfigurableConfiguration     configurableConfiguration = new ConfigurableConfiguration(configurationBuilder);

            Configurators.Configure(configurableConfiguration);
            configurationBuilder.Add(settingsProvider);
            IConfigurationRoot    configurationRoot     = configurationBuilder.Build();
            ConfigurationSettings configurationSettings = new ConfigurationSettings(settingsProvider, configurationRoot);

            // Create the service collection
            IServiceCollection serviceCollection = CreateServiceCollection() ?? new ServiceCollection();

            serviceCollection.TryAddSingleton <IConfigurableBootstrapper>(this);
            serviceCollection.TryAddSingleton <IBootstrapper>(this);
            serviceCollection.TryAddSingleton(_classCatalog);  // The class catalog is retrieved later for deferred logging once a service provider is built
            serviceCollection.TryAddSingleton <IConfiguration>(configurationRoot);

            // Run configurators on the service collection
            ConfigurableServices configurableServices = new ConfigurableServices(serviceCollection, configurationRoot);

            Configurators.Configure(configurableServices);

            // Add simple logging to make sure it's available in commands before the engine adds in,
            // but add it after the configurators have a chance to configure logging
            serviceCollection.AddLogging();

            // Create the stand-alone command line service container and register a few types needed for the CLI
            CommandServiceTypeRegistrar registrar = new CommandServiceTypeRegistrar();

            registrar.RegisterInstance(typeof(IConfigurationSettings), configurationSettings);
            registrar.RegisterInstance(typeof(IConfigurationRoot), configurationRoot);
            registrar.RegisterInstance(typeof(IServiceCollection), serviceCollection);
            registrar.RegisterInstance(typeof(IConfiguratorCollection), Configurators);
            registrar.RegisterInstance(typeof(IBootstrapper), this);

            // Create the command line parser and run the command
            ICommandApp app = _getCommandApp(registrar);

            app.Configure(commandConfigurator =>
            {
                commandConfigurator.ValidateExamples();
                ConfigurableCommands configurableCommands = new ConfigurableCommands(commandConfigurator);
                Configurators.Configure(configurableCommands);
            });
            int exitCode = await app.RunAsync(Arguments);

            // Dispose all instances of the console logger to flush the message queue and stop the listening thread
            ConsoleLoggerProvider.DisposeAll();

            return(exitCode);
        }
Beispiel #9
0
 public void AddConfigurator(Action <IMapperConfigurationExpression> action)
 {
     Configurators.Add(action);
 }