private CommandElements PrepareFreeCommand() { string Arguments = string.Empty; CommandElements command = new CommandElements(); command.ExecuterFilePath = GroovyExeFullPath; command.WorkingFolder = Path.GetDirectoryName(GroovyExeFullPath); Arguments += string.Format("\"{0}\"", GroovyScriptPath); if (GroovyPrameters != null) { foreach (GroovyPrameters gp in GroovyPrameters) { Arguments += " " + gp.Value; } } command.Arguments = Arguments; return(command); }
public void Execute() { Console.Write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Execution Started %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); try { switch (ExecutionMode) { case eExecutionMode.ScriptPath: CommandElements command = new CommandElements(); command = PrepareFreeCommand(); ExecuteCommand(command); ParseCommandOutput(); break; } } catch (Exception ex) { GingerAction.AddError("Error while executing script : " + ex.ToString()); } }
public void ExecuteCommand(object commandVal) { try { CommandElements commandVals = (CommandElements)commandVal; Process process = new Process(); if (commandVals.WorkingFolder != null) { process.StartInfo.WorkingDirectory = commandVals.WorkingFolder; } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { process.StartInfo.FileName = commandVals.ExecuterFilePath; process.StartInfo.Arguments = commandVals.Arguments; } else//Linux { var escapedExecuter = commandVals.ExecuterFilePath.Replace("\"", "\\\""); var escapedArgs = commandVals.Arguments.Replace("\"", "\\\""); process.StartInfo.WorkingDirectory = ""; process.StartInfo.FileName = "/bin/bash"; process.StartInfo.Arguments = $"-c \"{escapedExecuter} {escapedArgs}\""; } process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; mCommandOutputBuffer = string.Empty; mCommandOutputErrorBuffer = string.Empty; process.OutputDataReceived += (proc, outLine) => { AddCommandOutput(outLine.Data); }; process.ErrorDataReceived += (proc, outLine) => { AddCommandOutputError(outLine.Data); }; process.Exited += Process_Exited; Console.Write("--Staring process"); process.Start(); Stopwatch stopwatch = Stopwatch.StartNew(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); int maxWaitingTime = 1000 * 60 * 60;//1 hour process.WaitForExit(maxWaitingTime); Console.Write("--Process done"); stopwatch.Stop(); if (stopwatch.ElapsedMilliseconds >= maxWaitingTime) { GingerAction.AddError("Command processing timeout has reached!"); } } catch (Exception ex) { GingerAction.AddError("Failed to execute the command, Error is: '{0}'" + ex.Message); Console.Write(ex.Message); } finally { GingerAction.AddExInfo("--Exiting execute command"); } }