From 52e71850d10748eba867fe0a8679f4c00c8f8b3f Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sun, 10 Mar 2024 15:59:51 +0530 Subject: [PATCH] refactor: appease the clippy overlords This commit addresses various lint issues, mainly: * clippy flagging once_cell::Lazy variables consts * other simple fixes in the build command --- src/cli/build.rs | 211 +++++++++++++++++++++++++---------------------- src/main.rs | 4 +- 2 files changed, 114 insertions(+), 101 deletions(-) diff --git a/src/cli/build.rs b/src/cli/build.rs index dc35693..c3d31f7 100644 --- a/src/cli/build.rs +++ b/src/cli/build.rs @@ -54,106 +54,8 @@ impl BuildCommand { .await .context("failed to read input file")?; - let base_exe_path = if self.base.is_some() { - output_path.set_extension( - self.base - .clone() - .unwrap() - .extension() - .expect("failed to get extension of base binary"), - ); - - self.base - } else if let Some(target_inner) = self.target { - let target_exe_extension = match target_inner.as_str() { - "windows-x86_64" => "exe", - _ => "bin", - }; - - let path = - TARGET_BASE_DIR.join(format!("lune-{}.{}", target_inner, target_exe_extension)); - - output_path.set_extension(if target_exe_extension == "bin" { - "" - } else { - target_exe_extension - }); - - if !TARGET_BASE_DIR.exists() { - fs::create_dir_all(TARGET_BASE_DIR.to_path_buf()).await?; - } - - if !path.exists() { - println!("Requested target hasn't been downloaded yet, attempting to download"); - - let release_url = format!( - "https://github.com/lune-org/lune/releases/download/v{ver}/lune-{ver}-{target}.zip", - ver = env!("CARGO_PKG_VERSION"), - target = target_inner - ); - - let target_full_display = release_url - .split("/") - .last() - .or(Some("lune-UNKNOWN-UNKNOWN")) - .unwrap() - .replace("zip", target_exe_extension); - - println!( - "{} target {}", - style("Download").green().bold(), - target_full_display - ); - - // Maybe we should use the custom net client used in `@lune/net` - let dl_req = match reqwest::get(release_url).await { - Err(_) => { - eprintln!( - " {} Unable to download base binary found for target `{}`", - style("Download").red().bold(), - target_inner, - ); - - return Ok(ExitCode::FAILURE); - } - Ok(resp) => { - let resp_status = resp.status(); - - if resp_status != 200 && !resp_status.is_redirection() { - eprintln!( - " {} No precompiled base binary found for target `{}`", - style("Download").red().bold(), - target_inner - ); - - println!("{}: {}", style("HINT").yellow(), style("Perhaps try providing a path to self-compiled target with the `--base` flag").italic()); - - return Ok(ExitCode::FAILURE); - } - - resp - } - }; - - fs::OpenOptions::new() - .write(true) - .create(true) - .open(&path) - .await? - .write_all(&dl_req.bytes().await?) - .await?; - - println!( - " {} {}", - style("Downloaded").blue(), - style(target_full_display).underlined() - ) - } - - Some(path) - } else { - None - }; + // Dynamically derive the base executable path based on the CLI arguments provided + let base_exe_path = get_base_exe_path(self.base, self.target, &mut output_path).await; // Read the contents of the lune interpreter as our starting point println!( @@ -191,3 +93,112 @@ async fn write_executable_file_to(path: impl AsRef, bytes: impl AsRef<[u8] Ok(()) } + +async fn get_base_exe_path( + base: Option, + target: Option, + output_path: &mut PathBuf, +) -> Option { + if base.is_some() { + output_path.set_extension( + base.clone() + .unwrap() + .extension() + .expect("failed to get extension of base binary"), + ); + + base + } else if let Some(target_inner) = target { + let target_exe_extension = match target_inner.as_str() { + "windows-x86_64" => "exe", + _ => "bin", + }; + + let path = TARGET_BASE_DIR.join(format!("lune-{}.{}", target_inner, target_exe_extension)); + + output_path.set_extension(if target_exe_extension == "bin" { + "" + } else { + target_exe_extension + }); + + if !TARGET_BASE_DIR.exists() { + fs::create_dir_all(TARGET_BASE_DIR.to_path_buf()) + .await + .ok()?; + } + + if !path.exists() { + println!("Requested target hasn't been downloaded yet, attempting to download"); + + let release_url = format!( + "https://github.com/lune-org/lune/releases/download/v{ver}/lune-{ver}-{target}.zip", + ver = env!("CARGO_PKG_VERSION"), + target = target_inner + ); + + let target_full_display = release_url + .split('/') + .last() + .unwrap_or("lune-UNKNOWN-UNKNOWN") + .replace("zip", target_exe_extension); + + println!( + "{} target {}", + style("Download").green().bold(), + target_full_display + ); + + // Maybe we should use the custom net client used in `@lune/net` + let dl_req = match reqwest::get(release_url).await { + Err(_) => { + eprintln!( + " {} Unable to download base binary found for target `{}`", + style("Download").red().bold(), + target_inner, + ); + + return None; + } + Ok(resp) => { + let resp_status = resp.status(); + + if resp_status != 200 && !resp_status.is_redirection() { + eprintln!( + " {} No precompiled base binary found for target `{}`", + style("Download").red().bold(), + target_inner + ); + + println!("{}: {}", style("HINT").yellow(), style("Perhaps try providing a path to self-compiled target with the `--base` flag").italic()); + + return None; + } + + resp + } + }; + + fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&path) + .await + .ok()? + .write_all(&dl_req.bytes().await.ok()?) + .await + .ok()?; + + println!( + " {} {}", + style("Downloaded").blue(), + style(target_full_display).underlined() + ); + } + + Some(path) + } else { + None + } +} diff --git a/src/main.rs b/src/main.rs index ca28c76..734436d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,9 @@ clippy::match_bool, clippy::module_name_repetitions, clippy::multiple_crate_versions, - clippy::needless_pass_by_value + clippy::needless_pass_by_value, + clippy::declare_interior_mutable_const, + clippy::borrow_interior_mutable_const )] use std::process::ExitCode;