-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathProcessBuildTask.cs
More file actions
87 lines (80 loc) · 3.07 KB
/
ProcessBuildTask.cs
File metadata and controls
87 lines (80 loc) · 3.07 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace SmartCode.App.BuildTasks
{
public class ProcessBuildTask : IBuildTask
{
const string CREATE_NO_WINDOW = "CreateNoWindow";
const string WORKING_DIRECTORY = "WorkingDirectory";
const string FILE_NAME = "FileName";
const string ARGS = "Args";
const string TIMEOUT = "Timeout";
private readonly ILogger<ProcessBuildTask> _logger;
const int DEFAULT_TIME_OUT = 30 * 1000;
const bool DEFAULT_CREATE_NO_WINDOW = true;
public bool Initialized => true;
public string Name => "Process";
public ProcessBuildTask(ILogger<ProcessBuildTask> logger)
{
_logger = logger;
}
public Task Build(BuildContext context)
{
if (!context.Build.Paramters.Value(FILE_NAME, out string fileName))
{
throw new SmartCodeException($"Build:{context.BuildKey},Can not find Paramter:{FILE_NAME}!");
}
if (!context.Build.Paramters.Value(ARGS, out string args))
{
throw new SmartCodeException($"Build:{context.BuildKey},Can not find Paramter:{ARGS}!");
}
var process = new Process();
var startInfo = process.StartInfo;
startInfo.CreateNoWindow = DEFAULT_CREATE_NO_WINDOW;
startInfo.FileName = fileName;
startInfo.Arguments = args;
if (context.Build.Paramters.Value(WORKING_DIRECTORY, out string workingDic))
{
startInfo.WorkingDirectory = workingDic;
}
if (context.Build.Paramters.Value(CREATE_NO_WINDOW, out bool createNoWin))
{
startInfo.CreateNoWindow = createNoWin;
}
_logger.LogDebug($"--------Process.FileName:{startInfo.FileName},Args:{startInfo.Arguments} Start--------");
process.ErrorDataReceived += Process_ErrorDataReceived;
process.OutputDataReceived += Process_OutputDataReceived;
try
{
process.Start();
var timeOut = DEFAULT_TIME_OUT;
if (context.Build.Paramters.Value(TIMEOUT, out int _timeout))
{
timeOut = _timeout;
}
process.WaitForExit(timeOut);
_logger.LogDebug($"--------Process.FileName:{startInfo.FileName},Args:{startInfo.Arguments} End--------");
}
finally
{
process.Dispose();
}
return Task.CompletedTask;
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
_logger.LogDebug(e.Data);
}
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
_logger.LogDebug(e.Data);
}
public void Initialize(IDictionary<string, object> paramters)
{
}
}
}