-
Notifications
You must be signed in to change notification settings - Fork 193
/
build.rs
177 lines (150 loc) · 6.4 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
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
177
use std::process::Command;
use std::path::{Path, PathBuf};
use std::fs::{self, File};
use std::io::{self, Write};
use std::env;
use std::ffi::OsString;
fn grab_paths< P: AsRef< Path > >( path: P, output: &mut Vec< PathBuf > ) {
let path = path.as_ref();
let entries = match fs::read_dir( path ) {
Ok( entries ) => entries,
Err( _ ) => return
};
for entry in entries {
let entry = match entry {
Ok( entry ) => entry,
_ => continue
};
output.push( entry.path().into() );
}
}
fn check_command( command: &str ) -> bool {
match Command::new( command ).args( &[ "--version" ] ).status() {
Err( ref error ) if error.kind() == io::ErrorKind::NotFound => {
false
},
Err( error ) => {
panic!( "Cannot launch `{}`: {}", command, error );
},
Ok( _ ) => true
}
}
fn main() {
let src_out_dir: PathBuf = env::var_os( "OUT_DIR" ).expect( "missing OUT_DIR" ).into();
let crate_root: PathBuf = env::var_os( "CARGO_MANIFEST_DIR" ).expect( "missing CARGO_MANIFEST_DIR" ).into();
let target_dir: PathBuf = env::var_os( "CARGO_TARGET_DIR" ).map( |directory| directory.into() ).unwrap_or( crate_root.join( ".." ).join( "target" ) );
struct Lock {
semaphore: Option< semalock::Semalock >
}
impl Drop for Lock {
fn drop( &mut self ) {
let _ = self.semaphore.take().unwrap().unlink();
}
}
let _ = std::fs::create_dir_all( &target_dir );
let webui_dir = crate_root.join( ".." ).join( "webui" );
let webui_out_dir = webui_dir.join( "dist" );
{
let mut paths: Vec< PathBuf > = Vec::new();
paths.push( webui_dir.join( ".babelrc" ) );
paths.push( webui_dir.join( "node_modules" ) );
paths.push( webui_dir.join( "package.json" ) );
grab_paths( webui_dir.join( "src" ), &mut paths );
for path in paths {
println!( "cargo:rerun-if-changed={}", path.to_str().unwrap() );
}
}
let lock_path = target_dir.join( ".webui-lock" );
let mut lock = Lock {
semaphore: Some( semalock::Semalock::new( &lock_path ).expect( "failed to acquire a semaphore" ) )
};
lock.semaphore.as_mut().unwrap().with( move |_| {
let _ = fs::remove_dir_all( &webui_out_dir );
let mut yarn = "yarn";
if !check_command( yarn ) {
yarn = "yarnpkg";
if !check_command( yarn ) {
panic!( "Yarn not found; you need to install it before you can build the WebUI" );
}
}
if !check_command( "nodejs" ) {
if !check_command( "node" ) {
panic!( "Node.js not found; you need to install it before you can build the WebUI" );
}
}
if !webui_dir.join( "node_modules" ).exists() {
let mut child = Command::new( yarn )
.args( &[ "install" ] )
.current_dir( &webui_dir )
.spawn()
.expect( "cannot launch a child process to install the dependencies for the WebUI" );
match child.wait() {
Err( _ ) => {
panic!( "Failed to install the dependencies for the WebUI!" );
},
Ok( status ) if !status.success() => {
panic!( "Failed to install the dependencies for the WebUI; child process exited with error code {:?}! You might want to try to run 'rm -Rf ~/.cache/yarn' and try again.", status.code() );
},
Ok( _ ) => {}
}
}
assert!( webui_dir.join( "node_modules" ).exists() );
let yarn_supports_bin = Command::new( "/bin/sh" )
.args( &[ String::from( "-c" ), format!( "{} bin", yarn ) ] )
.current_dir( &webui_dir )
.status()
.expect( "cannot launch a child process to check whether Yarn supports the 'bin' subcommand" )
.success();
let bin_path: PathBuf =
if !yarn_supports_bin {
println!( "cargo:warning=You're using an ancient version of Yarn; this is unsupported" );
"node_modules/.bin".into()
} else {
let bin_path = Command::new( yarn )
.args( &[ "bin", "parcel" ] )
.current_dir( &webui_dir )
.output()
.expect( "cannot launch a child process to get Yarn's bin directory" )
.stdout;
let path = String::from_utf8(bin_path).unwrap();
let bin_path = path.trim();
bin_path.into()
};
let mut child = Command::new( bin_path )
.args( &[
"build".into(),
"src/index.html".into(),
"--dist-dir".into(),
OsString::from( webui_out_dir.clone() )
])
.current_dir( &webui_dir )
.spawn()
.expect( "cannot launch a child process to build the WebUI" );
match child.wait() {
Err( _ ) => {
panic!( "Failed to build WebUI!" );
},
Ok( status ) if !status.success() => {
panic!( "Failed to build WebUI; child process exited with error code {:?}!", status.code() );
},
Ok( _ ) => {}
}
let webui_out_dir = webui_out_dir.canonicalize().unwrap();
let mut assets: Vec< PathBuf > = Vec::new();
grab_paths( &webui_out_dir, &mut assets );
assert!( !assets.is_empty() );
let mut fp = File::create( src_out_dir.join( "webui_assets.rs" ) ).unwrap();
writeln!( fp, "#[cfg(not(test))]" ).unwrap();
writeln!( fp, "static WEBUI_ASSETS: &'static [(&'static str, &'static [u8])] = &[" ).unwrap();
for asset in &assets {
let target_path = asset.canonicalize().unwrap();
let key = target_path.strip_prefix( &webui_out_dir ).unwrap();
writeln!( fp, r#" ("{}", include_bytes!( "{}" )),"#, key.to_str().unwrap(), target_path.to_str().unwrap() ).unwrap();
println!( "cargo:rerun-if-changed={}", target_path.to_str().unwrap() );
}
writeln!( fp, "];" ).unwrap();
writeln!( fp, "#[cfg(test)]" ).unwrap();
writeln!( fp, "static WEBUI_ASSETS: &'static [(&'static str, &'static [u8])] = &[" ).unwrap();
writeln!( fp, "];" ).unwrap();
}).unwrap();
}