Skip to content

Commit

Permalink
added possibility to add path to rc file
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Gadorek committed Jul 10, 2024
1 parent 4267ad9 commit 7e96c94
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 10 additions & 1 deletion locales/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,13 @@ wizard.tool.download_failed:
cn: 下载失败
wizard.tool.extracted:
en: extracted tool
cn: 已解压工具
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 更新失败
62 changes: 49 additions & 13 deletions src/wizard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -95,8 +96,6 @@ async fn select_idf_version(target: &str, theme: &ColorfulTheme) -> Result<Strin
.interact()
.unwrap();
return Ok(avalible_versions[selected_version].clone());
// custom_path = format!("{}/{}", base_install_path, selected_target).clone();
// println!("Selected IDF version {:?}", selected_target.to_string());
}

fn download_idf(path: &str, tag: Option<&str>, mirror: Option<&str>) -> Result<String, String> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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::<Vec<String>>();
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
Expand Down

0 comments on commit 7e96c94

Please sign in to comment.