serai_docker_tests/
lib.rs

1use std::{
2  sync::{Mutex, OnceLock},
3  collections::HashMap,
4  env,
5};
6
7static BUILT: OnceLock<Mutex<HashMap<String, bool>>> = OnceLock::new();
8pub fn build(name: String) {
9  let built = BUILT.get_or_init(|| Mutex::new(HashMap::new()));
10  // Only one call to build will acquire this lock
11  let mut built_lock = built.lock().unwrap();
12  if built_lock.contains_key(&name) {
13    // If it was built, return
14    return;
15  }
16
17  // Else, hold the lock while we build
18  let mut path = env::current_exe().unwrap();
19  path.pop();
20  assert!(path.as_path().ends_with("deps"));
21  path.pop();
22  assert!(path.as_path().ends_with("debug"));
23  path.pop();
24  assert!(path.as_path().ends_with("target"));
25  path.pop();
26  path.push("deploy");
27
28  println!("Building {}...", &name);
29
30  assert!(std::process::Command::new("docker")
31    .current_dir(path)
32    .arg("compose")
33    .arg("build")
34    .arg(&name)
35    .spawn()
36    .unwrap()
37    .wait()
38    .unwrap()
39    .success());
40
41  println!("Built!");
42
43  // Set built
44  built_lock.insert(name, true);
45}