Exemplo n.º 1
0
        public override IScriptCommand Execute(IParameterDic pm)
        {
            switch (Mode)
            {
            case RunMode.Parallel:
            case RunMode.Queue:
                ScriptRunner.RunScript(pm, ScriptCommands);
                break;

            case RunMode.Sequence:
                foreach (var cmd in ScriptCommands)
                {
                    ScriptRunner.RunScript(pm, cmd);
                    if (pm.Error() != null)
                    {
                        return(ResultCommand.Error(pm.Error()));
                    }
                }
                break;

            default:
                return(ResultCommand.Error(new NotSupportedException(Mode.ToString())));
            }

            if (pm.Error() != null)
            {
                return(ResultCommand.Error(pm.Error()));
            }
            else
            {
                return(NextCommand);
            }
        }
Exemplo n.º 2
0
 public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm)
 {
     if (CanExecute(pm))
     {
         return(NextCommand);
     }
     else
     {
         return(ResultCommand.Error(pm.Error() ?? new ArgumentException("pm")));
     }
 }
Exemplo n.º 3
0
 public override IScriptCommand Execute(IParameterDic pm)
 {
     ScriptRunner.RunScript(pm, ScriptCommands);
     if (pm.Error() != null)
     {
         return(ResultCommand.Error(pm.Error()));
     }
     else
     {
         return(_nextCommand);
     }
 }
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 5
0
        public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm)
        {
            await ScriptRunner.RunScriptAsync(pm, ScriptCommands);

            if (pm.Error() != null)
            {
                return(ResultCommand.Error(pm.Error()));
            }
            else
            {
                return(_nextCommand);
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public override async Task <IScriptCommand> ExecuteAsync(IParameterDic pm)
        {
            switch (Mode)
            {
            case RunMode.Parallel:
                await Task.WhenAll(ScriptCommands.Select(cmd => ScriptRunner.RunScriptAsync(pm.Clone(), cmd)));

                break;

            case RunMode.Queue:
                await ScriptRunner.RunScriptAsync(pm, ScriptCommands)
                .ConfigureAwait(this.ContinueOnCaptureContext);

                break;

            case RunMode.Sequence:
                foreach (var cmd in ScriptCommands)
                {
                    await ScriptRunner.RunScriptAsync(pm, cmd)
                    .ConfigureAwait(this.ContinueOnCaptureContext);

                    if (pm.Error() != null)
                    {
                        return(ResultCommand.Error(pm.Error()));
                    }
                }
                break;

            default:
                return(ResultCommand.Error(new NotSupportedException(Mode.ToString())));
            }

            if (pm.Error() != null)
            {
                return(ResultCommand.Error(pm.Error()));
            }
            else
            {
                return(NextCommand);
            }
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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));
        }
Exemplo n.º 10
0
 public override IScriptCommand Execute(IParameterDic pm)
 {
     return(ResultCommand.Error(new Exception("NullScriptCommand should not be called.")));
 }