refactor: ♻️ Avoid hardcoded default values for arguments

This commit is contained in:
Sergio Gasquez 2022-09-19 19:20:32 +02:00
parent 9832b3a610
commit dcfd40e307
2 changed files with 65 additions and 38 deletions

View File

@ -7,10 +7,11 @@ use crate::utils::*;
use anyhow::Result;
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use log::{info, warn};
use log::info;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;
mod chip;
mod emoji;
@ -21,6 +22,11 @@ mod rust_toolchain;
mod toolchain;
mod utils;
#[cfg(windows)]
const DEFAULT_EXPORT_FILE: &str = "export-esp.bat";
#[cfg(not(windows))]
const DEFAULT_EXPORT_FILE: &str = "export-esp.sh";
#[derive(Parser)]
struct Opts {
#[clap(subcommand)]
@ -46,32 +52,32 @@ pub struct InstallOpts {
pub build_target: String,
/// Path to .cargo.
// TODO: Use home_dir to make it diferent in every OS: #[clap(short = 'c', long, default_value_t: &'a Path = Path::new(format!("{}/.cargo",home_dir())))]
#[clap(short = 'c', long, default_value = "/home/esp/.cargo")]
pub cargo_home: PathBuf,
#[clap(short = 'c', long, required = false)]
pub cargo_home: Option<PathBuf>,
/// Toolchain instalation folder.
#[clap(short = 'd', long, default_value = "/home/esp/.rustup/toolchains/esp")]
pub toolchain_destination: PathBuf,
#[clap(short = 'd', long, required = false)]
pub toolchain_destination: Option<PathBuf>,
/// Comma or space list of extra crates to install.
// Make it vector and have splliter =" "
#[clap(short = 'e', long, default_value = "cargo-espflash")]
pub extra_crates: String,
/// Destination of the export file generated.
#[clap(short = 'f', long)]
#[clap(short = 'f', long, required = false)]
pub export_file: Option<PathBuf>,
/// LLVM version. [13, 14, 15]
#[clap(short = 'l', long, default_value = "14")]
pub llvm_version: String,
/// [Only applies if using -s|--esp-idf-version]. Deletes some esp-idf folders to save space.
#[clap(short = 'm', long)]
pub minified_espidf: Option<bool>,
#[clap(short = 'm', long, requires = "espidf_version")]
pub minified_espidf: bool,
/// Nightly Rust toolchain version.
#[clap(short = 'n', long, default_value = "nightly")]
pub nightly_version: String,
// /// Path to .rustup.
#[clap(short = 'r', long, default_value = "/home/esp/.rustup")]
pub rustup_home: PathBuf,
#[clap(short = 'r', long, required = false)]
pub rustup_home: Option<PathBuf>,
// /// ESP-IDF branch to install. If empty, no esp-idf is installed.
#[clap(short = 's', long)]
#[clap(short = 's', long, required = false)]
pub espidf_version: Option<String>,
/// Xtensa Rust toolchain version.
#[clap(short = 't', long, default_value = "1.62.1.0")]
@ -107,6 +113,7 @@ fn install(args: InstallOpts) -> Result<()> {
info!("{} Installing esp-rs", emoji::DISC);
let arch = guess_host_triple::guess_host_triple().unwrap();
let targets: Vec<Chip> = parse_targets(&args.build_target).unwrap();
print_arguments(&args, arch, &targets);
let rust_toolchain = RustToolchain::new(&args, arch, &targets);
@ -117,17 +124,7 @@ fn install(args: InstallOpts) -> Result<()> {
check_rust_installation(&args.nightly_version)?;
if args.toolchain_destination.exists() {
warn!(
"{} Previous installation of Rust Toolchain exist in: {}.\n Please, remove the directory before new installation.",
emoji::WARN,
args.toolchain_destination.display()
);
return Ok(());
} else {
info!("{} Installing Xtensa Rust toolchain", emoji::WRENCH);
rust_toolchain.install_xtensa()?;
}
llvm.install()?;
exports.push(format!("export LIBCLANG_PATH=\"{}\"", &llvm.get_lib_path()));
@ -138,11 +135,7 @@ fn install(args: InstallOpts) -> Result<()> {
if args.espidf_version.is_some() {
let espidf_version = args.espidf_version.unwrap();
let espidf = EspIdf::new(
&espidf_version,
args.minified_espidf.unwrap_or(false),
targets,
);
let espidf = EspIdf::new(&espidf_version, args.minified_espidf, targets);
espidf.install()?;
exports.push(format!("export IDF_TOOLS_PATH=\"{}\"", get_tools_path()));
// TODO: Fix the export path
@ -164,13 +157,14 @@ fn install(args: InstallOpts) -> Result<()> {
for e in exports.iter() {
info!("{}", e);
}
if args.export_file.is_some() {
let mut file = File::create(args.export_file.unwrap())?;
let export_file = args
.export_file
.unwrap_or_else(|| PathBuf::from_str(DEFAULT_EXPORT_FILE).unwrap());
let mut file = File::create(export_file)?;
for e in exports.iter() {
file.write_all(e.as_bytes())?;
file.write_all(b"\n")?;
}
}
Ok(())
}

View File

@ -6,7 +6,7 @@ use crate::emoji;
use crate::utils::{download_file, get_dist_path};
use anyhow::Result;
use embuild::cmd;
use log::info;
use log::{info, warn};
use std::path::PathBuf;
const DEFAULT_XTENSA_RUST_REPOSITORY: &str =
@ -46,6 +46,18 @@ impl RustToolchain {
}
}
fn get_default_cargo_home() -> PathBuf {
dirs::home_dir().unwrap().join(".cargo")
}
fn get_default_rustup_home() -> PathBuf {
dirs::home_dir().unwrap().join(".rustup")
}
fn get_default_toolchain_destination() -> PathBuf {
Self::get_default_rustup_home().join("/toolchains/esp")
}
/// Gets the installer file.
pub fn get_installer(host_triple: &str) -> &str {
match host_triple {
@ -87,6 +99,16 @@ impl RustToolchain {
/// Installs the Xtensa Rust toolchain.
pub fn install_xtensa(&self) -> Result<()> {
if self.toolchain_destination.exists() {
warn!(
"{} Previous installation of Rust Toolchain exist in: {}.\n Please, remove the directory before new installation.",
emoji::WARN,
self.toolchain_destination.display()
);
return Ok(());
}
info!("{} Installing Xtensa Rust toolchain", emoji::WRENCH);
let host_triple = guess_host_triple::guess_host_triple().unwrap();
// Some platfroms like Windows are available in single bundle rust + src, because install
@ -149,7 +171,18 @@ impl RustToolchain {
"{}/v{}/{}",
DEFAULT_XTENSA_RUST_REPOSITORY, version, src_dist_file
);
let cargo_home = args
.cargo_home
.clone()
.unwrap_or_else(Self::get_default_cargo_home);
let rustup_home = args
.rustup_home
.clone()
.unwrap_or_else(Self::get_default_rustup_home);
let toolchain_destination = args
.toolchain_destination
.clone()
.unwrap_or_else(Self::get_default_toolchain_destination);
Self {
dist_file,
dist_url,
@ -158,9 +191,9 @@ impl RustToolchain {
targets: targets.to_vec(),
extra_crates: args.extra_crates.clone(),
nightly_version: args.nightly_version.clone(),
cargo_home: args.cargo_home.clone(),
rustup_home: args.rustup_home.clone(),
toolchain_destination: args.toolchain_destination.clone(),
cargo_home,
rustup_home,
toolchain_destination,
version,
}
}