refactor: ♻️ Add toolchain module and remove utils.rs

This commit is contained in:
Sergio Gasquez 2022-10-05 12:53:43 +02:00
parent 64bc296d62
commit bc4e620e56
8 changed files with 134 additions and 116 deletions

15
src/lib.rs Normal file
View File

@ -0,0 +1,15 @@
pub mod emoji;
pub mod targets;
pub mod toolchain;
pub mod logging {
use env_logger::{Builder, Env, WriteStyle};
/// Initializes the logger
pub fn initialize_logger(log_level: &str) {
Builder::from_env(Env::default().default_filter_or(log_level))
.format_target(false)
.format_timestamp_secs()
.write_style(WriteStyle::Always)
.init();
}
}

View File

@ -1,26 +1,29 @@
use crate::espidf::{get_install_path, get_tool_path, EspIdfRepo};
use crate::gcc_toolchain::install_gcc_targets;
use crate::llvm_toolchain::LlvmToolchain;
use crate::rust_toolchain::{
check_rust_installation, get_rustup_home, install_riscv_target, RustCrate, RustToolchain,
};
use crate::targets::{parse_targets, Target};
#[cfg(windows)]
use crate::utils::check_arguments;
use crate::utils::{clear_dist_folder, export_environment, logging::initialize_logger};
use anyhow::Result;
use clap::Parser;
use embuild::espidf::{parse_esp_idf_git_ref, EspIdfRemote};
use espup::{
emoji,
logging::initialize_logger,
targets::{parse_targets, Target},
toolchain::{
espidf::{
get_dist_path, get_install_path, get_tool_path, EspIdfRepo, DEFAULT_GIT_REPOSITORY,
},
gcc_toolchain::install_gcc_targets,
llvm_toolchain::LlvmToolchain,
rust_toolchain::{
check_rust_installation, get_rustup_home, install_riscv_target, RustCrate,
RustToolchain,
},
},
};
use log::{debug, info};
use std::{collections::HashSet, fs::remove_dir_all, path::PathBuf};
mod emoji;
mod espidf;
mod gcc_toolchain;
mod llvm_toolchain;
mod rust_toolchain;
mod targets;
mod utils;
use std::{
collections::HashSet,
fs::{remove_dir_all, File},
io::Write,
path::PathBuf,
};
#[cfg(windows)]
const DEFAULT_EXPORT_FILE: &str = "export-esp.ps1";
@ -230,7 +233,7 @@ fn uninstall(args: UninstallOpts) -> Result<()> {
info!("{} Deleting ESP-IDF", emoji::WRENCH);
let repo = EspIdfRemote {
git_ref: parse_esp_idf_git_ref(espidf_version),
repo_url: Some(espidf::DEFAULT_GIT_REPOSITORY.to_string()),
repo_url: Some(DEFAULT_GIT_REPOSITORY.to_string()),
};
remove_dir_all(get_install_path(repo).parent().unwrap())?;
}
@ -268,3 +271,55 @@ fn main() -> Result<()> {
SubCommand::Uninstall(args) => uninstall(args),
}
}
/// Deletes dist folder.
fn clear_dist_folder() -> Result<()> {
info!("{} Clearing dist folder", emoji::WRENCH);
remove_dir_all(&get_dist_path(""))?;
Ok(())
}
/// Creates the export file with the necessary environment variables.
pub fn export_environment(export_file: &PathBuf, exports: &[String]) -> Result<()> {
info!("{} Creating export file", emoji::WRENCH);
let mut file = File::create(export_file)?;
for e in exports.iter() {
file.write_all(e.as_bytes())?;
file.write_all(b"\n")?;
}
#[cfg(windows)]
info!(
"{} PLEASE set up the environment variables running: '.\\{}'",
emoji::INFO,
export_file.display()
);
#[cfg(unix)]
info!(
"{} PLEASE set up the environment variables running: '. {}'",
emoji::INFO,
export_file.display()
);
info!(
"{} This step must be done every time you open a new terminal.",
emoji::WARN
);
Ok(())
}
#[cfg(windows)]
/// For Windows, we need to check that we are installing all the targets if we are installing esp-idf.
pub fn check_arguments(targets: &HashSet<Target>, espidf_version: &Option<String>) -> Result<()> {
if espidf_version.is_some()
&& (!targets.contains(&Target::ESP32)
|| !targets.contains(&Target::ESP32C3)
|| !targets.contains(&Target::ESP32S2)
|| !targets.contains(&Target::ESP32S3))
{
bail!(
"{} When installing esp-idf in Windows, only --targets \"all\" is supported.",
emoji::ERROR
);
}
Ok(())
}

View File

@ -2,10 +2,10 @@
use crate::emoji;
use log::debug;
use std::collections::HashSet;
use strum::{Display, EnumString};
use std::{collections::HashSet, str::FromStr};
use strum::Display;
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, Display, EnumString)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, Display)]
pub enum Target {
/// Xtensa LX7 based dual core
@ -22,14 +22,16 @@ pub enum Target {
ESP32C3,
}
impl Target {
pub fn from_str(s: &str) -> Option<Self> {
impl FromStr for Target {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"esp32" => Some(Target::ESP32),
"esp32s2" => Some(Target::ESP32S2),
"esp32s3" => Some(Target::ESP32S3),
"esp32c3" => Some(Target::ESP32C3),
_ => None,
"esp32" => Ok(Target::ESP32),
"esp32s2" => Ok(Target::ESP32S2),
"esp32s3" => Ok(Target::ESP32S3),
"esp32c3" => Ok(Target::ESP32C3),
_ => Err(()),
}
}
}
@ -52,7 +54,7 @@ pub fn parse_targets(targets_str: &str) -> Result<HashSet<Target>, String> {
};
for target in targets_str {
targets.insert(Target::from_str(target).unwrap());
targets.insert(FromStr::from_str(target).unwrap());
}
debug!("{} Parsed targets: {:?}", emoji::DEBUG, targets);
Ok(targets)
@ -60,8 +62,8 @@ pub fn parse_targets(targets_str: &str) -> Result<HashSet<Target>, String> {
#[cfg(test)]
mod tests {
use crate::parse_targets;
use crate::Target;
use espup::parse_targets;
use espup::Target;
#[test]
fn test_parse_targets() {
assert_eq!(

View File

@ -1,9 +1,13 @@
//! GCC Toolchain source and installation tools
use crate::emoji;
use crate::gcc_toolchain::{get_toolchain_name, get_ulp_toolchain_name};
use crate::targets::Target;
use crate::utils::get_home_dir;
use crate::{
emoji,
targets::Target,
toolchain::{
gcc_toolchain::{get_toolchain_name, get_ulp_toolchain_name},
get_home_dir,
},
};
use anyhow::{Context, Result};
use embuild::{espidf, espidf::EspIdfRemote, git};
use log::{debug, info};

View File

@ -1,9 +1,10 @@
//! GCC Toolchain source and installation tools
use crate::emoji;
use crate::espidf::get_tool_path;
use crate::targets::Target;
use crate::utils::download_file;
use crate::{
emoji,
targets::Target,
toolchain::{download_file, espidf::get_tool_path},
};
use anyhow::Result;
use embuild::espidf::EspIdfVersion;
use log::{debug, info};

View File

@ -1,8 +1,9 @@
//! LLVM Toolchain source and installation tools
use crate::emoji;
use crate::espidf::get_tool_path;
use crate::utils::download_file;
use crate::{
emoji,
toolchain::{download_file, espidf::get_tool_path},
};
use anyhow::{bail, Ok, Result};
use log::info;
use std::path::{Path, PathBuf};
@ -146,7 +147,7 @@ fn get_release_with_underscores(version: &str) -> String {
#[cfg(test)]
mod tests {
use crate::llvm_toolchain::get_release_with_underscores;
use espup::llvm_toolchain::get_release_with_underscores;
#[test]
fn test_get_release_with_underscores() {

View File

@ -1,5 +1,4 @@
use crate::emoji;
use crate::espidf::get_dist_path;
#[cfg(windows)]
use crate::targets::Target;
use anyhow::{bail, Result};
@ -9,32 +8,17 @@ use log::info;
#[cfg(windows)]
use std::collections::HashSet;
use std::{
fs::{create_dir_all, remove_dir_all, File},
io::{copy, BufReader, Write},
path::{Path, PathBuf},
fs::{create_dir_all, File},
io::{copy, BufReader},
path::Path,
};
use tar::Archive;
use xz2::read::XzDecoder;
pub mod logging {
use env_logger::{Builder, Env, WriteStyle};
/// Initializes the logger
pub fn initialize_logger(log_level: &str) {
Builder::from_env(Env::default().default_filter_or(log_level))
.format_target(false)
.format_timestamp_secs()
.write_style(WriteStyle::Always)
.init();
}
}
/// Deletes dist folder.
pub fn clear_dist_folder() -> Result<()> {
info!("{} Clearing dist folder", emoji::WRENCH);
remove_dir_all(&get_dist_path(""))?;
Ok(())
}
pub mod espidf;
pub mod gcc_toolchain;
pub mod llvm_toolchain;
pub mod rust_toolchain;
/// Returns the path to the home directory.
pub fn get_home_dir() -> String {
@ -112,48 +96,3 @@ pub fn download_file(
}
Ok(format!("{}/{}", output_directory, file_name))
}
/// Creates the export file with the necessary environment variables.
pub fn export_environment(export_file: &PathBuf, exports: &[String]) -> Result<()> {
info!("{} Creating export file", emoji::WRENCH);
let mut file = File::create(export_file)?;
for e in exports.iter() {
file.write_all(e.as_bytes())?;
file.write_all(b"\n")?;
}
#[cfg(windows)]
info!(
"{} PLEASE set up the environment variables running: '.\\{}'",
emoji::INFO,
export_file.display()
);
#[cfg(unix)]
info!(
"{} PLEASE set up the environment variables running: '. {}'",
emoji::INFO,
export_file.display()
);
info!(
"{} This step must be done every time you open a new terminal.",
emoji::WARN
);
Ok(())
}
#[cfg(windows)]
/// For Windows, we need to check that we are installing all the targets if we are installing esp-idf.
pub fn check_arguments(targets: &HashSet<Target>, espidf_version: &Option<String>) -> Result<()> {
if espidf_version.is_some()
&& (!targets.contains(&Target::ESP32)
|| !targets.contains(&Target::ESP32C3)
|| !targets.contains(&Target::ESP32S2)
|| !targets.contains(&Target::ESP32S3))
{
bail!(
"{} When installing esp-idf in Windows, only --targets \"all\" is supported.",
emoji::ERROR
);
}
Ok(())
}

View File

@ -1,8 +1,9 @@
//! Xtensa Rust Toolchain source and installation tools
use crate::emoji;
use crate::espidf::get_dist_path;
use crate::utils::{download_file, get_home_dir};
use crate::{
emoji,
toolchain::{download_file, espidf::get_dist_path, get_home_dir},
};
use anyhow::{bail, Result};
use embuild::cmd;
use log::{info, warn};