Skip to content

Commit b1476cf

Browse files
committed
Minor build.rs cleanup
1 parent 7e5aab1 commit b1476cf

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

rust/build.rs

+21-28
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,24 @@ fn main() -> Result<(), J4rsBuildError> {
3131
let source_jar_location = PathBuf::from(format!(
3232
"../java/target/j4rs-{VERSION}-jar-with-dependencies.jar"
3333
));
34-
if Path::new(&source_jar_location).exists() {
34+
if source_jar_location.exists() {
3535
println!(
3636
"cargo:rerun-if-changed={}",
3737
source_jar_location.to_string_lossy()
3838
);
3939
}
40+
generate_src(&out_dir)?;
4041

41-
let target_os_res = env::var("CARGO_CFG_TARGET_OS");
42-
let target_os = target_os_res.as_ref().map(|x| &**x).unwrap_or("unknown");
43-
if target_os == "android" {
44-
generate_src(&out_dir)?;
42+
if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("android") {
43+
// skip the rest of setup on android
4544
return Ok(());
4645
}
4746

4847
// Copy the needed jar files if they are available
4948
// (that is, if the build is done with the full source-code - not in crates.io)
5049
copy_jars_from_java(&source_jar_location)?;
51-
copy_jars_to_exec_directory(&out_dir)?;
52-
generate_src(&out_dir)?;
50+
51+
copy_jars_to_target_directory(&out_dir)?;
5352

5453
Ok(())
5554
}
@@ -75,9 +74,7 @@ pub(crate) fn java_fx_version() -> &'static str {{
7574
fn copy_jars_from_java(jar_source_path: &Path) -> Result<(), J4rsBuildError> {
7675
if jar_source_path.exists() {
7776
// Find the destination file
78-
let home = env::var("CARGO_MANIFEST_DIR")?;
79-
let jassets_path_buf = Path::new(&home).join("jassets");
80-
let jassets_path = jassets_path_buf.to_str().unwrap().to_owned();
77+
let jassets_path = Path::new(&env::var("CARGO_MANIFEST_DIR")?).join("jassets");
8178

8279
let destination_jar_file =
8380
Path::new(&jassets_path).join(format!("j4rs-{VERSION}-jar-with-dependencies.jar"));
@@ -92,8 +89,9 @@ fn copy_jars_from_java(jar_source_path: &Path) -> Result<(), J4rsBuildError> {
9289
if do_copy {
9390
fs_extra::remove_items(&[&jassets_path])?;
9491

95-
let _ = fs::create_dir_all(&jassets_path_buf)
96-
.map_err(|error| panic!("Cannot create dir '{jassets_path_buf:?}': {error:?}"));
92+
fs::create_dir_all(&jassets_path)
93+
.map_err(|error| panic!("Cannot create dir '{jassets_path:?}': {error:?}"))
94+
.ok();
9795

9896
fs_extra::copy_items(&[jar_source_path], jassets_path, &CopyOptions::new())?;
9997
}
@@ -105,22 +103,17 @@ fn are_same_files(path1: &Path, path2: &Path) -> Result<bool, J4rsBuildError> {
105103
Ok(std::fs::read(path1)? == std::fs::read(path2)?)
106104
}
107105

108-
// Copies the jars to the exec directory.
109-
fn copy_jars_to_exec_directory(out_dir: &str) -> Result<(), J4rsBuildError> {
110-
let mut exec_dir_path_buf = PathBuf::from(out_dir);
111-
exec_dir_path_buf.pop();
112-
exec_dir_path_buf.pop();
113-
exec_dir_path_buf.pop();
114-
115-
let jassets_output_dir = exec_dir_path_buf.to_str().unwrap();
106+
// Copies the jars to CARGO_TARGET_DIR/*/jassets
107+
fn copy_jars_to_target_directory(out_dir: &str) -> Result<(), J4rsBuildError> {
108+
let mut target_dir = PathBuf::from(out_dir);
109+
target_dir.pop();
110+
target_dir.pop();
111+
target_dir.pop();
116112

117-
let home = env::var("CARGO_MANIFEST_DIR")?;
118-
let jassets_path_buf = Path::new(&home).join("jassets");
119-
let jassets_path = jassets_path_buf.to_str().unwrap().to_owned();
113+
let jassets_path = Path::new(&env::var("CARGO_MANIFEST_DIR")?).join("jassets");
114+
let jassets_jar_file = jassets_path.join(format!("j4rs-{VERSION}-jar-with-dependencies.jar"));
120115

121-
let jassets_jar_file =
122-
Path::new(&jassets_path).join(format!("j4rs-{VERSION}-jar-with-dependencies.jar"));
123-
let jassets_output_file = Path::new(&jassets_output_dir)
116+
let jassets_output_file = target_dir
124117
.join("jassets")
125118
.join(format!("j4rs-{VERSION}-jar-with-dependencies.jar"));
126119

@@ -132,8 +125,8 @@ fn copy_jars_to_exec_directory(out_dir: &str) -> Result<(), J4rsBuildError> {
132125
};
133126

134127
if do_copy {
135-
fs_extra::remove_items(&[format!("{jassets_output_dir}/jassets")])?;
136-
fs_extra::copy_items(&[jassets_path], jassets_output_dir, &CopyOptions::new())?;
128+
fs_extra::remove_items(&[target_dir.join("jassets")])?;
129+
fs_extra::copy_items(&[jassets_path], target_dir, &CopyOptions::new())?;
137130
}
138131

139132
Ok(())

0 commit comments

Comments
 (0)