forked from emk/subtitles-rs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
94 lines (83 loc) · 2.17 KB
/
build.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
use std::convert::From;
use std::error;
use std::error::Error as StdError;
use std::fmt;
use std::fs;
use std::io;
use std::process::{Command, exit};
use ElmMakeError::{CouldNotRun, Failed};
#[derive(Debug)]
enum ElmMakeError {
CouldNotRun(io::Error),
Failed,
}
impl fmt::Display for ElmMakeError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let desc = self.description();
match self {
&CouldNotRun(ref e) =>
write!(f, "{}: {}\nAre the elm tools installed?", desc, e),
&Failed => write!(f, "{}", desc),
}
}
}
impl error::Error for ElmMakeError {
fn description(&self) -> &str {
match self {
&CouldNotRun(_) => "Could not run `elm make`",
&Failed => "elm-make build failed",
}
}
fn cause(&self) -> Option<&error::Error> {
match self {
&CouldNotRun(ref e) => Some(e),
&Failed => None,
}
}
}
impl From<io::Error> for ElmMakeError {
fn from(err: io::Error) -> ElmMakeError {
CouldNotRun(err)
}
}
fn elm_make_once() -> Result<(), ElmMakeError> {
// Build the command to run.
let mut process = Command::new("elm");
process
.arg("make")
.arg("Main.elm")
.arg("--warn").arg("--yes")
.arg("--output").arg("../assets/elm.js")
.current_dir("ui");
// Try to run the command.
let status = try!(process.status());
if !status.success() {
return Err(Failed);
}
Ok(())
}
fn elm_make() -> Result<(), ElmMakeError> {
if fs::metadata("ui/elm-stuff").is_err() {
println!("Will retry `elm make` if it fails.");
for i in 0..2 {
match elm_make_once() {
Err(Failed) => {
println!("Build {} failed, perhaps because of network.", i);
println!("Retrying...");
continue
}
result => return result,
}
}
}
elm_make_once()
}
fn main() {
match elm_make() {
Ok(()) => {}
Err(ref err) => {
println!("{}", err);
exit(1);
}
}
}