refactor: ♻️ Use cargo-binstall when possible

This commit is contained in:
Sergio Gasquez 2022-09-26 08:08:16 +00:00
parent 7e85b504a6
commit 548245c136
2 changed files with 69 additions and 48 deletions

View File

@ -3,8 +3,7 @@ use crate::espidf::{get_install_path, get_tool_path, get_tools_path, EspIdfRepo}
use crate::gcc_toolchain::install_gcc_targets;
use crate::llvm_toolchain::LlvmToolchain;
use crate::rust_toolchain::{
check_rust_installation, get_rust_crate, get_rustup_home, install_crate, RustCrate,
RustToolchain,
check_rust_installation, get_rust_crate, get_rustup_home, RustCrate, RustToolchain,
};
use crate::utils::{
clear_dist_folder, export_environment, logging::initialize_logger, parse_targets,
@ -193,7 +192,7 @@ fn install(args: InstallOpts) -> Result<()> {
}
for extra_crate in extra_crates {
install_crate(extra_crate)?;
extra_crate.install()?;
}
if args.profile_minimal {

View File

@ -8,6 +8,7 @@ use crate::utils::{download_file, get_home_dir};
use anyhow::{bail, Result};
use embuild::cmd;
use log::{info, warn};
use std::fmt::Debug;
use std::{env, path::PathBuf, process::Stdio};
const DEFAULT_XTENSA_RUST_REPOSITORY: &str =
@ -171,6 +172,7 @@ pub struct BinstallCrate {
/// Crate destination.
pub fmt: String,
}
#[derive(Debug, Clone)]
pub struct RustCrate {
/// Crate name.
@ -179,6 +181,32 @@ pub struct RustCrate {
pub binstall: Option<BinstallCrate>,
}
impl RustCrate {
/// Installs an extra crate.
pub fn install(&self) -> Result<()> {
cmd!("cargo", "install", "cargo-binstall").run()?;
info!("{} Installing {} crate", emoji::WRENCH, self.name);
if let Some(binstall) = &self.binstall {
cmd!(
"cargo",
"binstall",
"--no-confirm",
"--pkg-url",
&binstall.url,
"--pkg-fmt",
&binstall.fmt,
"--bin-dir",
&binstall.bin,
&self.name
)
.run()?;
} else {
cmd!("cargo", "install", &self.name).run()?;
}
Ok(())
}
}
/// Gets the artifact extension based on the host architecture.
fn get_artifact_extension(host_triple: &str) -> &str {
match host_triple {
@ -236,7 +264,7 @@ pub fn check_rust_installation(nightly_version: &str) -> Result<()> {
/// Retuns the RustCrate from a given name.
pub fn get_rust_crate(name: &str) -> RustCrate {
// match name {
match name {
// "ldproxy" => {
// RustCrate {
// name: name.to_string(),
@ -257,35 +285,29 @@ pub fn get_rust_crate(name: &str) -> RustCrate {
// }),
// }
// }
// "cargo-generate" => {
// },
"cargo-generate" => {
RustCrate {
name: name.to_string() + "@0.15.2",
binstall: Some(BinstallCrate {
url: "{ repo }/releases/download/v{ version }/{ name}-{ version }-{ target }.{ archive-format }".to_string(),
bin: "{ bin }{ binary-ext }".to_string(),
fmt: "tgz".to_string(),
}),
}
}
// "wokwi-server" => {
// },
// "web-flash" => {
// },
// _ => RustCrate {
// name: name.to_string(),
// binstall: None,
// },
// }
RustCrate {
_ => RustCrate {
name: name.to_string(),
binstall: None,
},
}
}
/// Installs an extra crate.
pub fn install_crate(rust_crate: RustCrate) -> Result<()> {
info!("{} Installing {} crate", emoji::WRENCH, rust_crate.name);
cmd!("cargo", "install", rust_crate.name).run()?;
Ok(())
}
/// Installs rustup
fn install_rustup(nightly_version: &str) -> Result<()> {
#[cfg(windows)]