Пример #1
0
        public override void Execute()
        {
            if (ScriptName == null)
            {
                Reporter.ToLog(eLogLevel.ERROR, "Script file not Selected. Kindly select suitable file");
                this.Error = "Script file not loaded. Kindly select suitable file";
                return;
            }
            DataBuffer  = "";
            ErrorBuffer = "";
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.CreateNoWindow         = true;
            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError  = true;
            //TODO: user customerd script location

            switch (ScriptInterpreterType)
            {
            case eScriptInterpreterType.JS:
            case eScriptInterpreterType.VBS:
                if (File.Exists(GetSystemDirectory() + @"\cscript.exe"))
                {
                    p.StartInfo.FileName = GetSystemDirectory() + @"\cscript.exe";
                }
                else
                {
                    p.StartInfo.FileName = @"cscript";
                }
                break;

            case eScriptInterpreterType.Other:
                if (!string.IsNullOrEmpty(ScriptInterpreter))
                {
                    p.StartInfo.FileName = ScriptInterpreter.Replace(@"~\", this.SolutionFolder);
                }
                break;
            }
            p.OutputDataReceived += (proc, outLine) => { AddData(outLine.Data); };
            p.ErrorDataReceived  += (proc, outLine) => { AddError(outLine.Data); };
            p.Exited             += Process_Exited;
            if (string.IsNullOrEmpty(SolutionFolder))
            {
                if (string.IsNullOrEmpty(ScriptPath))
                {
                    p.StartInfo.WorkingDirectory = Path.GetDirectoryName(ScriptName);
                }
                else
                {
                    p.StartInfo.WorkingDirectory = ScriptPath;
                }
            }
            else
            {
                p.StartInfo.WorkingDirectory = SolutionFolder + @"Documents\scripts\";
            }
            try
            {
                string Params = GetCommandText(this);
                p.StartInfo.Arguments = "\"" + ScriptName + "\" " + Params;
                if (ScriptInterpreter != null && ScriptInterpreter.Contains("cmd.exe"))
                {
                    p.StartInfo.Arguments = " /k " + ScriptName + " " + Params;
                }

                p.Start();

                p.BeginOutputReadLine();
                p.BeginErrorReadLine();
                p.WaitForExit();
                while (!p.HasExited)
                {
                    Thread.Sleep(100);
                }
            }
            catch (Exception e)
            {
                Reporter.ToLog(eLogLevel.ERROR, e.Message);
                this.Error = "Failed to execute the script. Details: " + e.Message;
            }
            if (!string.IsNullOrEmpty(ErrorBuffer))
            {
                Error += ErrorBuffer;
            }
        }