-
-
Notifications
You must be signed in to change notification settings - Fork 932
Expand file tree
/
Copy pathbeam_compiler.rs
More file actions
157 lines (134 loc) · 4.83 KB
/
beam_compiler.rs
File metadata and controls
157 lines (134 loc) · 4.83 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
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
use gleam_core::{
Result,
error::{Error, ShellCommandFailureReason},
io::{FileSystemWriter, Stdio},
paths,
};
use crate::fs::get_os;
use std::{
collections::HashSet,
io::{self, BufRead, BufReader, Write},
process::{Child, ChildStdin, ChildStdout},
};
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;
#[derive(Debug)]
struct BeamCompilerInner {
process: Child,
stdin: ChildStdin,
stdout: BufReader<ChildStdout>,
}
#[derive(Debug, Default)]
pub struct BeamCompiler {
inner: Option<BeamCompilerInner>,
}
impl BeamCompiler {
pub fn compile<IO: FileSystemWriter>(
&mut self,
io: &IO,
out: &Utf8Path,
lib: &Utf8Path,
modules: &HashSet<Utf8PathBuf>,
stdio: Stdio,
) -> Result<Vec<String>, Error> {
let inner = match self.inner {
Some(ref mut inner) => match inner.process.try_wait() {
Ok(None) => inner,
_ => self.inner.insert(self.spawn(io, out)?),
},
None => self.inner.insert(self.spawn(io, out)?),
};
let args = format!(
"{{\"{}\", \"{}\", [\"{}\"]}}",
escape_path(lib),
escape_path(out.join("ebin")),
modules
.iter()
.map(|module| escape_path(out.join(paths::ARTEFACT_DIRECTORY_NAME).join(module)))
.join("\", \"")
);
tracing::debug!(args=?args, "call_beam_compiler");
writeln!(inner.stdin, "{args}.").map_err(|e| Error::ShellCommand {
program: "escript".into(),
reason: ShellCommandFailureReason::IoError(e.kind()),
})?;
let mut buf = String::new();
let mut accumulated_modules: Vec<String> = Vec::new();
while let (Ok(_), Ok(None)) = (inner.stdout.read_line(&mut buf), inner.process.try_wait()) {
let escript_path = paths::compilation_escript_path(out);
// Delete escript file, which is unnecessary after compilation
io.delete_file(&escript_path)?;
match buf.trim() {
"gleam-compile-result-ok" => {
// Return Ok with the accumulated modules
return Ok(accumulated_modules);
}
"gleam-compile-result-error" => {
return Err(Error::ShellCommand {
program: "escript".into(),
reason: ShellCommandFailureReason::Unknown,
});
}
s if s.starts_with("gleam-compile-module:") => {
if let Some(module_content) = s.strip_prefix("gleam-compile-module:") {
accumulated_modules.push(module_content.to_string());
}
}
_ => match stdio {
Stdio::Inherit => print!("{buf}"),
Stdio::Null => {}
},
}
buf.clear()
}
// If we get here, stdout got closed before we got an "ok" or "err".
Err(Error::ShellCommand {
program: "escript".into(),
reason: ShellCommandFailureReason::Unknown,
})
}
fn spawn<IO: FileSystemWriter>(
&self,
io: &IO,
out: &Utf8Path,
) -> Result<BeamCompilerInner, Error> {
let escript_path = paths::compilation_escript_path(out);
let escript_source = std::include_str!("../templates/gleam@@compile.erl");
io.write(&escript_path, escript_source)?;
tracing::trace!(escript_path=?escript_path, "spawn_beam_compiler");
let mut process = std::process::Command::new("escript")
.arg(escript_path)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.map_err(|e| match e.kind() {
io::ErrorKind::NotFound => Error::ShellProgramNotFound {
program: "escript".into(),
os: get_os(),
},
other => Error::ShellCommand {
program: "escript".into(),
reason: ShellCommandFailureReason::IoError(other),
},
})?;
let stdin = process.stdin.take().expect("could not get child stdin");
let stdout = process.stdout.take().expect("could not get child stdout");
Ok(BeamCompilerInner {
process,
stdin,
stdout: BufReader::new(stdout),
})
}
}
impl Drop for BeamCompiler {
fn drop(&mut self) {
if let Some(mut inner) = self.inner.take() {
// closing stdin will cause the erlang process to exit.
drop(inner.stdin);
let _ = inner.process.wait();
}
}
}
fn escape_path<T: AsRef<str>>(path: T) -> String {
path.as_ref().replace("\\", "\\\\")
}