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); } } }
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 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 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}'"); //suggest similar sounding properties var suggestions = _setOn.GetType().GetProperties().Where(c => CultureInfo.CurrentCulture.CompareInfo.IndexOf(c.Name, property, CompareOptions.IgnoreCase) >= 0).ToArray(); if (suggestions.Any()) { StringBuilder msg = new StringBuilder($"Unknown Property '{property}'"); msg.AppendLine(); msg.AppendLine("Did you mean:"); foreach (var s in suggestions) { msg.AppendLine(s.Name); } activator.Show(msg.ToString()); } } else { var picker = new CommandLineObjectPicker(new string[] { value ?? "NULL" }, activator); if (!picker.HasArgumentOfType(0, _property.PropertyType)) { SetImpossible($"Provided value could not be converted to '{_property.PropertyType}'"); } else { NewValue = picker[0].GetValueForParameterOfType(_property.PropertyType); } } }
public ExecuteCommandNewObject(IBasicActivateItems activator, CommandLineObjectPicker picker) : base(activator) { if (!picker.HasArgumentOfType(0, typeof(Type))) { SetImpossible("First parameter must be a Type of DatabaseEntity"); } else { _type = picker[0].Type; if (!typeof(DatabaseEntity).IsAssignableFrom(_type)) { SetImpossible("Type must be derived from DatabaseEntity"); } } _picker = picker; }
public void TwoCataloguesWithSameName_NoSession() { SetupMEF(); var cata1 = new Catalogue(Repository, "Hey"); // When there is only one object we can pick it by name var picker = new CommandLineObjectPicker(new string[] { "Catalogue:Hey" }, GetActivator()); Assert.IsTrue(picker.HasArgumentOfType(0, typeof(Catalogue))); Assert.AreEqual(cata1, picker.Arguments.First().GetValueForParameterOfType(typeof(Catalogue))); // But when there are 2 objects we don't know which to pick so cannot pick a Catalogue new Catalogue(Repository, "Hey"); var picker2 = new CommandLineObjectPicker(new string[] { "Catalogue:Hey" }, GetActivator()); Assert.IsFalse(picker2.HasArgumentOfType(0, typeof(Catalogue))); }
public ExecuteCommandSetUserSetting(IBasicActivateItems activator, [DemandsInitialization("Name of a property you want to change e.g. AllowIdentifiableExtractions")] 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) { _property = typeof(UserSettings).GetProperty(property, BindingFlags.Public | BindingFlags.Static); if (_property == null) { SetImpossible($"Unknown Property '{property}'"); //suggest similar sounding properties var suggestions = typeof(UserSettings).GetProperties(BindingFlags.Public | BindingFlags.Static).Where(c => CultureInfo.CurrentCulture.CompareInfo.IndexOf(c.Name, property, CompareOptions.IgnoreCase) >= 0).ToArray(); if (suggestions.Any()) { StringBuilder msg = new StringBuilder($"Unknown Property '{property}'"); msg.AppendLine(); msg.AppendLine("Did you mean:"); foreach (var s in suggestions) { msg.AppendLine(s.Name); } activator.Show(msg.ToString()); } } else { var picker = new CommandLineObjectPicker(new string[] { value ?? "NULL" }, activator); if (!picker.HasArgumentOfType(0, _property.PropertyType)) { SetImpossible($"Provided value could not be converted to '{_property.PropertyType}'"); } else { NewValue = picker[0].GetValueForParameterOfType(_property.PropertyType); } } }
public void TwoCataloguesWithSameName_WithSession() { SetupMEF(); using (NewObjectPool.StartSession()) { var cata1 = new Catalogue(Repository, "Hey"); // When there is only one object we can pick it by name var picker = new CommandLineObjectPicker(new string[] { "Catalogue:Hey" }, GetActivator()); Assert.IsTrue(picker.HasArgumentOfType(0, typeof(Catalogue))); Assert.AreEqual(cata1, picker.Arguments.First().GetValueForParameterOfType(typeof(Catalogue))); // There are now 2 objects with the same name but because we are in a session we can pick the latest var cata2 = new Catalogue(Repository, "Hey"); var picker2 = new CommandLineObjectPicker(new string[] { "Catalogue:Hey" }, GetActivator()); Assert.IsTrue(picker2.HasArgumentOfType(0, typeof(Catalogue))); Assert.AreEqual(cata2, picker2.Arguments.First().GetValueForParameterOfType(typeof(Catalogue))); } }
private void ExecuteCommand(ConstructorInfo constructorInfo, CommandLineObjectPicker picker) { List <object> parameterValues = new List <object>(); bool complainAboutExtraParameters = true; int idx = 0; //for each parameter on the constructor we want to invoke foreach (var parameterInfo in constructorInfo.GetParameters()) { var required = new RequiredArgument(parameterInfo); var argDelegate = GetDelegate(required); //if it is an easy one to automatically fill e.g. IBasicActivateItems if (argDelegate != null && argDelegate.IsAuto) { parameterValues.Add(argDelegate.Run(required)); } else //if the constructor argument is a picker, use the one passed in if (parameterInfo.ParameterType == typeof(CommandLineObjectPicker)) { if (picker == null) { throw new ArgumentException($"Type {constructorInfo.DeclaringType} contained a constructor which took an {parameterInfo.ParameterType} but no picker was passed"); } parameterValues.Add(picker); //the parameters are expected to be consumed by the target constructors so it's not really a problem if there are extra complainAboutExtraParameters = false; continue; } else //if we have argument values specified if (picker != null) { //and the specified value matches the expected parameter type if (picker.HasArgumentOfType(idx, parameterInfo.ParameterType)) { //consume a value parameterValues.Add(picker[idx].GetValueForParameterOfType(parameterInfo.ParameterType)); idx++; continue; } throw new Exception($"Expected parameter at index {idx} to be a {parameterInfo.ParameterType} (for parameter '{parameterInfo.Name}') but it was {(idx >= picker.Length ? "Missing":picker[idx].RawValue)}"); } else { parameterValues.Add(GetValueForParameterOfType(parameterInfo)); } } if (picker != null && idx < picker.Length && complainAboutExtraParameters) { throw new Exception("Unrecognised extra parameter " + picker[idx].RawValue); } var instance = (IAtomicCommand)constructorInfo.Invoke(parameterValues.ToArray()); if (instance.IsImpossible) { CommandImpossible?.Invoke(this, new CommandEventArgs(instance)); return; } instance.Execute(); CommandCompleted?.Invoke(this, new CommandEventArgs(instance)); }