Skip to content

Commit 0f29a49

Browse files
committed
find all esp-idf instances and evaluate which one to process
1 parent aa585ca commit 0f29a49

File tree

4 files changed

+104
-39
lines changed

4 files changed

+104
-39
lines changed

src-tauri/src/cli/mod.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use config::ConfigError;
88
use helpers::generic_input;
99
use helpers::generic_select;
1010
use idf_im_lib::get_log_directory;
11+
use idf_im_lib::idf_config::IdfConfig;
1112
use idf_im_lib::settings::Settings;
1213
use idf_im_lib::version_manager::remove_single_idf_version;
1314
use idf_im_lib::version_manager::select_idf_version;
@@ -293,12 +294,37 @@ pub async fn run_cli(cli: Cli) {
293294
}
294295
}
295296
Commands::Discover => {
296-
// TODO:Implement version discovery
297-
unimplemented!("Version discovery not implemented yet");
298-
println!("Discovering available versions... (This can take couple of minutes)");
299-
let idf_dirs = idf_im_lib::version_manager::find_esp_idf_folders("/");
297+
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+
if idf_dirs.is_empty() {
300+
info!("No IDF directories found");
301+
} else {
302+
info!("Found {} IDF directories:", idf_dirs.len());
303+
}
304+
let config = match idf_im_lib::version_manager::get_esp_ide_config() {
305+
Ok(config) => {
306+
if config.idf_installed.is_empty() {
307+
debug!(
308+
"No versions found. Every discovered version can be imported."
309+
);
310+
}
311+
config
312+
}
313+
Err(err) => {
314+
debug!("No ide config found. New will be created.");
315+
IdfConfig::default()
316+
}
317+
};
318+
let mut paths_to_add = vec![];
300319
for dir in idf_dirs {
301-
println!("Found IDF directory: {}", dir);
320+
info!("- {} ", &dir);
321+
if config.clone().is_path_in_config(dir.clone()) {
322+
info!("Already present!");
323+
} else {
324+
info!("Will be added...");
325+
paths_to_add.push(dir);
326+
}
327+
302328
}
303329
}
304330
Commands::Import { path } => match path {

src-tauri/src/lib/idf_config.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ pub struct IdfConfig {
3232
pub eim_path: Option<String>,
3333
}
3434

35+
impl Default for IdfConfig {
36+
fn default() -> Self {
37+
IdfConfig {
38+
git_path: crate::utils::get_git_path().unwrap(),
39+
idf_installed: [].to_vec(),
40+
idf_selected_id: "".to_string(),
41+
eim_path: None
42+
}
43+
}
44+
}
45+
3546
impl IdfConfig {
3647
/// Saves the configuration to a file.
3748
///
@@ -189,6 +200,10 @@ impl IdfConfig {
189200
}
190201
}
191202

203+
pub fn is_path_in_config(self, path:String) -> bool {
204+
self.idf_installed.iter().find(|i| i.path == path).is_some()
205+
}
206+
192207
/// Removes an IDF installation from the configuration.
193208
///
194209
/// This function searches for an installation matching the given identifier

src-tauri/src/lib/utils.rs

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,18 @@ pub fn is_valid_idf_directory(path: &str) -> bool {
106106
let path = PathBuf::from(path);
107107
let tools_path = path.join("tools");
108108
let tools_json_path = tools_path.join("tools.json");
109+
debug!("Checking for tools.json at: {}", tools_json_path.display());
109110
if !tools_json_path.exists() {
110111
return false;
111112
}
113+
debug!("Found tools.json at: {}", tools_json_path.display());
112114
match read_and_parse_tools_file(tools_json_path.to_str().unwrap()) {
113115
Ok(_) => {
116+
debug!("Valid IDF directory: {}", path.display());
114117
true
115118
}
116119
Err(_) => {
120+
debug!("Invalid IDF directory: {}", path.display());
117121
false
118122
}
119123
}
@@ -346,54 +350,71 @@ pub fn parse_tool_set_config(config_path: &str) -> Result<()> {
346350
let new_export_paths = vec![tool_set.env_vars.get("PATH").unwrap().to_string()];
347351
let tmp = PathBuf::from(tool_set.idf_location.clone());
348352
let version_path = tmp.parent().unwrap();
349-
single_version_post_install(
353+
match import_single_version(
350354
version_path.to_str().unwrap(),
351355
&tool_set.idf_location,
352356
&tool_set.idf_version,
353357
&new_idf_tools_path,
354358
new_export_paths,
355-
);
356-
357-
let new_activation_script = match std::env::consts::OS {
358-
"windows" => format!(
359-
"{}\\Microsoft.PowerShell_profile.ps1",
360-
version_path.to_str().unwrap()
361-
),
362-
_ => format!(
363-
"{}/activate_idf_{}.sh",
364-
version_path.to_str().unwrap(),
365-
tool_set.idf_version
366-
),
367-
};
368-
let installation = IdfInstallation {
369-
id: tool_set.id.to_string(),
370-
activation_script: new_activation_script,
371-
path: tool_set.idf_location,
372-
name: tool_set.idf_version,
373-
python: tool_set.system_python_executable_path,
374-
idf_tools_path: new_idf_tools_path,
375-
};
376-
let config_path = get_default_config_path();
377-
let mut current_config = match IdfConfig::from_file(&config_path) {
378-
Ok(config) => config,
379-
Err(e) => {
380-
return Err(anyhow!("Config file not found: {}", e));
381-
}
382-
};
383-
current_config.idf_installed.push(installation);
384-
match current_config.to_file(config_path, true, false) {
359+
Some(tool_set.system_python_executable_path),
360+
) {
385361
Ok(_) => {
386-
debug!("Updated config file with new tool set");
387-
return Ok(());
362+
debug!("Successfully imported tool set");
388363
}
389364
Err(e) => {
390-
return Err(anyhow!("Failed to update config file: {}", e));
365+
return Err(anyhow!("Failed to import tool set: {}", e));
391366
}
392367
}
393368
}
394369
Ok(())
395370
}
396371

372+
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<()> {
373+
single_version_post_install(
374+
path_to_create_activation_script,
375+
idf_location,
376+
idf_version,
377+
&idf_tools_path,
378+
export_paths,
379+
);
380+
let activation_script = match std::env::consts::OS {
381+
"windows" => format!(
382+
"{}\\Microsoft.PowerShell_profile.ps1",
383+
path_to_create_activation_script,
384+
),
385+
_ => format!(
386+
"{}/activate_idf_{}.sh",
387+
path_to_create_activation_script,
388+
idf_version
389+
),
390+
};
391+
let installation = IdfInstallation {
392+
id: idf_version.to_string(),
393+
activation_script,
394+
path: idf_location.to_string(),
395+
name: idf_version.to_string(),
396+
python: python.unwrap_or_else(|| "python".to_string()),
397+
idf_tools_path: idf_tools_path.to_string(),
398+
};
399+
let config_path = get_default_config_path();
400+
let mut current_config = match IdfConfig::from_file(&config_path) {
401+
Ok(config) => config,
402+
Err(e) => {
403+
return Err(anyhow!("Config file not found: {}", e));
404+
}
405+
};
406+
current_config.idf_installed.push(installation);
407+
match current_config.to_file(config_path, true, false) {
408+
Ok(_) => {
409+
debug!("Updated config file with new tool set");
410+
return Ok(());
411+
}
412+
Err(e) => {
413+
return Err(anyhow!("Failed to update config file: {}", e));
414+
}
415+
}
416+
}
417+
397418
/// Converts a path to a long path compatible with Windows.
398419
///
399420
/// This function takes a string representing a path and returns a new string.

src-tauri/src/lib/version_manager.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,12 @@ pub fn remove_single_idf_version(identifier: &str) -> Result<String> {
224224
pub fn find_esp_idf_folders(path: &str) -> Vec<String> {
225225
let path = Path::new(path);
226226
let mut dirs = crate::utils::find_directories_by_name(path, "esp-idf");
227+
debug!("Found {} esp-idf folders", dirs.len());
228+
debug!("Found folders: {:?}", dirs);
227229
dirs.sort();
228230
dirs.reverse();
229231
let filtered_dirs = crate::utils::filter_duplicate_paths(dirs.clone());
232+
debug!("Filtered folders: {:?}", filtered_dirs);
230233
filtered_dirs
231234
.iter()
232235
.filter(|p| crate::utils::is_valid_idf_directory(p))

0 commit comments

Comments
 (0)