예제 #1
0
        public ExecuteCommandSet(IBasicActivateItems activator,

                                 [DemandsInitialization("A single object on which you want to change a given property")]
                                 IMapsDirectlyToDatabaseTable setOn,
                                 [DemandsInitialization("Name of a property you want to change e.g. Description")]
                                 string property,
                                 [DemandsInitialization("New value to assign, this will be parsed into a valid Type if property is not a string")]
                                 string value) : base(activator)
        {
            _setOn = setOn;

            _property = _setOn.GetType().GetProperty(property);

            if (_property == null)
            {
                SetImpossible($"Unknown Property '{property}'");
            }
            else
            {
                var picker = new CommandLineObjectPicker(new string[] { value ?? "NULL" }, activator.RepositoryLocator);

                if (!picker.HasArgumentOfType(0, _property.PropertyType))
                {
                    SetImpossible($"Provided value could not be converted to '{_property.PropertyType}'");
                }
                else
                {
                    NewValue = picker[0].GetValueForParameterOfType(_property.PropertyType);
                }
            }
        }
예제 #2
0
        public int Run(IRDMPPlatformRepositoryServiceLocator repositoryLocator, IDataLoadEventListener listener,
                       ICheckNotifier checkNotifier, GracefulCancellationToken token)
        {
            _input    = new ConsoleInputManager(repositoryLocator, checkNotifier);
            _listener = listener;
            _invoker  = new CommandInvoker(_input);
            _invoker.CommandImpossible += (s, c) => Console.WriteLine($"Command Impossible:{c.Command.ReasonCommandImpossible}");
            _invoker.CommandCompleted  += (s, c) => Console.WriteLine("Command Completed");

            _commands = _invoker.GetSupportedCommands().ToDictionary(
                k => BasicCommandExecution.GetCommandName(k.Name),
                v => v, StringComparer.CurrentCultureIgnoreCase);

            _picker =
                _options.CommandArgs != null && _options.CommandArgs.Any() ?
                new CommandLineObjectPicker(_options.CommandArgs, repositoryLocator) :
                null;

            if (string.IsNullOrWhiteSpace(_options.CommandName))
            {
                RunCommandExecutionLoop(repositoryLocator);
            }
            else
            {
                RunCommand(_options.CommandName);
            }

            return(0);
        }
예제 #3
0
        /// <summary>
        /// Runs a main loop in which the user types many commands one after the other
        /// </summary>
        /// <param name="repositoryLocator"></param>
        private void RunCommandExecutionLoop(IRDMPPlatformRepositoryServiceLocator repositoryLocator)
        {
            //when running a command loop don't use command line arguments (shouldn't really be possible anyway)
            _picker = null;

            while (true)
            {
                Console.WriteLine("Enter Command (or 'exit')");
                var command = _input.GetString("Command", _commands.Keys.ToList());

                if (command.Contains(' '))
                {
                    _picker = new CommandLineObjectPicker(SplitCommandLine(command).Skip(1).ToArray(), repositoryLocator);
                    command = command.Substring(0, command.IndexOf(' '));
                }

                if (string.Equals(command, "exit", StringComparison.CurrentCultureIgnoreCase))
                {
                    break;
                }

                RunCommand(command);

                _picker = null;
            }
        }
예제 #4
0
        /// <summary>
        /// Runs a main loop in which the user types many commands one after the other
        /// </summary>
        /// <param name="repositoryLocator"></param>
        private void RunCommandExecutionLoop(IRDMPPlatformRepositoryServiceLocator repositoryLocator)
        {
            //when running a command loop don't use command line arguments (shouldn't really be possible anyway)
            _picker = null;

            while (true)
            {
                Console.WriteLine("Enter Command (or 'exit')");
                var command = _input.GetString(new DialogArgs {
                    WindowTitle = "Command"
                }, _commands.Keys.ToList());
                try
                {
                    command = GetCommandAndPickerFromLine(command, out _picker, repositoryLocator);

                    if (string.Equals(command, "exit", StringComparison.CurrentCultureIgnoreCase))
                    {
                        break;
                    }

                    RunCommand(command);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                _picker = null;
            }
        }
예제 #5
0
        public void TestSetArgument_CatalogueArrayOf2_Valid()
        {
            var cata1 = WhenIHaveA <Catalogue>();

            cata1.Name = "kapow bob";
            cata1.SaveToDatabase();

            var cata2 = WhenIHaveA <Catalogue>();

            cata2.Name = "kapow frank";
            cata2.SaveToDatabase();

            //Lets also test that PipelineComponentArgument also work (not just ProcessTaskArgument)
            var pca = WhenIHaveA <PipelineComponentArgument>();
            var pc  = pca.PipelineComponent;

            pca.Name = "ggg";
            pca.SetType(typeof(Catalogue[]));

            Assert.IsNull(pca.Value);

            var picker = new CommandLineObjectPicker(new [] { $"PipelineComponent:{pc.ID}", "ggg", $"Catalogue:kapow*" }, RepositoryLocator);

            Assert.DoesNotThrow(() => GetInvoker().ExecuteCommand(typeof(ExecuteCommandSetArgument), picker));

            Assert.Contains(cata1, (System.Collections.ICollection)pca.GetValueAsSystemType());
            Assert.Contains(cata2, (System.Collections.ICollection)pca.GetValueAsSystemType());
        }
예제 #6
0
        /// <summary>
        /// Returns the first best constructor on the <paramref name="type"/> preferring those decorated with <see cref="UseWithCommandLineAttribute"/>
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public virtual ConstructorInfo GetConstructor(Type type, CommandLineObjectPicker picker)
        {
            var constructors = type.GetConstructors();

            if (constructors.Length == 0)
            {
                return(null);
            }

            ConstructorInfo[] importDecorated = null;

            //If we have a picker, look for a constructor that wants to run from the command line
            if (picker != null)
            {
                importDecorated = constructors.Where(c => Attribute.IsDefined(c, typeof(UseWithCommandLineAttribute))).ToArray();
            }

            //otherwise look for a regular decorated constructor
            if (importDecorated == null || !importDecorated.Any())
            {
                importDecorated = constructors.Where(c => Attribute.IsDefined(c, typeof(UseWithObjectConstructorAttribute))).ToArray();
            }

            if (importDecorated.Any())
            {
                return(importDecorated[0]);
            }

            return(constructors[0]);
        }
예제 #7
0
        public void TestPicker_TypeYieldsEmptyArrayOfObjects()
        {
            foreach (var cat in RepositoryLocator.CatalogueRepository.GetAllObjects <Catalogue>())
            {
                cat.DeleteInDatabase();
            }

            Assert.IsEmpty(RepositoryLocator.CatalogueRepository.GetAllObjects <Catalogue>());

            //when interpreting the string "Catalogue" for a command
            var picker = new CommandLineObjectPicker(new [] { "Catalogue" }, GetActivator());

            //we can pick it as either a Catalogue or a collection of all the Catalogues
            Assert.AreEqual(typeof(Catalogue), picker.Arguments.Single().Type);
            Assert.IsEmpty(picker.Arguments.Single().DatabaseEntities);

            //when interpretting as a Type we get Catalogue
            Assert.IsTrue(picker.Arguments.First().HasValueOfType(typeof(Type)));
            Assert.AreEqual(typeof(Catalogue), picker.Arguments.Single().GetValueForParameterOfType(typeof(Type)));

            //if it is looking for an ienumerable of objects
            Assert.IsTrue(picker.Arguments.First().HasValueOfType(typeof(IMapsDirectlyToDatabaseTable[])));
            Assert.IsEmpty((IMapsDirectlyToDatabaseTable[])picker.Arguments.First().GetValueForParameterOfType(typeof(IMapsDirectlyToDatabaseTable[])));

            Assert.IsTrue(picker.Arguments.First().HasValueOfType(typeof(Catalogue[])));
            Assert.IsEmpty(((Catalogue[])picker.Arguments.First().GetValueForParameterOfType(typeof(Catalogue[]))).ToArray());
        }
예제 #8
0
        public void PickTypeName()
        {
            var picker = new CommandLineObjectPicker(new [] { "Name" }, GetActivator());

            Assert.IsNull(picker[0].Type);
            Assert.AreEqual("Name", picker[0].RawValue);
        }
예제 #9
0
        public void PickTypeName()
        {
            var picker = new CommandLineObjectPicker(new [] { "Name" }, RepositoryLocator);

            Assert.IsNull(picker[0].Type);
            Assert.AreEqual("Name", picker[0].RawValue);
        }
        public void TestSetArgument_CatalogueArray_SetToNull_Valid()
        {
            var cata1 = WhenIHaveA <Catalogue>();

            cata1.Name = "lolzzzyy";
            cata1.SaveToDatabase();


            //Lets also test that PipelineComponentArgument also work (not just ProcessTaskArgument)
            var pca = WhenIHaveA <PipelineComponentArgument>();
            var pc  = pca.PipelineComponent;

            pca.Name = "ggg";
            pca.SetType(typeof(Catalogue[]));
            pca.SetValue(new Catalogue[] { cata1 });
            pca.SaveToDatabase();

            Assert.Contains(cata1, (System.Collections.ICollection)pca.GetValueAsSystemType());

            var picker = new CommandLineObjectPicker(new [] { $"PipelineComponent:{pc.ID}", "ggg", $"Null" }, GetActivator());

            Assert.DoesNotThrow(() => GetInvoker().ExecuteCommand(typeof(ExecuteCommandSetArgument), picker));

            Assert.IsNull(pca.GetValueAsSystemType());
        }
예제 #11
0
        public ExecuteCommandSetArgument(IBasicActivateItems activator, CommandLineObjectPicker picker) : base(activator)
        {
            if (picker.Arguments.Count != 3)
            {
                SetImpossible($"Wrong number of parameters supplied to command, expected 3 but got {picker.Arguments.Count}");
                return;
            }

            if (!picker.HasArgumentOfType(0, typeof(IMapsDirectlyToDatabaseTable)))
            {
                SetImpossible("First parameter must be an IArgumentHost DatabaseEntity");
                return;
            }

            _host = picker[0].GetValueForParameterOfType(typeof(IMapsDirectlyToDatabaseTable)) as IArgumentHost;

            if (_host == null)
            {
                SetImpossible("First parameter must be an IArgumentHost");
                return;
            }

            var args = _host.GetAllArguments();

            _arg = args.FirstOrDefault(a => a.Name.Equals(picker[1].RawValue));

            if (_arg == null)
            {
                SetImpossible($"Could not find argument called '{picker[1].RawValue}' on '{_host}'.  Arguments found were {string.Join(",",args.Select(a=>a.Name))}");
                return;
            }

            Type argType;

            try
            {
                argType = _arg.GetConcreteSystemType();
            }
            catch (Exception e)
            {
                SetImpossible("Failed to get system Type of argument:" + e);
                return;
            }

            if (argType == null)
            {
                SetImpossible($"Argument '{_arg.Name}' has no listed Type");
                return;
            }


            if (!picker[2].HasValueOfType(argType))
            {
                SetImpossible($"Provided value '{picker[2].RawValue}' does not match expected Type '{argType.Name}' of argument '{_arg.Name}'");
                return;
            }

            _value = picker[2].GetValueForParameterOfType(argType);
        }
        public void TestSetArgument_WrongArgCount()
        {
            var picker = new CommandLineObjectPicker(new [] { "yyy" }, GetActivator());
            var cmd    = new ExecuteCommandSetArgument(GetMockActivator().Object, picker);

            Assert.IsTrue(cmd.IsImpossible);
            Assert.AreEqual("Wrong number of parameters supplied to command, expected 3 but got 1", cmd.ReasonCommandImpossible);
        }
        public void TestSetArgument_NotAHost()
        {
            var c = WhenIHaveA <Catalogue>();

            var picker = new CommandLineObjectPicker(new [] { $"Catalogue:{c.ID}", "fff", "yyy" }, GetActivator());
            var cmd    = new ExecuteCommandSetArgument(GetMockActivator().Object, picker);

            Assert.IsTrue(cmd.IsImpossible);
            Assert.AreEqual("First parameter must be an IArgumentHost", cmd.ReasonCommandImpossible);
        }
예제 #14
0
        public void Test_NewObjectCommand_WrongTypeArgument()
        {
            var picker = new CommandLineObjectPicker(new[] { "UnitTests" }, GetActivator());

            Assert.AreEqual(typeof(UnitTests), picker[0].Type);

            var ex = Assert.Throws <Exception>(() => GetInvoker().ExecuteCommand(typeof(ExecuteCommandNewObject), picker));

            StringAssert.StartsWith("Type must be derived from DatabaseEntity", ex.Message);
        }
예제 #15
0
        public void Test_NewObjectCommand_MissingNameArgument()
        {
            var picker = new CommandLineObjectPicker(new[] { "Catalogue" }, GetActivator());

            Assert.AreEqual(typeof(Catalogue), picker[0].Type);

            var ex = Assert.Throws <ArgumentException>(() => GetInvoker().ExecuteCommand(typeof(ExecuteCommandNewObject), picker));

            StringAssert.StartsWith("Value needed for parameter 'name' (of type 'System.String')", ex.Message);
        }
예제 #16
0
        public void Test_NewObjectCommand_Success()
        {
            var picker = new CommandLineObjectPicker(new[] { "Catalogue", "lolzeeeyeahyeah" }, GetActivator());

            Assert.AreEqual(typeof(Catalogue), picker[0].Type);

            Assert.DoesNotThrow(() => GetInvoker().ExecuteCommand(typeof(ExecuteCommandNewObject), picker));

            Assert.Contains("lolzeeeyeahyeah", RepositoryLocator.CatalogueRepository.GetAllObjects <Catalogue>().Select(c => c.Name).ToArray());
        }
예제 #17
0
        /// <summary>
        /// Takes a single command line e.g. "list Catalogue" and spits it into a command "list" (returned) and the arguments list (as <paramref name="picker"/>)
        /// </summary>
        /// <param name="command"></param>
        /// <param name="picker"></param>
        /// <param name="repositoryLocator"></param>
        /// <returns></returns>
        private string GetCommandAndPickerFromLine(string command, out CommandLineObjectPicker picker, IRDMPPlatformRepositoryServiceLocator repositoryLocator)
        {
            if (command.Contains(' '))
            {
                picker = new CommandLineObjectPicker(SplitCommandLine(command).Skip(1).ToArray(), _input);
                return(command.Substring(0, command.IndexOf(' ')));
            }

            picker = null;
            return(command);
        }
        public void TestSetArgument_NoArgumentFound()
        {
            var pt = WhenIHaveA <ProcessTask>();


            var picker = new CommandLineObjectPicker(new [] { $"ProcessTask:{pt.ID}", "fff", "yyy" }, GetActivator());
            var cmd    = new ExecuteCommandSetArgument(GetMockActivator().Object, picker);

            Assert.IsTrue(cmd.IsImpossible);
            StringAssert.StartsWith("Could not find argument called 'fff' on ", cmd.ReasonCommandImpossible);
        }
예제 #19
0
        public void Test_Delete_WithPicker()
        {
            var mgr     = new ConsoleInputManager(RepositoryLocator, new ThrowImmediatelyCheckNotifier());
            var invoker = new CommandInvoker(mgr);

            WhenIHaveA <Catalogue>();

            var picker = new CommandLineObjectPicker(new[] { "Catalogue:*" }, RepositoryLocator);

            invoker.ExecuteCommand(typeof(ExecuteCommandDelete), picker);
        }
예제 #20
0
        public void Test_PickCatalogueByID_PickTwo()
        {
            var cata1 = WhenIHaveA <Catalogue>();
            var cata2 = WhenIHaveA <Catalogue>();

            var picker = new CommandLineObjectPicker(new [] { $"Catalogue:{cata1.ID},{cata2.ID}" }, GetActivator());

            Assert.AreEqual(2, picker[0].DatabaseEntities.Count);
            Assert.Contains(cata1, picker[0].DatabaseEntities);
            Assert.Contains(cata2, picker[0].DatabaseEntities);
        }
예제 #21
0
        public void Test_Delete_WithPicker()
        {
            var mgr     = GetActivator();
            var invoker = new CommandInvoker(mgr);

            WhenIHaveA <Catalogue>();

            var picker = new CommandLineObjectPicker(new[] { "Catalogue:*" }, mgr);

            invoker.ExecuteCommand(typeof(ExecuteCommandDelete), picker);
        }
예제 #22
0
        public void Test_PickCatalogueByID_PickTwo()
        {
            var cata1 = WhenIHaveA <Catalogue>();
            var cata2 = WhenIHaveA <Catalogue>();

            var picker = new CommandLineObjectPicker(new [] { $"Catalogue:{cata1.ID},{cata2.ID}" }, RepositoryLocator);

            Assert.AreEqual(cata1, picker[0].DatabaseEntities[0]);
            Assert.AreEqual(cata2, picker[0].DatabaseEntities[1]);
            Assert.AreEqual(2, picker[0].DatabaseEntities.Count);
        }
예제 #23
0
        public void Test_RandomGarbage_GeneratesRawValueOnly()
        {
            string str    = $"Shiver me timbers";
            var    picker = new CommandLineObjectPicker(new [] { str }, GetActivator());

            Assert.AreEqual(str, picker[0].RawValue);
            Assert.IsNull(picker[0].DatabaseEntities);
            Assert.IsNull(picker[0].Database);
            Assert.IsNull(picker[0].Table);
            Assert.IsNull(picker[0].Type);
        }
예제 #24
0
        public void Test_PickCatalogueByTypeOnly_WithShortCode()
        {
            var cata1 = WhenIHaveA <Catalogue>();
            var cata2 = WhenIHaveA <Catalogue>();

            // c is short for Catalogue
            // so this would be the use case 'rdmp cmd list Catalogue' where user can instead write 'rdmp cmd list c'
            var picker = new CommandLineObjectPicker(new[] { $"c" }, GetActivator());

            Assert.AreEqual(2, picker[0].DatabaseEntities.Count);
            Assert.Contains(cata1, picker[0].DatabaseEntities);
            Assert.Contains(cata2, picker[0].DatabaseEntities);
        }
예제 #25
0
        public void Test_PickCatalogueByName_WithShortCode()
        {
            var cata1 = WhenIHaveA <Catalogue>();
            var cata2 = WhenIHaveA <Catalogue>();

            cata1.Name = "Biochem";
            cata2.Name = "Haematology";

            var picker = new CommandLineObjectPicker(new[] { $"c:*io*" }, GetActivator());

            Assert.AreEqual(cata1, picker[0].DatabaseEntities[0]);
            Assert.AreEqual(1, picker[0].DatabaseEntities.Count);
        }
예제 #26
0
        private CommandLineObjectPickerArgumentValue ReadLineWithAuto(PickObjectBase[] pickers, IEnumerable <string> autoComplete)
        {
            if (DisallowInput)
            {
                throw new InputDisallowedException("Value required");
            }

            string line = ReadLineWithAuto(autoComplete);

            var picker = new CommandLineObjectPicker(new[] { line }, RepositoryLocator, pickers);

            return(picker[0]);
        }
예제 #27
0
        public void Test_PickCatalogueByID_PickOne()
        {
            var cata = WhenIHaveA <Catalogue>();

            var picker = new CommandLineObjectPicker(new [] { $"Catalogue:{cata.ID}" }, GetActivator());

            Assert.AreEqual(cata, picker[0].DatabaseEntities.Single());


            //specifying the same ID twice shouldn't return duplicate objects
            picker = new CommandLineObjectPicker(new [] { $"Catalogue:{cata.ID},{cata.ID}" }, GetActivator());

            Assert.AreEqual(cata, picker[0].DatabaseEntities.Single());
        }
예제 #28
0
        public void Test_PickWithPropertyQuery_PeriodicityNull()
        {
            // Catalogues
            var c1 = WhenIHaveA <Catalogue>();
            var c2 = WhenIHaveA <Catalogue>();

            c1.PivotCategory_ExtractionInformation_ID = 10;
            c2.PivotCategory_ExtractionInformation_ID = null;

            var picker = new CommandLineObjectPicker(new[] { $"Catalogue?PivotCategory_ExtractionInformation_ID:null" }, GetActivator());

            Assert.AreEqual(1, picker[0].DatabaseEntities.Count);
            Assert.Contains(c2, picker[0].DatabaseEntities);
        }
예제 #29
0
        public void Test_PickerForWhitespace(string val)
        {
            var picker = new CommandLineObjectPicker(new [] { val }, GetActivator());

            Assert.AreEqual(1, picker.Length);

            Assert.IsNull(picker[0].Database);
            Assert.IsNull(picker[0].DatabaseEntities);
            Assert.IsFalse(picker[0].ExplicitNull);
            Assert.AreEqual(val, picker[0].RawValue);
            Assert.IsNull(picker[0].Type);

            Assert.AreEqual(val, picker[0].GetValueForParameterOfType(typeof(string)));
            Assert.IsTrue(picker.HasArgumentOfType(0, typeof(string)));
        }
        public void TestSetArgument_Int_Valid()
        {
            var pta = WhenIHaveA <ProcessTaskArgument>();
            var pt  = pta.ProcessTask;

            pta.Name = "fff";
            pta.SetType(typeof(int));

            Assert.IsNull(pta.Value);

            var picker = new CommandLineObjectPicker(new [] { $"ProcessTask:{pt.ID}", "fff", "5" }, GetActivator());

            Assert.DoesNotThrow(() => GetInvoker().ExecuteCommand(typeof(ExecuteCommandSetArgument), picker));

            Assert.AreEqual(5, pta.GetValueAsSystemType());
        }