mirror of
https://github.com/lune-org/lune.git
synced 2025-04-04 10:30:54 +01:00
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
This commit is contained in:
parent
7e40851a79
commit
52e71850d1
2 changed files with 114 additions and 101 deletions
211
src/cli/build.rs
211
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<Path>, bytes: impl AsRef<[u8]
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_base_exe_path(
|
||||
base: Option<PathBuf>,
|
||||
target: Option<String>,
|
||||
output_path: &mut PathBuf,
|
||||
) -> Option<PathBuf> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue