From 7e96c9451dd0ac0078b196f25059c41029f60955 Mon Sep 17 00:00:00 2001 From: Petr Gadorek Date: Wed, 10 Jul 2024 16:16:05 +0200 Subject: [PATCH] added possibility to add path to rc file --- Cargo.lock | 1 + Cargo.toml | 1 + locales/app.yml | 11 ++++++++- src/wizard/mod.rs | 62 +++++++++++++++++++++++++++++++++++++---------- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb261ee..7ee8158 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1763,6 +1763,7 @@ dependencies = [ "console", "crossterm", "dialoguer", + "dirs", "git2", "idf-im-lib", "indicatif", diff --git a/Cargo.toml b/Cargo.toml index 9992884..4a4a409 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ git2 = "0.19.0" log = "0.4.21" simple_logger = "5.0.0" rust-i18n = "3.0.1" +dirs = "5.0.1" diff --git a/locales/app.yml b/locales/app.yml index a08e17f..983229e 100644 --- a/locales/app.yml +++ b/locales/app.yml @@ -154,4 +154,13 @@ wizard.tool.download_failed: cn: 下载失败 wizard.tool.extracted: en: extracted tool - cn: 已解压工具 \ No newline at end of file + cn: 已解压工具 +wizard.after_install.add_to_path.prompt: + en: Do you want to add ESP-IDF to your PATH permanently? + cn: 是否要永久加入 ESP-IDF 到 PATH 中? +wizard.shellrc.update.success: + en: ESP-IDF shellrc updated successfully + cn: ESP-IDF shellrc 已更新成功 +wizard.shellrc.update.error: + en: ESP-IDF shellrc update failed + cn: ESP-IDF shellrc 更新失败 \ No newline at end of file diff --git a/src/wizard/mod.rs b/src/wizard/mod.rs index f388de6..850d9a2 100644 --- a/src/wizard/mod.rs +++ b/src/wizard/mod.rs @@ -6,6 +6,7 @@ use indicatif::{ProgressBar, ProgressState, ProgressStyle}; use log::{debug, error, info, trace, warn}; use rust_i18n::t; use std::fmt::Write; +use std::fs::OpenOptions; use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; use std::{env, fs}; @@ -95,8 +96,6 @@ async fn select_idf_version(target: &str, theme: &ColorfulTheme) -> Result, mirror: Option<&str>) -> Result { @@ -289,6 +288,34 @@ fn python_sanity_check() -> Result<(), String> { } } +fn add_to_shell_rc(content: &str) -> Result<(), String> { + let shell = env::var("SHELL").unwrap_or_else(|_| String::from("")); + let home = dirs::home_dir().unwrap(); + + let rc_file = match shell.as_str() { + "/bin/bash" => home.join(".bashrc"), + "/bin/zsh" => home.join(".zshrc"), + "/bin/fish" => home.join(".config/fish/config.fish"), + _ => return Err("Unsupported shell".to_string()), + }; + + let mut file = OpenOptions::new() + .append(true) + .create(true) + .open(rc_file) + .unwrap(); + + match std::io::Write::write_all(&mut file, content.as_bytes()) { + Ok(_) => info!("{}", t!("wizard.shellrc.update.success")), + Err(err) => { + error!("{}", t!("wizard.shellrc.update.error")); + error!("Error: {:?}", err); + } + }; + + Ok(()) +} + fn install_python_environment() {} pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { @@ -711,22 +738,31 @@ pub async fn run_wizzard_run(mut config: Settings) -> Result<(), String> { } #[cfg(not(windows))] if std::env::consts::OS != "windows" { - for p in export_paths { - idf_im_lib::add_path_to_path(&p); - } let exports = env_vars .into_iter() .map(|(k, v)| format!("export {}=\"{}\"; ", k, v)) .collect::>(); - println!( - "{}:\r\n\r\n{}", - t!("wizard.posix.succes_message"), - format!( - "{}; export PATH={}; ", - exports.join(""), - env::var("PATH").unwrap_or_default() - ) + let exp_strig = format!( + "{}{}; ", + exports.join(""), + format!("export PATH=\"$PATH:{:?}\"", export_paths.join(":")) ); + + match Confirm::new() + .with_prompt(t!("wizard.after_install.add_to_path.prompt")) + .interact() + { + Ok(true) => match add_to_shell_rc(&exp_strig) { + Ok(_) => println!("{}", t!("wizard.posix.succes_message")), + Err(err) => panic!("{:?}", err.to_string()), + }, + Ok(false) => println!( + "{}:\r\n\r\n{}\r\n\r\n", + t!("wizard.posix.succes_message"), + exp_strig + ), + Err(err) => panic!("{:?}", err.to_string()), + } } // TODO: offer to save settings