Skip to content

Commit 6b64793

Browse files
committed
Merge branch 'feature/process-streams' into develop
2 parents 86bbe9e + 8597794 commit 6b64793

File tree

8 files changed

+465
-140
lines changed

8 files changed

+465
-140
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using ScriptEngine.Machine;
6+
using ScriptEngine.Machine.Contexts;
7+
using System.Diagnostics;
8+
9+
namespace ScriptEngine.HostedScript.Library
10+
{
11+
/// <summary>
12+
/// Позволяет управлять процессом операционной системы. Получать текст из стандартных потоков,
13+
/// проверять активность, pid, завершать процесс и т.п.
14+
/// </summary>
15+
[ContextClass("Процесс", "Process")]
16+
public class ProcessContext : AutoContext<ProcessContext>, IDisposable
17+
{
18+
private System.Diagnostics.Process _p;
19+
private StdTextReadStream _stdOutContext;
20+
private StdTextReadStream _stdErrContext;
21+
private StdTextWriteStream _stdInContext;
22+
23+
public ProcessContext(System.Diagnostics.Process p)
24+
{
25+
this._p = p;
26+
}
27+
28+
[ContextProperty("ПотокВывода", "StdOut")]
29+
public StdTextReadStream StdOut
30+
{
31+
get
32+
{
33+
if(_stdOutContext == null)
34+
_stdOutContext = new StdTextReadStream(_p.StandardOutput);
35+
return _stdOutContext;
36+
}
37+
}
38+
39+
[ContextProperty("ПотокОшибок", "StdErr")]
40+
public StdTextReadStream StdErr
41+
{
42+
get
43+
{
44+
if (_stdErrContext == null)
45+
_stdErrContext = new StdTextReadStream(_p.StandardError);
46+
return _stdErrContext;
47+
}
48+
}
49+
50+
[ContextProperty("ПотокВвода", "StdIn")]
51+
public StdTextWriteStream StdIn
52+
{
53+
get
54+
{
55+
if (_stdInContext == null)
56+
_stdInContext = new StdTextWriteStream(_p.StandardInput);
57+
return _stdInContext;
58+
}
59+
}
60+
61+
[ContextMethod("Запустить", "Start")]
62+
public void Start()
63+
{
64+
_p.Start();
65+
}
66+
67+
[ContextProperty("Завершен","HasExited")]
68+
public bool HasExited
69+
{
70+
get
71+
{
72+
return _p.HasExited;
73+
}
74+
}
75+
76+
[ContextProperty("КодВозврата", "ExitCode")]
77+
public int ExitCode
78+
{
79+
get
80+
{
81+
return _p.ExitCode;
82+
}
83+
}
84+
85+
[ContextMethod("ОжидатьЗавершения", "WaitForExit")]
86+
public void WaitForExit()
87+
{
88+
_p.WaitForExit();
89+
}
90+
91+
[ContextProperty("Идентификатор", "ProcessId")]
92+
public int ProcessId
93+
{
94+
get
95+
{
96+
return _p.Id;
97+
}
98+
}
99+
100+
[ContextMethod("Завершить","Stop")]
101+
public void Stop()
102+
{
103+
_p.Kill();
104+
}
105+
106+
public void Dispose()
107+
{
108+
if(_stdOutContext != null)
109+
{
110+
_stdOutContext.Dispose();
111+
_stdOutContext = null;
112+
}
113+
114+
if (_stdErrContext != null)
115+
{
116+
_stdErrContext.Dispose();
117+
_stdErrContext = null;
118+
}
119+
120+
if(_stdInContext != null)
121+
{
122+
_stdInContext.Dispose();
123+
_stdInContext = null;
124+
}
125+
126+
_p.Dispose();
127+
}
128+
}
129+
}

src/ScriptEngine.HostedScript/Library/ProcessStartupContext.cs

Lines changed: 0 additions & 96 deletions
This file was deleted.
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
1-
using ScriptEngine.Machine.Contexts;
1+
using ScriptEngine.Machine;
2+
using ScriptEngine.Machine.Contexts;
23
using System;
34
using System.Collections.Generic;
5+
using System.IO;
46
using System.Linq;
57
using System.Text;
68

79
namespace ScriptEngine.HostedScript.Library
810
{
9-
public class StdTextReadStream : AutoContext<StdTextReadStream>
11+
[ContextClass("ПотокВыводаТекста","TextOutputStream")]
12+
public class StdTextReadStream : AutoContext<StdTextReadStream>, IDisposable
1013
{
14+
private StreamReader _reader;
15+
16+
public StdTextReadStream(StreamReader source)
17+
{
18+
_reader = source;
19+
}
20+
21+
[ContextProperty("ЕстьДанные", "HasData")]
22+
public bool HasData
23+
{
24+
get
25+
{
26+
return !_reader.EndOfStream;
27+
}
28+
}
29+
30+
[ContextMethod("Прочитать", "Read")]
31+
public string Read()
32+
{
33+
return _reader.ReadToEnd();
34+
}
35+
36+
[ContextMethod("ПрочитатьСтроку", "ReadLine")]
37+
public string ReadLine()
38+
{
39+
return _reader.ReadLine();
40+
}
41+
42+
[ContextMethod("Закрыть", "Close")]
43+
public void Close()
44+
{
45+
_reader.Close();
46+
}
47+
48+
public void Dispose()
49+
{
50+
_reader.Dispose();
51+
}
1152
}
1253
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using ScriptEngine.Machine;
2+
using ScriptEngine.Machine.Contexts;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
9+
namespace ScriptEngine.HostedScript.Library
10+
{
11+
[ContextClass("ПотокВводаТекста", "TextInputStream")]
12+
public class StdTextWriteStream : AutoContext<StdTextWriteStream>, IDisposable
13+
{
14+
private StreamWriter _writer;
15+
16+
public StdTextWriteStream(StreamWriter writer)
17+
{
18+
_writer = writer;
19+
}
20+
21+
[ContextMethod("Записать","Write")]
22+
public void Write(string data)
23+
{
24+
_writer.Write(data);
25+
}
26+
27+
[ContextMethod("ЗаписатьСтроку", "WriteLine")]
28+
public void WriteLine(string data)
29+
{
30+
_writer.WriteLine(data);
31+
}
32+
33+
[ContextMethod("Закрыть", "Close")]
34+
public void Close()
35+
{
36+
_writer.Close();
37+
}
38+
39+
public void Dispose()
40+
{
41+
_writer.Dispose();
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)