-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
process.rs
176 lines (147 loc) · 4.99 KB
/
process.rs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::gc::Gc;
use crate::values::port::{SteelPort, SteelPortRepr};
use crate::values::structs::SteelResult;
use crate::SteelVal;
use crate::{rvals::Custom, steel_vm::builtin::BuiltInModule};
use crate::{steel_vm::register_fn::RegisterFn, SteelErr};
use std::io::{BufReader, BufWriter};
use std::process::{Child, Command, Stdio};
pub fn process_module() -> BuiltInModule {
let mut module = BuiltInModule::new("steel/process".to_string());
module
.register_fn("command", CommandBuilder::new)
.register_fn("set-current-dir!", CommandBuilder::current_dir)
.register_fn("set-piped-stdout!", CommandBuilder::stdout_piped)
.register_fn("spawn-process", CommandBuilder::spawn_process)
.register_fn("wait", ChildProcess::wait)
.register_fn("wait->stdout", ChildProcess::wait_with_stdout)
.register_fn("which", binary_exists_on_path)
.register_fn("child-stdout", ChildProcess::stdout)
.register_fn("child-stderr", ChildProcess::stderr)
.register_fn("child-stdin", ChildProcess::stdin)
.register_fn("set-env-var!", CommandBuilder::env_var)
.register_fn("kill", ChildProcess::kill);
module
}
#[derive(Debug)]
struct CommandBuilder {
command: Command,
}
#[derive(Debug)]
struct ChildProcess {
child: Option<Child>,
}
fn binary_exists_on_path(binary: String) -> Option<String> {
#[cfg(not(target_arch = "wasm32"))]
match which::which(binary) {
Ok(v) => Some(v.into_os_string().into_string().unwrap()),
Err(_) => None,
}
#[cfg(target_arch = "wasm32")]
None
}
impl ChildProcess {
pub fn new(child: Child) -> Self {
Self { child: Some(child) }
}
pub fn stdout(&mut self) -> Option<SteelVal> {
let stdout = self
.child
.as_mut()
.and_then(|x| x.stdout.take())
.and_then(|x| {
Some(SteelVal::PortV(SteelPort {
port: Gc::new_mut(SteelPortRepr::ChildStdOutput(BufReader::new(x))),
}))
});
stdout
}
pub fn stderr(&mut self) -> Option<SteelVal> {
let stdout = self
.child
.as_mut()
.and_then(|x| x.stderr.take())
.and_then(|x| {
Some(SteelVal::PortV(SteelPort {
port: Gc::new_mut(SteelPortRepr::ChildStdError(BufReader::new(x))),
}))
});
stdout
}
pub fn stdin(&mut self) -> Option<SteelVal> {
let stdout = self
.child
.as_mut()
.and_then(|x| x.stdin.take())
.and_then(|x| {
Some(SteelVal::PortV(SteelPort {
port: Gc::new_mut(SteelPortRepr::ChildStdInput(BufWriter::new(x))),
}))
});
stdout
// todo!()
}
fn wait_impl(&mut self) -> Result<SteelVal, SteelErr> {
let exit_status = self
.child
.take()
.ok_or_else(crate::throw!(Generic => "Child already awaited!"))?
.wait()
.map_err(SteelErr::from)?;
match exit_status.code() {
Some(code) => Ok(code.into()),
None => Ok(false.into()),
}
}
pub fn wait(&mut self) -> SteelResult<SteelVal, SteelErr> {
self.wait_impl().into()
}
pub fn kill(&mut self) -> Result<SteelVal, SteelErr> {
self.child
.take()
.ok_or_else(crate::throw!(Generic => "Child already killed!"))?
.kill()
.map_err(SteelErr::from)
.map(|_| SteelVal::Void)
}
fn wait_with_stdout_impl(&mut self) -> Result<String, SteelErr> {
let stdout = self
.child
.take()
.ok_or_else(crate::throw!(Generic => "Child already awaited!"))?
.wait_with_output()?
.stdout;
String::from_utf8(stdout)
.map_err(|e| SteelErr::new(crate::rerrs::ErrorKind::ConversionError, e.to_string()))
}
pub fn wait_with_stdout(&mut self) -> SteelResult<String, SteelErr> {
self.wait_with_stdout_impl().into()
}
}
impl CommandBuilder {
pub fn new(command: String, args: crate::values::lists::SteelList<String>) -> CommandBuilder {
let mut command = Command::new(command);
command.args(&args);
Self { command }
}
pub fn current_dir(&mut self, directory: String) {
self.command.current_dir(directory);
}
pub fn env_var(&mut self, key: String, value: String) {
self.command.env(key, value);
}
pub fn stdout_piped(&mut self) {
self.command.stdout(Stdio::piped());
self.command.stderr(Stdio::piped());
self.command.stdin(Stdio::piped());
}
pub fn spawn_process(&mut self) -> SteelResult<ChildProcess, SteelErr> {
self.command
.spawn()
.map(ChildProcess::new)
.map_err(|x| x.into())
.into()
}
}
impl Custom for CommandBuilder {}
impl Custom for ChildProcess {}