Skip to content

Commit 48eef17

Browse files
committed
Merge branch 'cleanup_build.rs' of github.com:rukai/j4rs into rukai-cleanup_build.rs
2 parents 989b24a + fbe9315 commit 48eef17

File tree

2 files changed

+38
-85
lines changed

2 files changed

+38
-85
lines changed

rust/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ futures = "0.3"
3535

3636
[build-dependencies]
3737
fs_extra = "1.3"
38-
dirs = "5.0"
3938
glob = "0.3"
40-
java-locator = "0.1"
41-
sha2 = "0.10"
4239

4340
[dev-dependencies]
4441
criterion = { version = "0.5", features = ["html_reports"] }

rust/build.rs

Lines changed: 38 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,29 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
extern crate dirs;
15-
extern crate fs_extra;
1614

1715
use std::error::Error;
1816
use std::fmt;
19-
#[allow(unused_imports)]
20-
use std::fs::{File, OpenOptions};
21-
use std::io::prelude::*;
2217
use std::path::{Path, PathBuf};
2318
use std::{env, fs};
2419

20+
use fs_extra::dir::CopyOptions;
2521
use glob::glob;
26-
use java_locator;
27-
use sha2::{Digest, Sha256};
2822

2923
// This is the version of the jar that should be used
30-
const VERSION: &'static str = "0.20.0-SNAPSHOT";
31-
const JAVA_FX_VERSION: &'static str = "21.0.2";
24+
const VERSION: &str = "0.20.0-SNAPSHOT";
25+
const JAVA_FX_VERSION: &str = "21.0.2";
3226

3327
fn main() -> Result<(), J4rsBuildError> {
3428
let out_dir = env::var("OUT_DIR")?;
35-
let source_jar_location = format!("../java/target/j4rs-{}-jar-with-dependencies.jar", VERSION);
36-
if File::open(&source_jar_location).is_ok() {
37-
println!("cargo:rerun-if-changed={}", source_jar_location);
29+
let source_jar_location = PathBuf::from(format!(
30+
"../java/target/j4rs-{VERSION}-jar-with-dependencies.jar"
31+
));
32+
if Path::new(&source_jar_location).exists() {
33+
println!(
34+
"cargo:rerun-if-changed={}",
35+
source_jar_location.to_string_lossy()
36+
);
3837
}
3938

4039
let target_os_res = env::var("CARGO_CFG_TARGET_OS");
@@ -55,76 +54,53 @@ fn main() -> Result<(), J4rsBuildError> {
5554

5655
fn generate_src(out_dir: &str) -> Result<(), J4rsBuildError> {
5756
let dest_path = Path::new(&out_dir).join("j4rs_init.rs");
58-
let mut f = File::create(&dest_path)?;
59-
6057
let contents = format!(
6158
"
6259
pub(crate) fn j4rs_version() -> &'static str {{
63-
\"{}\"
60+
\"{VERSION}\"
6461
}}
6562
6663
pub(crate) fn java_fx_version() -> &'static str {{
67-
\"{}\"
64+
\"{JAVA_FX_VERSION}\"
6865
}}
69-
",
70-
VERSION,
71-
JAVA_FX_VERSION
66+
"
7267
);
73-
74-
f.write_all(contents.as_bytes())?;
68+
std::fs::write(dest_path, contents)?;
7569
Ok(())
7670
}
7771

7872
// Copies the jars from the `java` directory to the source directory of rust.
79-
fn copy_jars_from_java(jar_source_path: &str) -> Result<(), J4rsBuildError> {
80-
if let Ok(mut source_jar_file) = File::open(&jar_source_path) {
73+
fn copy_jars_from_java(jar_source_path: &Path) -> Result<(), J4rsBuildError> {
74+
if jar_source_path.exists() {
8175
// Find the destination file
8276
let home = env::var("CARGO_MANIFEST_DIR")?;
8377
let jassets_path_buf = Path::new(&home).join("jassets");
8478
let jassets_path = jassets_path_buf.to_str().unwrap().to_owned();
8579

86-
let destination_jar_file_res = {
87-
let djpb = Path::new(&jassets_path)
88-
.join(format!("j4rs-{}-jar-with-dependencies.jar", VERSION));
89-
File::open(djpb)
90-
};
80+
let destination_jar_file =
81+
Path::new(&jassets_path).join(format!("j4rs-{VERSION}-jar-with-dependencies.jar"));
9182

9283
// Copy only if the files are not the same
93-
let do_copy = if destination_jar_file_res.is_ok() {
94-
let mut destination_jar_file = destination_jar_file_res.unwrap();
95-
!are_same_files(&mut source_jar_file, &mut destination_jar_file).unwrap_or(true)
84+
let do_copy = if destination_jar_file.exists() {
85+
!are_same_files(&jar_source_path, &destination_jar_file).unwrap_or(true)
9686
} else {
9787
true
9888
};
9989

10090
if do_copy {
101-
fs_extra::remove_items(vec![jassets_path.clone()].as_ref())?;
91+
fs_extra::remove_items(&[&jassets_path])?;
10292

103-
let _ = fs::create_dir_all(jassets_path_buf.clone())
104-
.map_err(|error| panic!("Cannot create dir '{:?}': {:?}", jassets_path_buf, error));
93+
let _ = fs::create_dir_all(&jassets_path_buf)
94+
.map_err(|error| panic!("Cannot create dir '{jassets_path_buf:?}': {error:?}"));
10595

106-
let ref options = fs_extra::dir::CopyOptions::new();
107-
let _ = fs_extra::copy_items(vec![jar_source_path].as_ref(), jassets_path, options)?;
96+
fs_extra::copy_items(&[jar_source_path], jassets_path, &CopyOptions::new())?;
10897
}
10998
}
11099
Ok(())
111100
}
112101

113-
fn are_same_files(f1: &mut File, f2: &mut File) -> Result<bool, J4rsBuildError> {
114-
let mut buffer1: Vec<u8> = Vec::new();
115-
let mut hasher1 = Sha256::new();
116-
let mut buffer2: Vec<u8> = Vec::new();
117-
let mut hasher2 = Sha256::new();
118-
119-
f1.read_to_end(&mut buffer1)?;
120-
hasher1.update(&buffer1);
121-
let hash1 = hasher1.finalize();
122-
123-
f2.read_to_end(&mut buffer2)?;
124-
hasher2.update(&buffer2);
125-
let hash2 = hasher2.finalize();
126-
127-
Ok(hash1 == hash2)
102+
fn are_same_files(path1: &Path, path2: &Path) -> Result<bool, J4rsBuildError> {
103+
Ok(std::fs::read(path1)? == std::fs::read(path2)?)
128104
}
129105

130106
// Copies the jars to and returns the PathBuf of the exec directory.
@@ -134,43 +110,31 @@ fn copy_jars_to_exec_directory(out_dir: &str) -> Result<PathBuf, J4rsBuildError>
134110
exec_dir_path_buf.pop();
135111
exec_dir_path_buf.pop();
136112

137-
let jassets_output = exec_dir_path_buf.clone();
138-
let jassets_output_dir = jassets_output.to_str().unwrap();
113+
let jassets_output_dir = exec_dir_path_buf.to_str().unwrap();
139114

140115
let home = env::var("CARGO_MANIFEST_DIR")?;
141116
let jassets_path_buf = Path::new(&home).join("jassets");
142117
let jassets_path = jassets_path_buf.to_str().unwrap().to_owned();
143118

144-
let jassets_jar_file_res = {
145-
let japb =
146-
Path::new(&jassets_path).join(format!("j4rs-{}-jar-with-dependencies.jar", VERSION));
147-
File::open(japb)
148-
};
149-
let jassets_output_file_res = {
150-
let jaopb = Path::new(&jassets_output_dir)
151-
.join("jassets")
152-
.join(format!("j4rs-{}-jar-with-dependencies.jar", VERSION));
153-
File::open(jaopb)
154-
};
119+
let jassets_jar_file =
120+
Path::new(&jassets_path).join(format!("j4rs-{VERSION}-jar-with-dependencies.jar"));
121+
let jassets_output_file = Path::new(&jassets_output_dir)
122+
.join("jassets")
123+
.join(format!("j4rs-{VERSION}-jar-with-dependencies.jar"));
155124

156125
// Delete the target jassets and copy only if the files are not the same
157-
let do_copy = if jassets_jar_file_res.is_ok() && jassets_output_file_res.is_ok() {
158-
let mut jassets_jar_file = jassets_jar_file_res.unwrap();
159-
let mut jassets_output_jar_file = jassets_output_file_res.unwrap();
160-
161-
!are_same_files(&mut jassets_jar_file, &mut jassets_output_jar_file).unwrap_or(true)
126+
let do_copy = if jassets_jar_file.exists() && jassets_output_file.exists() {
127+
!are_same_files(&jassets_jar_file, &jassets_output_file).unwrap_or(true)
162128
} else {
163129
true
164130
};
165131

166132
if do_copy {
167-
fs_extra::remove_items(vec![format!("{}/jassets", jassets_output_dir)].as_ref())?;
168-
169-
let ref options = fs_extra::dir::CopyOptions::new();
170-
let _ = fs_extra::copy_items(vec![jassets_path].as_ref(), jassets_output_dir, options)?;
133+
fs_extra::remove_items(&[format!("{jassets_output_dir}/jassets")])?;
134+
fs_extra::copy_items(&[jassets_path], jassets_output_dir, &CopyOptions::new())?;
171135
}
172136

173-
let jassets_output_files = glob(format!("{}/jassets/**/*", jassets_output_dir).as_str())?;
137+
let jassets_output_files = glob(&format!("{jassets_output_dir}/jassets/**/*"))?;
174138
for glob_res in jassets_output_files {
175139
println!("cargo:rerun-if-changed={}", glob_res?.to_str().unwrap());
176140
}
@@ -210,14 +174,6 @@ impl From<std::io::Error> for J4rsBuildError {
210174
}
211175
}
212176

213-
impl From<java_locator::errors::JavaLocatorError> for J4rsBuildError {
214-
fn from(err: java_locator::errors::JavaLocatorError) -> J4rsBuildError {
215-
J4rsBuildError {
216-
description: format!("{:?}", err),
217-
}
218-
}
219-
}
220-
221177
impl From<fs_extra::error::Error> for J4rsBuildError {
222178
fn from(err: fs_extra::error::Error) -> J4rsBuildError {
223179
J4rsBuildError {

0 commit comments

Comments
 (0)