From 5f474c47aa2a06aa8906e41399c6e121c3e417b1 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 5 Apr 2020 01:04:31 +0800 Subject: [PATCH] update buildj --- Cargo.lock | 8 ++--- Cargo.toml | 2 +- src/build_json.rs | 4 +-- src/jdk.rs | 74 ++++++++++++++++++++--------------------------- src/local_util.rs | 12 +++----- src/main.rs | 2 +- src/tool.rs | 25 ++++++---------- 7 files changed, 52 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf3bda1..728843f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,7 +72,7 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", - "rust_util 0.1.0 (git+https://github.com/jht5945/rust_util)", + "rust_util 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "urlencoding 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1008,8 +1008,8 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.1.0" -source = "git+https://github.com/jht5945/rust_util#9c1bed76d1408467ff09a97a2783848751de8ba0" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1602,7 +1602,7 @@ dependencies = [ "checksum reqwest 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)" = "2c2064233e442ce85c77231ebd67d9eca395207dec2127fe0bbedde4bd29a650" "checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf" "checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" -"checksum rust_util 0.1.0 (git+https://github.com/jht5945/rust_util)" = "" +"checksum rust_util 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "35700070bf50662d748fb06926d16d24c1f7d6cfd62ba556dc58541d3c96d436" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" diff --git a/Cargo.toml b/Cargo.toml index 1a31189..659cf7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ urlencoding = "1.0.0" dirs = "2.0.1" rust-crypto = "0.2.36" lazy_static = "1.3.0" -rust_util = { git = "https://github.com/jht5945/rust_util" } +rust_util = "0.2.1" diff --git a/src/build_json.rs b/src/build_json.rs index b6fbebc..32bfcf1 100644 --- a/src/build_json.rs +++ b/src/build_json.rs @@ -125,9 +125,7 @@ pub fn find_build_json_in_parents() -> Option { pub fn find_build_json() -> Option { match find_build_json_in_current() { - Some(p) => { - Some(p) - }, + Some(p) => Some(p), None => match find_build_json_in_parents() { Some(p) => { print_message(MessageType::WARN, &format!("Cannot find {} in current dir, find: {}", BUILD_JSON, p)); diff --git a/src/jdk.rs b/src/jdk.rs index e6b25d6..18c70f8 100644 --- a/src/jdk.rs +++ b/src/jdk.rs @@ -9,6 +9,7 @@ use std::{ use super::{ rust_util::{ + iff, util_os::*, util_msg::*, }, @@ -37,11 +38,7 @@ pub fn get_java_home(version: &str) -> Option { Some(j) => Some(j), None => match get_local_java_home(version) { Some(j) => Some(j), - None => if get_cloud_java(version) { - get_local_java_home(version) - } else { - None - }, + None => iff!(get_cloud_java(version), get_local_java_home(version), None), }, } } @@ -93,53 +90,46 @@ pub fn get_local_java_home(version: &str) -> Option { let local_java_home_base_dir = local_util::get_user_home_dir(LOCAL_JAVA_HOME_BASE_DIR).ok()?; let paths = fs::read_dir(Path::new(&local_java_home_base_dir)).ok()?; for path in paths { - match path { - Err(_) => (), - Ok(dir_entry) => match dir_entry.path().to_str() { - None => (), - Some(p) => { + if let Ok(dir_entry) = path { + if let Some(p)= dir_entry.path().to_str() { + if *VERBOSE { + print_message(MessageType::DEBUG, &format!("Try match path: {}", p)); + } + let mut path_name = p; + if p.ends_with('/') { + path_name = &path_name[..path_name.len() - 1] + } + if let Some(i) = path_name.rfind('/') { + path_name = &path_name[i+1..]; + } + let matched_path_opt = if (path_name.starts_with("jdk-") && (&path_name[4..]).starts_with(version)) + || (path_name.starts_with("jdk") && (&path_name[3..]).starts_with(version)) { + Some(p) + } else { + None + }; + if let Some(matched_path) = matched_path_opt { if *VERBOSE { - print_message(MessageType::DEBUG, &format!("Try match path: {}", p)); + print_message(MessageType::DEBUG, &format!("Matched JDK path found: {}", matched_path)); } - let mut path_name = p; - if p.ends_with('/') { - path_name = &path_name[..path_name.len() - 1] - } - let last_index_of_slash = path_name.rfind('/'); - match last_index_of_slash { - None => (), - Some(i) => path_name = &path_name[i+1..], - }; - let matched_path = if (path_name.starts_with("jdk-") && (&path_name[4..]).starts_with(version)) - || (path_name.starts_with("jdk") && (&path_name[3..]).starts_with(version)) { - p + if local_util::is_path_exists(matched_path, "Contents/Home") { + return Some(format!("{}/{}", matched_path, "Contents/Home")); } else { - "" - }; - if !matched_path.is_empty() { - if *VERBOSE { - print_message(MessageType::DEBUG, &format!("Matched JDK path found: {}", matched_path)); - } - if local_util::is_path_exists(matched_path, "Contents/Home") { - return Some(format!("{}/{}", matched_path, "Contents/Home")); - } else { - return Some(matched_path.to_string()); - } + return Some(matched_path.to_string()); } - }, - }, - }; + } + } + } } None } pub fn extract_jdk_and_wait(file_name: &str) { - match local_util::get_user_home_dir(LOCAL_JAVA_HOME_BASE_DIR) { - Err(_) => (), - Ok(local_java_home_base_dir) => local_util::extract_package_and_wait(&local_java_home_base_dir, file_name).unwrap_or_else(|err| { + if let Ok(local_java_home_base_dir) = local_util::get_user_home_dir(LOCAL_JAVA_HOME_BASE_DIR) { + local_util::extract_package_and_wait(&local_java_home_base_dir, file_name).unwrap_or_else(|err| { print_message(MessageType::ERROR, &format!("Extract file: {}, failed: {}", file_name, err)); - }), - }; + }); + } } pub fn get_env() -> HashMap { diff --git a/src/local_util.rs b/src/local_util.rs index 93bf3ce..c9e0789 100644 --- a/src/local_util.rs +++ b/src/local_util.rs @@ -71,10 +71,7 @@ pub fn calc_sha256(d: &[u8]) -> String { pub fn calc_file_digest(digest: &mut dyn Digest, digest_alg: &str, file_name: &str) -> XResult { let mut buf: [u8; DEFAULT_BUF_SIZE] = [0u8; DEFAULT_BUF_SIZE]; let mut f = File::open(file_name)?; - let file_len = match f.metadata() { - Err(_) => -1_i64, - Ok(meta_data) => meta_data.len() as i64, - }; + let file_len = f.metadata().map(|md| md.len() as i64).unwrap_or(-1_i64); let start = SystemTime::now(); let mut written = 0_i64; loop { @@ -130,10 +127,9 @@ pub fn extract_package_and_wait(dir: &str, file_name: &str) -> XResult<()> { } pub fn init_home_dir(home_sub_dir: &str) { - match get_user_home_dir(home_sub_dir) { - Err(_) => (), - Ok(user_home_dir) => init_dir(&user_home_dir), - }; + if let Ok(user_home_dir) = get_user_home_dir(home_sub_dir) { + init_dir(&user_home_dir); + } } pub fn init_dir(dir: &str) { diff --git a/src/main.rs b/src/main.rs index 4c2a2aa..f0e3723 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ const BUILD_DATE: &str = env!("BUILD_DATE"); fn do_with_buildin_arg_java(first_arg: &str, args: &[String]) { let ver = &first_arg[7..]; - if ver == "" { + if ver.is_empty() { print_message(MessageType::ERROR, "Java version is not assigned!"); return; } diff --git a/src/tool.rs b/src/tool.rs index 604dd0a..91dd8d2 100644 --- a/src/tool.rs +++ b/src/tool.rs @@ -50,11 +50,9 @@ impl BuilderDesc { pub fn get_builder_bin(&self) -> String { match &self.bin { Some(b) => b.clone(), - None => { - match self.name { - BuilderName::Maven => format!("{}/bin/mvn", self.home.clone()), - BuilderName::Gradle => format!("{}/bin/gradle", self.home.clone()), - } + None => match self.name { + BuilderName::Maven => format!("{}/bin/mvn", self.home.clone()), + BuilderName::Gradle => format!("{}/bin/gradle", self.home.clone()), } } } @@ -105,23 +103,18 @@ pub fn get_local_builder_home_sub(builder_name: BuilderName, local_builder_home_ print_message(MessageType::ERROR, &format!("Cannot find builder home in: {}", local_builder_home_dir)); None }, - Some(p) => { - Some(BuilderDesc{name: builder_name, home: p, bin: None}) - }, + Some(p) => Some(BuilderDesc{name: builder_name, home: p, bin: None}), } } pub fn get_local_builder_home_sub_first_sub_dir(local_builder_home_dir: &str) -> Option { let paths = fs::read_dir(Path::new(&local_builder_home_dir)).ok()?; for path in paths { - match path { - Err(_) => (), - Ok(p) => { - if p.path().is_dir() { - return Some(p.path().to_str()?.to_string()); - } - }, - }; + if let Ok(p) = path { + if p.path().is_dir() { + return Some(p.path().to_str()?.to_string()); + } + } } None }