-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
85 lines (70 loc) · 2.38 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
#[cfg(not(feature = "bundled"))]
fn main() {}
#[cfg(feature = "bundled")]
fn main() -> color_eyre::Result<()> {
bundled::build_itext()
}
#[cfg(feature = "bundled")]
mod bundled {
use color_eyre::eyre::Error;
use color_eyre::Result;
use std::env::var;
use std::fs;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use cfg_if::cfg_if;
pub fn build_itext() -> Result<()> {
println!("cargo:rerun-if-changed=bundle");
let manifest_dir = PathBuf::from(var("CARGO_MANIFEST_DIR")?);
run_gradle_command("shadowjar")?;
let builddir = manifest_dir.join("bundle").join("build").join("libs");
let outjar = fs::read_dir(builddir)?
.into_iter()
.find(|entry| {
let entry = match entry {
Ok(e) => e,
Err(_) => return false,
};
let fname = entry.file_name();
let fname = fname.to_string_lossy();
fname.starts_with("bundle") && fname.ends_with("all.jar")
})
.and_then(|x| x.ok())
.ok_or(Error::msg("Could not find output jar"))?
.path();
let outdir = PathBuf::from(var("OUT_DIR")?);
fs::copy(outjar, outdir.join("dependencies.jar"))?;
run_gradle_command("clean")?;
Ok(())
}
fn gradle_command_name() -> &'static str {
cfg_if! {
if #[cfg(unix)] {
"./gradlew"
} else if #[cfg(windows)] {
"./gradlew.bat"
} else {
compiler_error!("Platform not supported");
}
}
}
fn run_gradle_command(cmd: &str) -> Result<()> {
let manifest_dir = PathBuf::from(var("CARGO_MANIFEST_DIR")?);
let output = Command::new(gradle_command_name())
.arg(cmd)
.current_dir(manifest_dir.join("bundle").canonicalize()?)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?
.wait_with_output()?;
if output.status.success() {
Ok(())
} else {
let stdout = String::from_utf8(output.stdout)?;
eprintln!("{stdout}");
let stderr = String::from_utf8(output.stderr)?;
eprintln!("{stderr}");
Err(Error::msg("Building Java dependency failed"))
}
}
}