-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathScriptHandler.cs
71 lines (70 loc) · 2.67 KB
/
ScriptHandler.cs
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
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace AutoDarkModeSvc.Handlers
{
public static class ScriptHandler
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public static void Launch(string name, string path, List<string> args, string cwd = null)
{
try
{
if (args == null) args = new();
string argsString = "";
argsString = string.Join(" ", args.Select(a => $"\"{a}\""));
Logger.Info($"running {name}: \"{path}\" {argsString}");
List<string> stdOut = new();
List<string> stdErr = new();
using Process p = new();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = path;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
args.ForEach(a => p.StartInfo.ArgumentList.Add(a));
if (cwd != null) p.StartInfo.WorkingDirectory = cwd;
p.ErrorDataReceived += (sender, line) =>
{
if (line.Data != null) stdErr.Add(line.Data);
};
p.OutputDataReceived += (sender, line) =>
{
if (line.Data != null) stdOut.Add(line.Data);
};
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
bool timeout = !p.WaitForExit(10000);
if (!timeout)
{
p.WaitForExit();
}
if (stdErr.Count != 0)
{
Logger.Warn($"{name}'s output does not indicate success: {string.Join("\n", stdErr)}");
}
if (stdOut.Count > 0)
{
Logger.Info($"{name}'s output: {string.Join("\n", stdOut)}");
}
if (timeout)
{
p.Kill();
Logger.Warn($"{name}: {path} {args} took too long to complete and had to be stopped");
}
if (p.ExitCode != 0)
{
Logger.Warn($"{name}'s exit code does not indicate success. exit code: { p.ExitCode }");
}
}
catch (Exception ex)
{
Logger.Warn(ex, $"error while running {name}:");
}
}
}
}