Skip to content

Commit e9f1180

Browse files
committed
IDF installed using eim or eclipse can be discovered
1 parent 0f29a49 commit e9f1180

File tree

2 files changed

+108
-10
lines changed

2 files changed

+108
-10
lines changed

src-tauri/src/cli/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use helpers::generic_select;
1010
use idf_im_lib::get_log_directory;
1111
use idf_im_lib::idf_config::IdfConfig;
1212
use idf_im_lib::settings::Settings;
13+
use idf_im_lib::utils::try_import_existing_idf;
1314
use idf_im_lib::version_manager::remove_single_idf_version;
1415
use idf_im_lib::version_manager::select_idf_version;
1516
use log::debug;
@@ -295,7 +296,7 @@ pub async fn run_cli(cli: Cli) {
295296
}
296297
Commands::Discover => {
297298
info!("Discovering available versions... (This can take couple of minutes)");
298-
let idf_dirs = idf_im_lib::version_manager::find_esp_idf_folders("/tmp");
299+
let idf_dirs = idf_im_lib::version_manager::find_esp_idf_folders("/");
299300
if idf_dirs.is_empty() {
300301
info!("No IDF directories found");
301302
} else {
@@ -326,6 +327,16 @@ pub async fn run_cli(cli: Cli) {
326327
}
327328

328329
}
330+
for p in paths_to_add {
331+
match try_import_existing_idf(&p) {
332+
Ok(_) => {
333+
info!("Added to config: {}", p);
334+
}
335+
Err(err) => {
336+
error!("Failed to add: {} - reason :{}",p, err);
337+
}
338+
}
339+
}
329340
}
330341
Commands::Import { path } => match path {
331342
Some(config_file) => {

src-tauri/src/lib/utils.rs

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
use crate::{
2-
command_executor::execute_command,
3-
idf_config::{IdfConfig, IdfInstallation},
4-
idf_tools::read_and_parse_tools_file,
5-
single_version_post_install,
6-
version_manager::get_default_config_path,
2+
command_executor::execute_command, idf_config::{IdfConfig, IdfInstallation}, idf_tools::{self, read_and_parse_tools_file}, replace_unescaped_spaces_win, settings::Settings, single_version_post_install, version_manager::get_default_config_path
73
};
84
use anyhow::{anyhow, Result};
95
use log::{debug, warn};
106
use rust_search::SearchBuilder;
117
use serde::{Deserialize, Serialize};
8+
use uuid::Uuid;
129
#[cfg(not(windows))]
1310
use std::os::unix::fs::MetadataExt;
1411
use std::{
15-
collections::{HashMap, HashSet},
16-
fs::{self},
17-
io,
18-
path::{Path, PathBuf},
12+
collections::{HashMap, HashSet}, fmt::format, fs::{self}, io, path::{Path, PathBuf}
1913
};
2014
/// This function retrieves the path to the git executable.
2115
///
@@ -369,6 +363,99 @@ pub fn parse_tool_set_config(config_path: &str) -> Result<()> {
369363
Ok(())
370364
}
371365

366+
pub fn try_import_existing_idf(idf_path:&str) -> Result<()> {
367+
let path = Path::new(idf_path);
368+
let default_settings = Settings::default();
369+
370+
// test if was installed by eim
371+
if let Some(parent_dir) = path.parent() {
372+
let parent_filename = parent_dir.file_name().unwrap().to_str().unwrap();
373+
if let Some(grandparent_dir) = parent_dir.parent() {
374+
let target_dir = grandparent_dir;
375+
if let Ok(entries) = fs::read_dir(target_dir) {
376+
for entry in entries {
377+
if let Ok(entry) = entry {
378+
if let Some(file_name) = entry.file_name().to_str() {
379+
if file_name.starts_with(&format!("activate_idf_{}",parent_filename)) && entry.file_type().map(|ft| ft.is_file()).unwrap_or(false) {
380+
// was installed by EIM
381+
382+
let installation = IdfInstallation {
383+
id: format!("esp-idf-{}", Uuid::new_v4().to_string().replace("-", "")),
384+
activation_script: entry.path().to_str().unwrap().to_string(),
385+
path: idf_path.to_string(),
386+
name: parent_filename.to_string(),
387+
python: "python".to_string(),
388+
idf_tools_path: default_settings.idf_tools_path.unwrap(),
389+
};
390+
let config_path = get_default_config_path();
391+
let mut current_config = match IdfConfig::from_file(&config_path) {
392+
Ok(config) => config,
393+
Err(e) => {
394+
IdfConfig::default()
395+
}
396+
};
397+
current_config.idf_installed.push(installation);
398+
match current_config.to_file(config_path, true, false) {
399+
Ok(_) => {
400+
debug!("Updated config file with new tool set");
401+
return Ok(());
402+
}
403+
Err(e) => {
404+
return Err(anyhow!("Failed to update config file: {}", e));
405+
}
406+
}
407+
}
408+
}
409+
}
410+
}
411+
}
412+
}
413+
}
414+
// was not installed by eim
415+
let path_to_create_activation_script = match path.parent() {
416+
Some(parent) => parent,
417+
None => path,
418+
};
419+
let idf_version = path_to_create_activation_script.file_name().unwrap().to_str().unwrap();
420+
let tools_file = match idf_tools::read_and_parse_tools_file(&Path::new(idf_path).join("tools").join("tools.json").to_str().unwrap()){
421+
Ok(tools_file) => tools_file,
422+
Err(e) => {
423+
return Err(anyhow!("Failed to read tools.json file: {}", e));
424+
}
425+
};
426+
let export_paths = idf_tools::get_tools_export_paths(
427+
tools_file,
428+
["all".to_string()].to_vec(),
429+
&default_settings.tool_install_folder_name.unwrap(),
430+
)
431+
.into_iter()
432+
.map(|p| {
433+
if std::env::consts::OS == "windows" {
434+
replace_unescaped_spaces_win(&p)
435+
} else {
436+
p
437+
}
438+
})
439+
.collect();
440+
match import_single_version(
441+
path_to_create_activation_script.to_str().unwrap(),
442+
idf_path,
443+
idf_version,
444+
&default_settings.idf_tools_path.unwrap(),
445+
export_paths,
446+
None,
447+
) {
448+
Ok(_) => {
449+
debug!("Successfully imported tool set");
450+
}
451+
Err(e) => {
452+
warn!("Failed to import tool set: {}", e);
453+
}
454+
}
455+
// TODO: add more approaches for different legacy installations
456+
Ok(())
457+
}
458+
372459
pub fn import_single_version(path_to_create_activation_script: &str,idf_location: &str, idf_version: &str, idf_tools_path: &str, export_paths: Vec<String>, python: Option<String>) -> Result<()> {
373460
single_version_post_install(
374461
path_to_create_activation_script,

0 commit comments

Comments
 (0)