public static IServiceCollection AddSimpleCommandsFromRepository(
            this IServiceCollection services,
            IRepository repository)
        {
            List <SimpleCommand> simpleCommands = repository.List(CommandPolicy.All());

            foreach (SimpleCommand simpleCommand in simpleCommands)
            {
                services.AddSingleton <IBotCommand>(simpleCommand);
            }

            return(services);
        }
예제 #2
0
        public static ContainerBuilder AddSimpleCommandsFromRepository(
            this ContainerBuilder builder,
            IRepository repository)
        {
            List <SimpleCommand> simpleCommands = repository.List(CommandPolicy.All());

            foreach (SimpleCommand simpleCommand in simpleCommands)
            {
                builder.RegisterInstance(simpleCommand).As <IBotCommand>();
            }

            return(builder);
        }
예제 #3
0
        private static (List <CommandEntity> WordsToAdd, List <CommandEntity> WordsToRemove) GetCommandWordChanges(IRepository repository)
        {
            const string conventionSuffix = "Command";

            // Access the assembly to make sure it's loaded
            Assembly assembly = typeof(BlastCommand).Assembly;

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            IEnumerable <TypeInfo> allCommandTypes =
                assemblies.SelectMany(x => x.DefinedTypes);

            DefaultCommandData commandData = GetDefaultData();

            var concreteCommands = allCommandTypes
                                   .Where(x => typeof(IBotCommand).IsAssignableFrom(x))
                                   .Where(x => !x.IsAbstract)
                                   .Where(x => !x.IsSubclassOf(typeof(DataEntity)))
                                   .Where(x => x.FullName.EndsWith(conventionSuffix))
                                   .ToList();

            List <CommandEntity> commandEntities = repository.List(CommandPolicy.All());
            List <string>        commandTypes    = commandEntities.Select(x => x.FullTypeName).ToList();

            CommandEntity CommandEntityFromTypeAndDefaultData(TypeInfo commandType)
            {
                var entity = commandData.Commands.SingleOrDefault(x => x.FullTypeName == commandType.FullName)
                             ?? new CommandEntity
                {
                    FullTypeName = commandType.FullName,
                };

                entity.CommandWord = commandType.Name.Substring(0, commandType.Name.Length - conventionSuffix.Length);
                return(entity);
            }

            List <CommandEntity> missingDefaults = concreteCommands
                                                   .Where(x => !commandTypes.Contains(x.FullName))
                                                   .Select(CommandEntityFromTypeAndDefaultData)
                                                   .ToList();

            var entitiesToRemove = commandEntities.Where(x => concreteCommands.All(c => c.FullName != x.FullTypeName)).ToList();

            return(missingDefaults, entitiesToRemove);
        }