-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path_exec.shade
More file actions
61 lines (50 loc) · 1.24 KB
/
Copy path_exec.shade
File metadata and controls
61 lines (50 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use namespace="System.Diagnostics"
use namespace="System.IO"
default workingdir="${Directory.GetCurrentDirectory()}"
default commandline=""
log info="Exec"
log info=" program: ${program}"
log info=" commandline: ${commandline}"
log info=" workingdir: ${workingdir}"
functions
@{
bool __WriteExecOutputToLogger { get; set; }
}
@{
var processStartInfo = new ProcessStartInfo {
UseShellExecute = false,
WorkingDirectory = workingdir,
FileName = program,
Arguments = commandline
};
if (__WriteExecOutputToLogger)
{
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardOutput = true;
}
using (var process = Process.Start(processStartInfo))
{
if (__WriteExecOutputToLogger)
{
process.EnableRaisingEvents = true;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.ErrorDataReceived += (sender, eventArgs) =>
{
if (!string.IsNullOrWhiteSpace(eventArgs.Data))
{
Log.Error(eventArgs.Data);
}
};
process.OutputDataReceived += (sender, eventArgs) =>
{
Log.Info(eventArgs.Data);
};
}
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception(string.Format("Exit code {0} from {1}", process.ExitCode, program));
}
}
}