Skip to content

Commit 2c0da7a

Browse files
author
zilch
committed
add Kill function to Operation
1 parent 029efaa commit 2c0da7a

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

Editor/EditorShell.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ public static Operation Execute(string cmd,Options options = null){
117117
start.RedirectStandardInput = true;
118118
start.StandardOutputEncoding = options.encoding;
119119
start.StandardErrorEncoding = options.encoding;
120+
121+
if(operation.isKillRequested){
122+
return;
123+
}
124+
120125
p = Process.Start(start);
126+
operation.BindProcess(p);
121127

122128
p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) {
123129
UnityEngine.Debug.LogError(e.Data);
@@ -135,12 +141,11 @@ public static Operation Execute(string cmd,Options options = null){
135141
break;
136142
}
137143
line = line.Replace("\\","/");
138-
139144
Enqueue(delegate() {
140145
operation.FeedLog(LogType.Log,line);
141146
});
142-
143147
}while(true);
148+
144149
while(true){
145150
string error = p.StandardError.ReadLine();
146151
if(string.IsNullOrEmpty(error)){
@@ -179,6 +184,15 @@ public class Options{
179184
public class Operation{
180185
public event UnityAction<LogType,string> onLog;
181186
public event UnityAction<int> onExit;
187+
188+
private Process _process;
189+
190+
private bool _killRequested = false;
191+
192+
internal void BindProcess(Process process){
193+
_process = process;
194+
}
195+
182196
internal void FeedLog(LogType logType,string log){
183197
if(onLog != null){
184198
onLog(logType,log);
@@ -187,6 +201,26 @@ internal void FeedLog(LogType logType,string log){
187201
this.hasError = true;
188202
}
189203
}
204+
205+
public bool isKillRequested{
206+
get{
207+
return _killRequested;
208+
}
209+
}
210+
211+
public void Kill(){
212+
if(_killRequested){
213+
return;
214+
}
215+
_killRequested = true;
216+
if(_process != null){
217+
_process.Kill();
218+
_process = null;
219+
}else{
220+
FireDone(137);
221+
}
222+
}
223+
190224
public bool hasError{
191225
get;private set;
192226
}

Tests/EditorShellTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ public IEnumerator ExitWithCode1Async(){
4040
Assert.True(task.Result == 1);
4141
}
4242

43+
[UnityTest]
44+
public IEnumerator KillAsyncOperation(){
45+
var operation = EditorShell.Execute("sleep 5",new EditorShell.Options());
46+
KillAfter1Second(operation);
47+
var task = GetOperationTask(operation);
48+
yield return new TaskYieldable<int>(task);
49+
Debug.Log("exit with code = " + task.Result);
50+
Assert.True(task.Result == 137);
51+
}
52+
53+
private async void KillAfter1Second(EditorShell.Operation operation){
54+
await Task.Delay(1000);
55+
operation.Kill();
56+
}
57+
58+
private async Task<int> GetOperationTask(EditorShell.Operation operation){
59+
int code = await operation;
60+
return code;
61+
}
62+
4363
private async Task<int> ExecuteShellAsync(string cmd){
4464
var task = EditorShell.Execute(cmd,new EditorShell.Options());
4565
int code = await task;

0 commit comments

Comments
 (0)