Example #1
0
        /// =======================================================================================================
        private static void RunCommand(AutoReport auto_report, Map map, DataGridView batch_data,
                                       out List <Control> interactive_controls,
                                       List <Var> vars = null)
        {
            List <Control> interactivecontrols = null;
            int            rep = 1;

            if (Regex.IsMatch(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), COMMAND_MODE_REPEAT))
            {
                rep = Convert.ToInt16(Regex.Match(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), COMMAND_MODE_REPEAT).Value.Substring(1));
            }
            if (Regex.IsMatch(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), COMMAND_MODE_THREAD_CATCH))
            {
                auto_report.WaitForPreviousBatches(map);
            }
            Action runcommand = new Action(() =>
            {
                for (int i = 1; i <= rep; i++)
                {
                    DataGridViewRow cmd = batch_data.Rows[map.C];
                    string workdir      = Data.GetCell(cmd, (int)CAT.BatchFields.Path);
                    string command      = Data.GetCell(cmd, (int)CAT.BatchFields.Command);
                    Map newmap          = new Map(map.A, map.B, map.C, rep > 1 ? i : 0);
                    auto_report.AddCommand(new CommandReport(newmap, workdir + ">" + command));
                    Status status = Status.START;
                    if (Data.IsCommandEnabled(cmd))
                    {
                        if (vars != null)
                        {
                            string initialcommand = command;
                            string initialworkdir = workdir;
                            foreach (Var vartoinsert in vars)
                            {
                                command = command.Replace("{" + vartoinsert.Name + "}", vartoinsert.Value);
                                workdir = workdir.Replace("{" + vartoinsert.Name + "}", vartoinsert.Value);
                            }
                            command = BasicArithmetic(command);
                            if (initialcommand != command || initialworkdir != workdir)
                            {
                                auto_report.AddOverlayedMessage(newmap, "Variable updates (Workdir>Command): " + workdir + ">" + command, Status.WARN);
                            }
                        }
                        if (string.IsNullOrWhiteSpace(Data.GetCell(cmd, (int)CAT.BatchFields.Command)))
                        {
                            auto_report.AddOverlayedMessage(newmap, "Command is null", Status.FAIL);
                            status = Status.FAIL;
                        }
                        else if (string.IsNullOrWhiteSpace(Data.GetCell(cmd, (int)CAT.BatchFields.Timeout)))
                        {
                            auto_report.AddOverlayedMessage(newmap, "Timeout is null", Status.FAIL);
                            status = Status.FAIL;
                        }
                        else
                        {
                            CAT.FormAccess(CAT.FormDelegates.SetCommandStatus, new object[] { Status.BUSY, newmap.C }, newmap.A);
                            if (Regex.IsMatch(Data.GetCell(cmd, (int)CAT.BatchFields.Path), @".*\.dll"))
                            {
                                status = RunDll(newmap, workdir, command, Data.GetCmdTimeout(cmd), out interactivecontrols, auto_report);
                            }
                            else
                            {
                                status = RunCmd(auto_report, newmap, "cmd.exe", workdir, command, Data.GetCmdTimeout(cmd), "/C");
                            }
                        }
                    }
                    else
                    {
                        status = Status.SKIP;
                    }
                    auto_report.CommandComplete(newmap, status);
                    CAT.FormAccess(CAT.FormDelegates.SetCommandStatus, new object[] { status, newmap.C }, newmap.A);
                }
            });

            if (Regex.IsMatch(Data.GetCell(batch_data, (int)CAT.BatchFields.Mode, map.C), "-t"))
            {
                Task t = new Task(() => { runcommand(); });
                CommandTasks.Add(t);
                t.Start();
            }
            else
            {
                runcommand();
            }
            interactive_controls = interactivecontrols;
        }