Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements #384

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

### Changed
- Reduce logs verbosity, add docstrings, and use async methods (#384)

### Removed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Options:
> **Note**
>
> #### GitHub API
> During the installation process, several GitHub queries are made, [which are subject to certain limits](https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#rate-limiting). Our number of queries should not hit the limits unless you are running `espup install` command numerous times in a short span of time. We recommend setting the [`GITHUB_TOKEN` environment variable](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#about-the-github_token-secret) when using `espup` in CI, if you want to use `espup` on CI, recommend using it via the [`xtensa-toolchain` action](https://github.com/esp-rs/xtensa-toolchain/), and making sure `GITHUB_TOKEN` is not set when using it on a host machine. See https://github.com/esp-rs/xtensa-toolchain/issues/15 for more details on this.
> During the installation process, several GitHub queries are made, [which are subject to certain limits](https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#rate-limiting). Our number of queries should not hit the limit unless you are running `espup install` command numerous times in a short span of time. We recommend setting the [`GITHUB_TOKEN` environment variable](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#about-the-github_token-secret) when using `espup` in CI, if you want to use `espup` on CI, recommend using it via the [`xtensa-toolchain` action](https://github.com/esp-rs/xtensa-toolchain/), and making sure `GITHUB_TOKEN` is not set when using it on a host machine. See https://github.com/esp-rs/xtensa-toolchain/issues/15 for more details on this.

```
Usage: espup install [OPTIONS]
Expand Down
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Command line interface.

use crate::targets::{parse_targets, Target};
use clap::Parser;
use clap_complete::Shell;
Expand Down
20 changes: 12 additions & 8 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use directories::BaseDirs;
use log::info;
#[cfg(windows)]
use log::warn;
#[cfg(windows)]
use std::env;
use std::{
env,
fs::File,
io::Write,
path::{Path, PathBuf},
Expand Down Expand Up @@ -58,7 +57,7 @@ pub fn get_export_file(export_file: Option<PathBuf>) -> Result<PathBuf, Error> {
if export_file.is_absolute() {
Ok(export_file)
} else {
let current_dir = std::env::current_dir()?;
let current_dir = env::current_dir()?;
Ok(current_dir.join(export_file))
}
} else {
Expand Down Expand Up @@ -113,7 +112,12 @@ pub fn export_environment(export_file: &Path) -> Result<(), Error> {
mod tests {
use crate::env::{create_export_file, get_export_file, DEFAULT_EXPORT_FILE};
use directories::BaseDirs;
use std::{env::current_dir, path::PathBuf};
use std::{
env::current_dir,
fs::{create_dir_all, read_to_string},
path::PathBuf,
};
use tempfile::TempDir;

#[test]
#[allow(unused_variables)]
Expand Down Expand Up @@ -142,20 +146,20 @@ mod tests {
#[test]
fn test_create_export_file() {
// Creates the export file and writes the correct content to it
let temp_dir = tempfile::TempDir::new().unwrap();
let temp_dir = TempDir::new().unwrap();
let export_file = temp_dir.path().join("export.sh");
let exports = vec![
"export VAR1=value1".to_string(),
"export VAR2=value2".to_string(),
];
create_export_file(&export_file, &exports).unwrap();
let contents = std::fs::read_to_string(export_file).unwrap();
let contents = read_to_string(export_file).unwrap();
assert_eq!(contents, "export VAR1=value1\nexport VAR2=value2\n");

// Returns the correct error when it fails to create the export file (it already exists)
let temp_dir = tempfile::TempDir::new().unwrap();
let temp_dir = TempDir::new().unwrap();
let export_file = temp_dir.path().join("export.sh");
std::fs::create_dir_all(&export_file).unwrap();
create_dir_all(&export_file).unwrap();
let exports = vec![
"export VAR1=value1".to_string(),
"export VAR2=value2".to_string(),
Expand Down
39 changes: 18 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ use clap::{CommandFactory, Parser};
use espup::env::set_environment_variable;
use espup::{
cli::{CompletionsOpts, InstallOpts, UninstallOpts},
error::Error,
logging::initialize_logger,
toolchain::{
gcc::uninstall_gcc_toolchains, install as toolchain_install, llvm::Llvm,
rust::get_rustup_home, InstallMode,
gcc::uninstall_gcc_toolchains,
install as toolchain_install,
llvm::Llvm,
remove_dir,
rust::{get_rustup_home, XtensaRust},
InstallMode,
},
update::check_for_update,
};
use log::info;
use miette::Result;
use std::{env, fs::remove_dir_all};
use std::{env, io::stdout};

#[derive(Parser)]
#[command(about, version)]
Expand Down Expand Up @@ -42,12 +45,7 @@ async fn completions(args: CompletionsOpts) -> Result<()> {

info!("Generating completions for {} shell", args.shell);

clap_complete::generate(
args.shell,
&mut Cli::command(),
"espup",
&mut std::io::stdout(),
);
clap_complete::generate(args.shell, &mut Cli::command(), "espup", &mut stdout());

info!("Completions successfully generated!");

Expand All @@ -69,21 +67,20 @@ async fn uninstall(args: UninstallOpts) -> Result<()> {
check_for_update(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));

info!("Uninstalling the Espressif Rust ecosystem");
let install_path = get_rustup_home().join("toolchains").join(args.name);
let toolchain_dir = get_rustup_home().join("toolchains").join(args.name);

Llvm::uninstall(&install_path)?;
if toolchain_dir.exists() {
Llvm::uninstall(&toolchain_dir).await?;

uninstall_gcc_toolchains(&install_path)?;
uninstall_gcc_toolchains(&toolchain_dir).await?;

info!(
"Deleting the Xtensa Rust toolchain located in '{}'",
&install_path.display()
);
remove_dir_all(&install_path)
.map_err(|_| Error::RemoveDirectory(install_path.display().to_string()))?;
XtensaRust::uninstall(&toolchain_dir).await?;

#[cfg(windows)]
set_environment_variable("PATH", &env::var("PATH").unwrap())?;
remove_dir(&toolchain_dir).await?;

#[cfg(windows)]
set_environment_variable("PATH", &env::var("PATH").unwrap())?;
}

info!("Uninstallation successfully completed!");
Ok(())
Expand Down
19 changes: 10 additions & 9 deletions src/toolchain/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use crate::{
use async_trait::async_trait;
use log::{debug, info, warn};
use miette::Result;
use std::{
fs::remove_dir_all,
path::{Path, PathBuf},
};
#[cfg(windows)]
use std::env;
use std::path::{Path, PathBuf};
use tokio::fs::remove_dir_all;

const DEFAULT_GCC_REPOSITORY: &str = "https://github.com/espressif/crosstool-NG/releases/download";
const DEFAULT_GCC_RELEASE: &str = "13.2.0_20230928";
Expand Down Expand Up @@ -87,9 +87,9 @@ impl Installable for Gcc {
"$Env:PATH = \"{};\" + $Env:PATH",
&self.get_bin_path()
));
std::env::set_var(
env::set_var(
"PATH",
self.get_bin_path().replace('/', "\\") + ";" + &std::env::var("PATH").unwrap(),
self.get_bin_path().replace('/', "\\") + ";" + &env::var("PATH").unwrap(),
);
}
#[cfg(unix)]
Expand Down Expand Up @@ -125,7 +125,7 @@ fn get_artifact_extension(host_triple: &HostTriple) -> &str {
}

/// Checks if the toolchain is pressent, if present uninstalls it.
pub fn uninstall_gcc_toolchains(toolchain_path: &Path) -> Result<(), Error> {
pub async fn uninstall_gcc_toolchains(toolchain_path: &Path) -> Result<(), Error> {
info!("Uninstalling GCC");

let gcc_toolchains = vec![XTENSA_GCC, RISCV_GCC];
Expand All @@ -141,14 +141,15 @@ pub fn uninstall_gcc_toolchains(toolchain_path: &Path) -> Result<(), Error> {
DEFAULT_GCC_RELEASE,
toolchain
);
std::env::set_var(
env::set_var(
"PATH",
std::env::var("PATH")
env::var("PATH")
.unwrap()
.replace(&format!("{gcc_path};"), ""),
);
}
remove_dir_all(&gcc_path)
.await
.map_err(|_| Error::RemoveDirectory(gcc_path.display().to_string()))?;
}
}
Expand Down
38 changes: 24 additions & 14 deletions src/toolchain/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use directories::BaseDirs;
use log::{info, warn};
use miette::Result;
use regex::Regex;
#[cfg(windows)]
use std::env;
use std::path::{Path, PathBuf};
#[cfg(unix)]
use std::{fs::create_dir_all, os::unix::fs::symlink};
use std::{
fs::remove_dir_all,
path::{Path, PathBuf},
u8,
};
use tokio::fs::remove_dir_all;

const DEFAULT_LLVM_REPOSITORY: &str = "https://github.com/espressif/llvm-project/releases/download";
const DEFAULT_LLVM_15_VERSION: &str = "esp-15.0.0-20221201";
Expand Down Expand Up @@ -57,7 +56,7 @@ impl Llvm {
/// Gets the binary path.
fn get_lib_path(&self) -> String {
#[cfg(windows)]
let llvm_path = format!("{}/esp-clang/bin", self.path.to_str().unwrap());
let llvm_path = format!("{}/esp-clang/bin", self.path.to_str().unwrap()).replace('/', "\\");
#[cfg(unix)]
let llvm_path = format!("{}/esp-clang/lib", self.path.to_str().unwrap());
llvm_path
Expand All @@ -66,7 +65,8 @@ impl Llvm {
/// Gets the binary path of clang
fn get_bin_path(&self) -> String {
#[cfg(windows)]
let llvm_path = format!("{}/esp-clang/bin/clang.exe", self.path.to_str().unwrap());
let llvm_path =
format!("{}/esp-clang/bin/clang.exe", self.path.to_str().unwrap()).replace('/', "\\");
#[cfg(unix)]
let llvm_path = format!("{}/esp-clang/bin/clang", self.path.to_str().unwrap());
llvm_path
Expand Down Expand Up @@ -121,36 +121,45 @@ impl Llvm {
}

/// Uninstall LLVM toolchain.
pub fn uninstall(toolchain_path: &Path) -> Result<(), Error> {
pub async fn uninstall(toolchain_path: &Path) -> Result<(), Error> {
info!("Uninstalling Xtensa LLVM");
let llvm_path = toolchain_path.join(CLANG_NAME);
if llvm_path.exists() {
#[cfg(windows)]
if cfg!(windows) {
delete_environment_variable("LIBCLANG_PATH")?;
delete_environment_variable("CLANG_PATH")?;
let updated_path = std::env::var("PATH").unwrap().replace(
let mut updated_path = env::var("PATH").unwrap().replace(
&format!(
"{}\\{}\\esp-clang\\bin;",
llvm_path.display().to_string().replace('/', "\\"),
DEFAULT_LLVM_15_VERSION,
),
"",
);
updated_path = updated_path.replace(
&format!(
"{}\\{}\\esp-clang\\bin;",
llvm_path.display().to_string().replace('/', "\\"),
DEFAULT_LLVM_16_VERSION,
),
"",
);
set_environment_variable("PATH", &updated_path)?;
}
remove_dir_all(&llvm_path)
.await
.map_err(|_| Error::RemoveDirectory(llvm_path.display().to_string()))?;
#[cfg(unix)]
if cfg!(unix) {
let espup_dir = BaseDirs::new().unwrap().home_dir().join(".espup");

if espup_dir.exists() {
remove_dir_all(espup_dir.display().to_string())
.await
.map_err(|_| Error::RemoveDirectory(espup_dir.display().to_string()))?;
}
}
let path = toolchain_path.join(CLANG_NAME);
remove_dir_all(&path)
.map_err(|_| Error::RemoveDirectory(path.display().to_string()))?;
}
Ok(())
}
Expand Down Expand Up @@ -193,9 +202,9 @@ impl Installable for Llvm {
&format!("{}\\libclang.dll", self.get_lib_path().replace('/', "\\")),
)?;

std::env::set_var(
env::set_var(
"PATH",
self.get_lib_path().replace('/', "\\") + ";" + &std::env::var("PATH").unwrap(),
self.get_lib_path().replace('/', "\\") + ";" + &env::var("PATH").unwrap(),
);
}
#[cfg(unix)]
Expand All @@ -210,6 +219,7 @@ impl Installable for Llvm {
let llvm_symlink_path = espup_dir.join("esp-clang");
if llvm_symlink_path.exists() {
remove_dir_all(&llvm_symlink_path)
.await
.map_err(|_| Error::RemoveDirectory(llvm_symlink_path.display().to_string()))?;
}
info!(
Expand Down
Loading