/// <summary>
        /// Adds --help option, if there isn't already a help flag set.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="template">The help template. Defaults to <c>--help</c>.</param>
        /// <returns>The builder.</returns>
        public static IConventionBuilder UseDefaultHelpOption(this IConventionBuilder builder, string template = DefaultHelpOptionConvention.DefaultHelpTemplate)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddConvention(new DefaultHelpOptionConvention(template)));
        }
 /// <summary>
 /// Provides the same conventions as using <see cref="McMaster.Extensions.CommandLineUtils.ConventionBuilderExtensions.UseDefaultConventions"/>,
 /// but with an additional external service provider.
 /// </summary>
 /// <param name="builder">The convention builder.</param>
 /// <param name="additionalServices">Additional services that should be passed to the service provider.</param>
 public static IConventionBuilder UseDefaultConventionsWithServices(this IConventionBuilder builder, IServiceProvider additionalServices) =>
 builder.UseAttributes()
 .SetAppNameFromEntryAssembly()
 .SetRemainingArgsPropertyOnModel()
 .SetSubcommandPropertyOnModel()
 .SetParentPropertyOnModel()
 .UseOnExecuteMethodFromModel()
 .UseOnValidateMethodFromModel()
 .UseOnValidationErrorMethodFromModel()
 .UseConstructorInjection(additionalServices);
Пример #3
0
        /// <summary>
        /// Applies a collection of default conventions, such as applying options in attributes
        /// on the model type,
        /// </summary>
        /// <returns>The builder.</returns>
        public static IConventionBuilder UseAttributes(this IConventionBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder
                   .UseCommandAttribute()
                   .UseVersionOptionFromMemberAttribute()
                   .UseVersionOptionAttribute()
                   .UseHelpOptionAttribute()
                   .UseOptionAttributes()
                   .UseArgumentAttributes()
                   .UseSubcommandAttributes());
        }
Пример #4
0
        /// <summary>
        /// Applies a collection of default conventions, such as applying options in attributes
        /// on the model type,
        /// </summary>
        /// <returns>The builder.</returns>
        public static IConventionBuilder UseDefaultConventions(this IConventionBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder
                   .UseAttributes()
                   .SetAppNameFromEntryAssembly()
                   .SetRemainingArgsPropertyOnModel()
                   .SetSubcommandPropertyOnModel()
                   .SetParentPropertyOnModel()
                   .UseOnExecuteMethodFromModel()
                   .UseOnValidationErrorMethodFromModel());
        }
        private static ISimpleInjectorContainerAdapter GetContainerAdapter(IConventionBuilder<Lifestyle> builder)
        {
            var container = builder.ContainerAdapter as ISimpleInjectorContainerAdapter;
            if (container == null)
            {
                var decorator = builder.ContainerAdapter as IDecorator;
                if (decorator == null)
                {
                    throw new Exception("ConventionBuilder with SimpleInjectorContainerAdapter was expected");
                }
                container = decorator.Decoratee as ISimpleInjectorContainerAdapter;
                if (container == null)
                {
                    throw new Exception("Not a compatible convention builder");
                    //todo: replace with appropriate exception
                }
            }

            return container;
        }
 /// <summary>
 /// Invokes a method named "OnValidationError" on the model type when validation fails.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder UseOnValidationErrorMethodFromModel(this IConventionBuilder builder)
 => builder.AddConvention(new ValidationErrorMethodConvention());
 /// <summary>
 /// Adds subcommands for each <see cref="SubcommandAttribute" /> on the model type.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder UseSubcommandAttributes(this IConventionBuilder builder)
 => builder.AddConvention(new SubcommandAttributeConvention());
 /// <summary>
 /// Applies settings from <see cref="ArgumentAttribute" /> on the model type.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder UseArgumentAttributes(this IConventionBuilder builder)
 => builder.AddConvention(new ArgumentAttributeConvention());
 /// <summary>
 /// Applies settings from <see cref="OptionAttribute" /> on the model type.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder UseOptionAttributes(this IConventionBuilder builder)
 => builder.AddConvention(new OptionAttributeConvention());
 /// <summary>
 /// Applies settings from <see cref="VersionOptionAttribute" /> on the model type.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder UseVersionOptionAttribute(this IConventionBuilder builder)
 => builder.AddConvention(new VersionOptionAttributeConvention());
 public static IConventionBuilder UsePagerForHelpText(this IConventionBuilder builder, bool usePager = true)
 {
     builder.AddConvention(new UsePagerForHelpTextConvention(usePager));
     return(builder);
 }
 /// <summary>
 /// Sets a property named "RemainingArgs" or "RemainingArguments" on the model type to the value
 /// of <see cref="CommandLineApplication.RemainingArguments" />.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder SetRemainingArgsPropertyOnModel(this IConventionBuilder builder)
 => builder.AddConvention(new RemainingArgsPropertyConvention());
 /// <summary>
 /// Enables using constructor injection to initialize the model type.
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="additionalServices">Additional services that should be passed to the service provider.</param>
 public static IConventionBuilder UseConstructorInjection(this IConventionBuilder builder, IServiceProvider additionalServices)
 => builder.AddConvention(new ConstructorInjectionConvention(additionalServices));
 /// <summary>
 /// Sets a method named "OnExecute" or "OnExecuteAsync" on the model type to handle
 /// <see cref="CommandLineApplication.Invoke" />
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder UseOnExecuteMethodFromModel(this IConventionBuilder builder)
 => builder.AddConvention(new ExecuteMethodConvention());
 /// <summary>
 /// Enables setting properties on a command model from <see cref="CommandOption"/>s.
 /// </summary>
 /// <param name="builder">The convention builder.</param>
 /// <param name="propertySuffix">The naming suffix for candidate properties that should have option values mapped to them.</param>
 /// <param name="nestedPropertySuffix">The naming suffix to use for properties of a complex type (non-primitive) that option values should be mapped to.</param>
 public static IConventionBuilder UseOptionProperties(this IConventionBuilder builder, string propertySuffix = "", string nestedPropertySuffix = "Options") =>
 builder.AddConvention(new OptionPropertiesConvention
 {
     NestedPropertySuffix = nestedPropertySuffix,
     PropertySuffix       = propertySuffix
 });
Пример #16
0
 public ConventionProvider(IConventionBuilder builder)
 {
     _builder     = builder;
     _conventions = new ConcurrentDictionary <Type, IConvention>();
 }
Пример #17
0
        public static IHoneyCombBuilder AddRabbitMQ(this IHoneyCombBuilder builder, RabbitMqOptions options, IJsonSerializer jsonSerializer = null,
                                                    IConventionBuilder conventionBuilder = null, IRabbitQueuePrefixProvider rabbitQueueIdentifierProvider = null, int retriesCountOnConnectingFailed = -1)
        {
            var jsonSerializerIsRegistered = builder.Services.Any(x => x.ServiceType == typeof(IJsonSerializer));

            if (!jsonSerializerIsRegistered)
            {
                if (jsonSerializer is null)
                {
                    var factory = new Open.Serialization.Json.Newtonsoft.JsonSerializerFactory(new JsonSerializerSettings
                    {
                        ContractResolver  = new CamelCasePropertyNamesContractResolver(),
                        NullValueHandling = NullValueHandling.Ignore
                    });
                    jsonSerializer = factory.GetSerializer();
                }
                builder.Services.AddSingleton(jsonSerializer);
            }

            var queueIdentIsRegistered = builder.Services.Any(x => x.ServiceType == typeof(IRabbitQueuePrefixProvider));

            if (!queueIdentIsRegistered)
            {
                if (rabbitQueueIdentifierProvider is null)
                {
                    builder.Services.AddSingleton <IRabbitQueuePrefixProvider, RabbitQueueIdentifierProvider>();
                }
                else
                {
                    builder.Services.AddSingleton(rabbitQueueIdentifierProvider);
                }
            }

            builder.Services.AddSingleton(options);
            builder.Services.AddSingleton <IConventionBuilder, UnderscoreCaseConventionBuilder>();

            if (conventionBuilder is null)
            {
                builder.Services.AddSingleton <IConventionProvider, ConventionProvider>();
            }
            else
            {
                builder.Services.AddSingleton(conventionBuilder);
            }

            builder.Services.AddSingleton <IRabbitMqClient, RabbitMqClient>();
            builder.Services.AddSingleton <IBusPublisher, RabbitMqPublisher>();
            builder.Services.AddSingleton <IBusSubscriber, RabbitMqSubscriber>();

            builder.Services.AddTransient <IInitializer, RabbitMqExchangeInitializer>();

            var connectionFactory = new ConnectionFactory
            {
                Port        = options.Port,
                VirtualHost = options.VirtualHost,
                UserName    = options.Username,
                Password    = options.Password,
                RequestedConnectionTimeout = TimeSpan.FromMilliseconds(options.RequestedConnectionTimeout), //ms
                SocketReadTimeout          = TimeSpan.FromMilliseconds(options.SocketReadTimeout),          //ms
                SocketWriteTimeout         = TimeSpan.FromMilliseconds(options.SocketWriteTimeout),         //ms
                RequestedChannelMax        = options.RequestedChannelMax,
                RequestedFrameMax          = options.RequestedFrameMax,
                RequestedHeartbeat         = TimeSpan.FromSeconds(options.RequestedHeartbeat), //sec
                UseBackgroundThreadsForIO  = options.UseBackgroundThreadsForIO,
                DispatchConsumersAsync     = true,
                Ssl = options.Ssl is null
                    ? new SslOption()
                    : new SslOption(options.Ssl.ServerName, options.Ssl.CertificatePath, options.Ssl.Enabled)
            };


            builder.Services.AddSingleton(connectionFactory);
            builder.Services.AddSingleton <IConnectionFactory>(new ConnectionWithRetryFactory(connectionFactory, options, builder));


            //var policyBuilder = Policy.Handle<Exception>();
            //RetryPolicy retryPolicy = null;

            //if (retriesCountOnConnectingFailed < 0)
            //    retryPolicy = policyBuilder.WaitAndRetryForever(r => TimeSpan.FromSeconds(3 * r), OnConnectionException);
            //else
            //    retryPolicy = policyBuilder.WaitAndRetry(retriesCountOnConnectingFailed, r => TimeSpan.FromSeconds(3 * r), OnConnectionException);

            //var connection = retryPolicy.ExecuteAndCapture(() => connectionFactory.CreateConnection(options.HostNames.ToList(), options.ConnectionName));

            //void OnConnectionException(Exception ex, TimeSpan ts)
            //{
            //    var logger = builder.Services.BuildServiceProvider().GetRequiredService<ILogger<IConnection>>();
            //    logger.LogError(ex, $"Error while connecting to RabbitMq. {ex.Message}");
            //}

            //builder.Services.AddSingleton(connection);


            return(builder);
        }
 /// <summary>
 /// Enables using constructor injection to initialize the model type.
 /// </summary>
 /// <param name="builder"></param>
 public static IConventionBuilder UseConstructorInjection(this IConventionBuilder builder)
 => builder.AddConvention(new ConstructorInjectionConvention());
Пример #19
0
 /// <summary>
 /// Sets the subcommand name using the model type, if available and not otherwise set using <see cref="CommandAttribute"/>.
 /// </summary>
 /// <param name="builder"></param>
 /// <returns></returns>
 public static IConventionBuilder UseCommandNameFromModelType(this IConventionBuilder builder)
 => builder.AddConvention(new CommandNameFromTypeConvention());
 /// <summary>
 /// Sets a property named "Parent" on the model type to the value
 /// of the model of the parent command.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder SetParentPropertyOnModel(this IConventionBuilder builder)
 => builder.AddConvention(new ParentPropertyConvention());
 /// <summary>
 /// Sets <see cref="CommandLineApplication.Name" /> to match the name of
 /// <see cref="System.Reflection.Assembly.GetEntryAssembly" />
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder SetAppNameFromEntryAssembly(this IConventionBuilder builder)
 => builder.AddConvention(new AppNameFromEntryAssemblyConvention());
 /// <summary>
 /// Sets a property named "Subcommand" on the model type to the value
 /// of the model of the selected subcommand.
 /// </summary>
 /// <param name="builder">The builder.</param>
 /// <returns>The builder.</returns>
 public static IConventionBuilder SetSubcommandPropertyOnModel(this IConventionBuilder builder)
 => builder.AddConvention(new SubcommandPropertyConvention());
 public static IConventionBuilder MakeSuggestionsInErrorMessage(this IConventionBuilder builder,
                                                                bool makeSuggestions = true)
 {
     builder.AddConvention(new MakeSuggestionsInErrorMessageConvention(makeSuggestions));
     return(builder);
 }