public override IScriptCommand Execute(IParameterDic pm) { object source = pm.Get <object>(SourceVariableKey); if (source == null) { logger.Error("Source not found."); } if (ValueConverterKey != null) { object valueConverter = pm.Get <object>(ValueConverterKey); if (valueConverter is Func <object, object> ) //GetProperty, ExecuteMethod, GetArrayItem { Func <object, object> valueConverterFunc = valueConverter as Func <object, object>; object value = valueConverterFunc(source); pm.Set(DestinationVariableKey, value, SkipIfExists); } else if (valueConverter is Action <object, object> ) //SetProperty { Action <object, object> valueConverterAct = valueConverter as Action <object, object>; object value = pm.Get <object>(DestinationVariableKey); valueConverterAct(source, value); } } else { pm.Set(DestinationVariableKey, source, SkipIfExists); } return(NextCommand); }
private bool compare(IParameterDic pm) { try { object value1 = pm.Get <Object>(Variable1Key); object value2 = pm.Get <Object>(Variable2Key); if (value1 != null && value2 != null) { var left = Expression.Constant(value1); var right = Expression.Constant(value2); Expression expression; switch (Operator) { case ComparsionOperator.Equals: expression = Expression.Equal(left, right); break; case ComparsionOperator.GreaterThan: expression = Expression.GreaterThan(left, right); break; case ComparsionOperator.GreaterThanOrEqual: expression = Expression.GreaterThanOrEqual(left, right); break; case ComparsionOperator.LessThan: expression = Expression.LessThan(left, right); break; case ComparsionOperator.LessThanOrEqual: expression = Expression.LessThanOrEqual(left, right); break; case ComparsionOperator.StartWith: case ComparsionOperator.StartWithIgnoreCase: return(value1.ToString().StartsWith(value2.ToString(), Operator == ComparsionOperator.StartWith ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase)); case ComparsionOperator.EndsWith: case ComparsionOperator.EndsWithIgnoreCase: return(value1.ToString().EndsWith(value2.ToString(), Operator == ComparsionOperator.EndsWith ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase)); default: throw new NotSupportedException(Operator.ToString()); } return(Expression.Lambda <Func <bool> >(expression).Compile().Invoke()); } else { return(Operator == ComparsionOperator.Equals && value1 == null && value2 == null); } } catch (Exception ex) { throw ex; } }
public override IScriptCommand Execute(IParameterDic pm) { object value = Value; if (ValueFunc != null) { value = ValueFunc(); } if (value is string) { string valueString = (string)value; if (valueString.StartsWith("{") && valueString.EndsWith("}")) { value = pm.Get(valueString); } } if (pm.Set <Object>(VariableKey, value, SkipIfExists)) { logger.Debug(String.Format("{0} = {1}", VariableKey, value)); } // else logger.Debug(String.Format("Skipped {0}, already exists.", VariableKey)); return(NextCommand); }
public static T RunScript <T>(string resultVariable = "{Result}", IParameterDic initialParameters = null, params IScriptCommand[] commands) { initialParameters = initialParameters ?? new ParameterDic(); RunScript(initialParameters, false, commands); return(initialParameters.Get(resultVariable, default(T))); }
public override IScriptCommand Execute(IParameterDic pm) { Func <object, object> checkParameters = p => { if (p is string) { string pString = p as string; if (pString.StartsWith("{") && pString.EndsWith("}")) { return(pm.Get(pString)); } } return(p); }; object firstValue = pm.Get(Value1Key); object secondValue = pm.Get(Value2Key); List <object> secondArrayList = secondValue is Array ? (secondValue as Array).Cast <object>().ToList() : new List <object>() { secondValue }; object value = firstValue; string methodName = OperatorType.ToString(); var mInfo = typeof(FileExplorer.Utils.ExpressionUtils) .GetRuntimeMethods().First(m => m.Name == methodName) .MakeGenericMethod(value.GetType()); foreach (var addItem in secondArrayList.Select(p => checkParameters(p)).ToArray()) { switch (mInfo.GetParameters().Length) { case 1: value = mInfo.Invoke(null, new object[] { value }); break; case 2: value = mInfo.Invoke(null, new object[] { value, addItem }); break; default: throw new NotSupportedException(); } } Value = value; return(base.Execute(pm)); }
public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm) { IEnumerable e = pm.Get <IEnumerable>(ItemsKey); if (e == null) { return(ResultCommand.Error(new ArgumentException(ItemsKey))); } IProgress <TransferProgress> progress = NullProgress <TransferProgress> .Instance; if (IsProgressEnabled) { List <object> list; e = list = e.Cast <object>().ToList(); progress = pm.Progress <TransferProgress>(); progress.Report(TransferProgress.IncrementTotalEntries(list.Count)); } uint counter = 0; pm.Set <bool>(BreakKey, false); foreach (var item in e) { if (pm.Get <bool>(BreakKey)) { break; } counter++; pm.Set(CurrentItemKey, item); await ScriptRunner.RunScriptAsync(pm, NextCommand); progress.Report(TransferProgress.IncrementProcessedEntries()); if (pm.Error() != null) { pm.Set <Object>(CurrentItemKey, null); return(ResultCommand.Error(pm.Error())); } } logger.Info(String.Format("Looped {0} items", counter)); pm.Set <Object>(CurrentItemKey, null); return(ThenCommand); }
public override IScriptCommand Execute(IParameterDic pm) { try { object value1 = pm.Get <Object>(Variable1Key); object value2 = pm.Get <Object>(Variable2Key); bool result = compare(pm); logger.Debug(String.Format("Executing {0} Command ({1})", result, result ? NextCommand : OtherwiseCommand)); return(result ? NextCommand : OtherwiseCommand); } catch (Exception ex) { return(OtherwiseCommand); } }
public static void RunScript(IParameterDic initialParameters, bool cloneParameters, params IScriptCommand[] commands) { if (cloneParameters) { initialParameters = initialParameters.Clone(); } IScriptRunner runner = initialParameters.Get <IScriptRunner>("{ScriptRunner}", Instance); runner.Run(new Queue <IScriptCommand>(commands), initialParameters); }
public static Task RunScriptAsync(IParameterDic initialParameters, bool cloneParameters, params IScriptCommand[] commands) { if (cloneParameters) { initialParameters = initialParameters.Clone(); } IScriptRunner runner = initialParameters.Get <IScriptRunner>("{ScriptRunner}", Instance); return(runner.RunAsync(new Queue <IScriptCommand>(commands), initialParameters)); }
/// <summary> /// Subtract specified values to the value assigned to the parameter dic. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dic"></param> /// <param name="variableKey"></param> /// <param name="values"></param> /// <returns></returns> public static T Subtract <T>(this IParameterDic dic, string variableKey, params T[] values) { T retValue = dic.Get <T>(variableKey, default(T)); foreach (T value in values) { retValue = ExpressionUtils.Subtract <T>(value, retValue); } dic.Set <T>(variableKey, retValue); return(retValue); }
public override IScriptCommand Execute(IParameterDic pm) { IScriptCommand command = pm.Get <IScriptCommand>(CommandKey); if (command == null && ThrowIfError) { return(ResultCommand.Error(new ArgumentNullException(CommandKey))); } command = command ?? ResultCommand.NoError; log.Info("Running " + CommandKey); return(ScriptCommands.RunQueue(NextCommand, command)); }
public override IScriptCommand Execute(IParameterDic pm) { ICommand command = pm.Get <ICommand>(CommandKey); object parameter = pm.Get <object>(ParameterKey); if (command == null && ThrowIfError) { return(ResultCommand.Error(new ArgumentNullException(CommandKey))); } if ((command == null || !command.CanExecute(parameter)) && ThrowIfError) { log.Debug("Running " + CommandKey); return(ResultCommand.Error(new ArgumentException("CanExecute = false."))); } if (command != null) { command.Execute(parameter); } return(NextCommand); }
public override IScriptCommand Execute(IParameterDic pm) { object obj = pm.Get(VariableKey); if (obj != null) { print(obj.ToString()); var typeInfo = obj is Array ? typeof(Array).GetTypeInfo() : obj.GetType().GetTypeInfo(); foreach (var pi in typeInfo.EnumeratePropertyInfoRecursive()) { print(string.Format(Format, pi.Name, pi.GetValue(obj))); } } return(base.Execute(pm)); }
public static string ReplaceVariableInsideBracketed(this IParameterDic pd, string variableKey) { if (variableKey == null) { return(null); } Regex regex = new Regex("{(?<TextInsideBrackets>[^}]+)}"); string value = variableKey; Match match = regex.Match(value); while (!value.StartsWith("::") && match.Success) { string key = "{" + match.Groups["TextInsideBrackets"].Value + "}"; object val = pd.Get(key); value = value.Replace(key, val == null ? "" : val.ToString()); match = regex.Match(value); } return(value); }
public override IScriptCommand Execute(IParameterDic pm) { Func <object, object> checkParameters = p => { if (p is string) { string pString = p as string; if (pString.StartsWith("{") && pString.EndsWith("}")) { return(pm.Get(pString)); } } return(p); }; switch (ConverterType) { case ValueConverterType.GetArrayItem: Int32 id = ConverterParameter.Count() > 0 ? Int32.Parse(ConverterParameter.First().ToString()) : -1; if (id == -1) { return(ResultCommand.Error(new KeyNotFoundException())); } Value = (Func <Object, Object>)(v => (v as Array).GetValue(id)); break; case ValueConverterType.GetProperty: string property = ConverterParameter.Count() > 0 ? ConverterParameter.First() as string : null; Func <Object, Object> retVal = String.IsNullOrEmpty(property) ? (Func <Object, Object>)(v => v) : (Func <Object, Object>)(v => { var typeInfo = v is Array ? typeof(Array).GetTypeInfo() : v.GetType().GetTypeInfo(); var propertyInfo = typeInfo.GetPropertyInfoRecursive(property); if (propertyInfo == null) { var fieldInfo = typeInfo.GetFieldInfoRecursive(property); if (fieldInfo == null) { throw new KeyNotFoundException(String.Format("{0} cannot be found in {1}", property, v.GetType())); } else { return(fieldInfo.GetValue(v)); } } else { return(propertyInfo.GetValue(v)); } }); Value = retVal; break; case ValueConverterType.SetProperty: string property1 = ConverterParameter.Count() > 0 ? ConverterParameter.First() as string : null; Action <Object, Object> retVal1 = String.IsNullOrEmpty(property1) ? (Action <Object, Object>)((o, v) => { }) : (Action <Object, Object>)((o, v) => { if (o == null) { return; } var typeInfo = o is Array ? typeof(Array).GetTypeInfo() : o.GetType().GetTypeInfo(); var propertyInfo = typeInfo.GetPropertyInfoRecursive(property1); if (propertyInfo == null) { var fieldInfo = typeInfo.GetFieldInfoRecursive(property1); if (fieldInfo == null) { logger.Error(String.Format("{0} cannot be found in {1}", property1, o.GetType())); } else { fieldInfo.SetValue(o, v); } } else { propertyInfo.SetValue(o, v); } }); Value = retVal1; break; case ValueConverterType.ExecuteMethod: string methodName = ConverterParameter.Count() > 0 ? ConverterParameter.First() as string : null; object[] methodParameters = ConverterParameter.Skip(1) .Select(param => checkParameters(param)) .ToArray(); Func <MethodInfo, bool> matchParametrers = (pi) => { if (pi.GetParameters().Length == methodParameters.Length) { //var parameters = pi.GetParameters(); //for (int i = 0; i < methodParameters.Length; i++) //{ // if (!(parameters[i].ParameterType // .IsAssignableFrom(methodParameters[i].GetType()))) // return false; //} return(true); } return(false); }; Func <Object, Object> retVa1l = String.IsNullOrEmpty(methodName) ? (Func <Object, Object>)(v => v) : (Func <Object, Object>)(v => { var typeInfo = v is Array ? typeof(Array).GetTypeInfo() : v.GetType().GetTypeInfo(); var methodInfo = typeInfo.DeclaredMethods .FirstOrDefault( pi => pi.Name.Equals(methodName, StringComparison.CurrentCultureIgnoreCase) && matchParametrers(pi)); if (methodInfo == null) { throw new KeyNotFoundException(methodName); } else { return(methodInfo.Invoke(v, methodParameters)); } }); Value = retVa1l; break; case ValueConverterType.AddValue: Func <Object, Object> retVa12 = (Func <Object, Object>)(v => { var mInfo = typeof(FileExplorer.Utils.ExpressionUtils) .GetRuntimeMethods().First(m => m.Name == "Add") .MakeGenericMethod(v.GetType()); foreach (var addItem in ConverterParameter.Select(p => checkParameters(p)).ToArray()) { v = mInfo.Invoke(null, new object[] { v, addItem }); } return(v); }); Value = retVa12; break; case ValueConverterType.ConcatArray: Func <Object, Object> retVa13 = (Func <Object, Object>)(v => { var parameters = ConverterParameter.Select(p => checkParameters(p)).ToArray(); if (v == null) { v = Array.CreateInstance(parameters.First().GetType(), new int[] { 0 }); } List <object> items2Add = new List <object>(); foreach (var item in parameters) { if (item is Array) { items2Add.AddRange((item as Array).Cast <object>()); } else { items2Add.Add(item); } } Array array = v as Array; Type arrayType = array.GetType().GetElementType(); Array newArray = Array.CreateInstance(arrayType, array.Length + items2Add.Count()); Array.Copy(array, 0, newArray, 0, array.Length); Array.Copy(items2Add.ToArray(), 0, newArray, array.Length, items2Add.Count()); return(newArray); }); Value = retVa13; break; //case ValueConverterType.SubString: // Func<Object, Object> retVa14 = (Func<Object, Object>)(v => // { // if (ConverterParameter.Length != 2) // return ResultCommand.Error(new ArgumentException("Incorrect number of parameters (2).")); // var parameters = ConverterParameter.Select(p => checkParameters(p)).ToArray(); // int startIdx = (int)parameters[0]; // int? length = (int?)parameters[1]; // string str = v as string; // return length.HasValue ? str.Substring(startIdx, length.Value) : str.Substring(startIdx); // }); // Value = retVa14; // break; default: return(ResultCommand.Error(new NotSupportedException(ConverterType.ToString()))); } return(base.Execute(pm)); }
public static bool IsHandled(this IParameterDic dic) { return(dic.Get <bool>(VariableKeys.IsHandled, false)); }
public static Exception Error(this IParameterDic dic) { return(dic.Get <Exception>(VariableKeys.Error, null)); }
public static IScriptRunner ScriptRunner(this IParameterDic dic) { return(dic.Get <IScriptRunner>(VariableKeys.ScriptRunner, null)); }
public static IProgress <T> Progress <T>(this IParameterDic dic) { return(dic.Get <IProgress <T> >(getProgressVariable <T>(), null) ?? NullProgress <T> .Instance); }