mirror of
https://github.com/esp-rs/espup.git
synced 2025-10-03 07:34:57 +00:00
refactor: ♻️ Avoid hardcoded default values for arguments
This commit is contained in:
parent
9832b3a610
commit
dcfd40e307
52
src/main.rs
52
src/main.rs
@ -7,10 +7,11 @@ use crate::utils::*;
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
||||||
use log::{info, warn};
|
use log::info;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
mod chip;
|
mod chip;
|
||||||
mod emoji;
|
mod emoji;
|
||||||
@ -21,6 +22,11 @@ mod rust_toolchain;
|
|||||||
mod toolchain;
|
mod toolchain;
|
||||||
mod utils;
|
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)]
|
#[derive(Parser)]
|
||||||
struct Opts {
|
struct Opts {
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
@ -46,32 +52,32 @@ pub struct InstallOpts {
|
|||||||
pub build_target: String,
|
pub build_target: String,
|
||||||
/// Path to .cargo.
|
/// 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())))]
|
// 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")]
|
#[clap(short = 'c', long, required = false)]
|
||||||
pub cargo_home: PathBuf,
|
pub cargo_home: Option<PathBuf>,
|
||||||
/// Toolchain instalation folder.
|
/// Toolchain instalation folder.
|
||||||
#[clap(short = 'd', long, default_value = "/home/esp/.rustup/toolchains/esp")]
|
#[clap(short = 'd', long, required = false)]
|
||||||
pub toolchain_destination: PathBuf,
|
pub toolchain_destination: Option<PathBuf>,
|
||||||
/// Comma or space list of extra crates to install.
|
/// Comma or space list of extra crates to install.
|
||||||
// Make it vector and have splliter =" "
|
// Make it vector and have splliter =" "
|
||||||
#[clap(short = 'e', long, default_value = "cargo-espflash")]
|
#[clap(short = 'e', long, default_value = "cargo-espflash")]
|
||||||
pub extra_crates: String,
|
pub extra_crates: String,
|
||||||
/// Destination of the export file generated.
|
/// Destination of the export file generated.
|
||||||
#[clap(short = 'f', long)]
|
#[clap(short = 'f', long, required = false)]
|
||||||
pub export_file: Option<PathBuf>,
|
pub export_file: Option<PathBuf>,
|
||||||
/// LLVM version. [13, 14, 15]
|
/// LLVM version. [13, 14, 15]
|
||||||
#[clap(short = 'l', long, default_value = "14")]
|
#[clap(short = 'l', long, default_value = "14")]
|
||||||
pub llvm_version: String,
|
pub llvm_version: String,
|
||||||
/// [Only applies if using -s|--esp-idf-version]. Deletes some esp-idf folders to save space.
|
/// [Only applies if using -s|--esp-idf-version]. Deletes some esp-idf folders to save space.
|
||||||
#[clap(short = 'm', long)]
|
#[clap(short = 'm', long, requires = "espidf_version")]
|
||||||
pub minified_espidf: Option<bool>,
|
pub minified_espidf: bool,
|
||||||
/// Nightly Rust toolchain version.
|
/// Nightly Rust toolchain version.
|
||||||
#[clap(short = 'n', long, default_value = "nightly")]
|
#[clap(short = 'n', long, default_value = "nightly")]
|
||||||
pub nightly_version: String,
|
pub nightly_version: String,
|
||||||
// /// Path to .rustup.
|
// /// Path to .rustup.
|
||||||
#[clap(short = 'r', long, default_value = "/home/esp/.rustup")]
|
#[clap(short = 'r', long, required = false)]
|
||||||
pub rustup_home: PathBuf,
|
pub rustup_home: Option<PathBuf>,
|
||||||
// /// ESP-IDF branch to install. If empty, no esp-idf is installed.
|
// /// 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>,
|
pub espidf_version: Option<String>,
|
||||||
/// Xtensa Rust toolchain version.
|
/// Xtensa Rust toolchain version.
|
||||||
#[clap(short = 't', long, default_value = "1.62.1.0")]
|
#[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);
|
info!("{} Installing esp-rs", emoji::DISC);
|
||||||
let arch = guess_host_triple::guess_host_triple().unwrap();
|
let arch = guess_host_triple::guess_host_triple().unwrap();
|
||||||
let targets: Vec<Chip> = parse_targets(&args.build_target).unwrap();
|
let targets: Vec<Chip> = parse_targets(&args.build_target).unwrap();
|
||||||
|
|
||||||
print_arguments(&args, arch, &targets);
|
print_arguments(&args, arch, &targets);
|
||||||
|
|
||||||
let rust_toolchain = RustToolchain::new(&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)?;
|
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()?;
|
rust_toolchain.install_xtensa()?;
|
||||||
}
|
|
||||||
|
|
||||||
llvm.install()?;
|
llvm.install()?;
|
||||||
exports.push(format!("export LIBCLANG_PATH=\"{}\"", &llvm.get_lib_path()));
|
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() {
|
if args.espidf_version.is_some() {
|
||||||
let espidf_version = args.espidf_version.unwrap();
|
let espidf_version = args.espidf_version.unwrap();
|
||||||
let espidf = EspIdf::new(
|
let espidf = EspIdf::new(&espidf_version, args.minified_espidf, targets);
|
||||||
&espidf_version,
|
|
||||||
args.minified_espidf.unwrap_or(false),
|
|
||||||
targets,
|
|
||||||
);
|
|
||||||
espidf.install()?;
|
espidf.install()?;
|
||||||
exports.push(format!("export IDF_TOOLS_PATH=\"{}\"", get_tools_path()));
|
exports.push(format!("export IDF_TOOLS_PATH=\"{}\"", get_tools_path()));
|
||||||
// TODO: Fix the export path
|
// TODO: Fix the export path
|
||||||
@ -164,13 +157,14 @@ fn install(args: InstallOpts) -> Result<()> {
|
|||||||
for e in exports.iter() {
|
for e in exports.iter() {
|
||||||
info!("{}", e);
|
info!("{}", e);
|
||||||
}
|
}
|
||||||
if args.export_file.is_some() {
|
let export_file = args
|
||||||
let mut file = File::create(args.export_file.unwrap())?;
|
.export_file
|
||||||
|
.unwrap_or_else(|| PathBuf::from_str(DEFAULT_EXPORT_FILE).unwrap());
|
||||||
|
let mut file = File::create(export_file)?;
|
||||||
for e in exports.iter() {
|
for e in exports.iter() {
|
||||||
file.write_all(e.as_bytes())?;
|
file.write_all(e.as_bytes())?;
|
||||||
file.write_all(b"\n")?;
|
file.write_all(b"\n")?;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ use crate::emoji;
|
|||||||
use crate::utils::{download_file, get_dist_path};
|
use crate::utils::{download_file, get_dist_path};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use embuild::cmd;
|
use embuild::cmd;
|
||||||
use log::info;
|
use log::{info, warn};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
const DEFAULT_XTENSA_RUST_REPOSITORY: &str =
|
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.
|
/// Gets the installer file.
|
||||||
pub fn get_installer(host_triple: &str) -> &str {
|
pub fn get_installer(host_triple: &str) -> &str {
|
||||||
match host_triple {
|
match host_triple {
|
||||||
@ -87,6 +99,16 @@ impl RustToolchain {
|
|||||||
|
|
||||||
/// Installs the Xtensa Rust toolchain.
|
/// Installs the Xtensa Rust toolchain.
|
||||||
pub fn install_xtensa(&self) -> Result<()> {
|
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();
|
let host_triple = guess_host_triple::guess_host_triple().unwrap();
|
||||||
|
|
||||||
// Some platfroms like Windows are available in single bundle rust + src, because install
|
// Some platfroms like Windows are available in single bundle rust + src, because install
|
||||||
@ -149,7 +171,18 @@ impl RustToolchain {
|
|||||||
"{}/v{}/{}",
|
"{}/v{}/{}",
|
||||||
DEFAULT_XTENSA_RUST_REPOSITORY, version, src_dist_file
|
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 {
|
Self {
|
||||||
dist_file,
|
dist_file,
|
||||||
dist_url,
|
dist_url,
|
||||||
@ -158,9 +191,9 @@ impl RustToolchain {
|
|||||||
targets: targets.to_vec(),
|
targets: targets.to_vec(),
|
||||||
extra_crates: args.extra_crates.clone(),
|
extra_crates: args.extra_crates.clone(),
|
||||||
nightly_version: args.nightly_version.clone(),
|
nightly_version: args.nightly_version.clone(),
|
||||||
cargo_home: args.cargo_home.clone(),
|
cargo_home,
|
||||||
rustup_home: args.rustup_home.clone(),
|
rustup_home,
|
||||||
toolchain_destination: args.toolchain_destination.clone(),
|
toolchain_destination,
|
||||||
version,
|
version,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user