Skip to content

Commit 178bcb2

Browse files
committed
added search ofor esp_idf.json files and parsing as a part of discovery process
1 parent c78f20a commit 178bcb2

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

src-tauri/src/cli/cli_args.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ pub enum Commands {
6565
},
6666

6767
/// Discover available ESP-IDF versions (not implemented yet)
68-
Discover,
68+
Discover {
69+
#[arg(help = "Discover available ESP-IDF versions and imports them")]
70+
path: Option<String>,
71+
},
6972

7073
/// Remove specific ESP-IDF version
7174
Remove {

src-tauri/src/cli/mod.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::path::Path;
12
use std::path::PathBuf;
23

34
use cli_args::Cli;
@@ -10,6 +11,8 @@ use helpers::generic_select;
1011
use idf_im_lib::get_log_directory;
1112
use idf_im_lib::idf_config::IdfConfig;
1213
use idf_im_lib::settings::Settings;
14+
use idf_im_lib::utils::find_by_name_and_extension;
15+
use idf_im_lib::utils::parse_esp_idf_json;
1316
use idf_im_lib::utils::try_import_existing_idf;
1417
use idf_im_lib::version_manager::remove_single_idf_version;
1518
use idf_im_lib::version_manager::select_idf_version;
@@ -294,10 +297,48 @@ pub async fn run_cli(cli: Cli) {
294297
}
295298
}
296299
}
297-
Commands::Discover => {
298-
// todo: first parse existing esp_idf.json (using parse_esp_idf_json)
300+
Commands::Discover {path } => {
299301
info!("Discovering available versions... (This can take couple of minutes)");
300-
let idf_dirs = idf_im_lib::version_manager::find_esp_idf_folders("/");
302+
// TODO: add warning and confirmation
303+
let path = path.unwrap_or_else(|| {
304+
let default_path = match std::env::consts::OS {
305+
"windows" => {
306+
"C:\\".to_string()
307+
}
308+
_ => {
309+
"/".to_string()
310+
}
311+
};
312+
313+
314+
debug!("No path provided, using default: {}", default_path);
315+
default_path
316+
});
317+
// first parse existing esp_idf.json (using parse_esp_idf_json)
318+
319+
let search_patch = Path::new(&path);
320+
let esp_idf_json_path = find_by_name_and_extension(
321+
search_patch,
322+
"esp_idf",
323+
"json",
324+
);
325+
if esp_idf_json_path.is_empty() {
326+
info!("No esp_idf.json found");
327+
} else {
328+
info!("Found {} esp_idf.json files:", esp_idf_json_path.len());
329+
}
330+
for path in esp_idf_json_path {
331+
info!("- {} ", &path);
332+
match parse_esp_idf_json(&path) {
333+
Ok(_) => {
334+
info!("Parsed config: {:?}", path);
335+
}
336+
Err(err) => {
337+
info!("Failed to parse esp_idf.json: {}", err);
338+
}
339+
}
340+
}
341+
let idf_dirs = idf_im_lib::version_manager::find_esp_idf_folders(&path);
301342
if idf_dirs.is_empty() {
302343
info!("No IDF directories found");
303344
} else {

src-tauri/src/lib/utils.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ pub fn find_directories_by_name(path: &Path, name: &str) -> Vec<String> {
8383
filter_directories(filter_subpaths(search))
8484
}
8585

86+
pub fn find_by_name_and_extension(path: &Path, name: &str, extension: &str) -> Vec<String> {
87+
SearchBuilder::default()
88+
.location(path)
89+
.search_input(name)
90+
.ext(extension)
91+
.strict()
92+
.ignore_case()
93+
.hidden()
94+
.build()
95+
.collect()
96+
}
97+
8698
/// Checks if the given path is a valid ESP-IDF directory.
8799
///
88100
/// # Purpose
@@ -516,6 +528,8 @@ pub fn try_import_existing_idf(idf_path:&str) -> Result<()> {
516528
}
517529

518530
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<()> {
531+
// TODO: skip is path does not exist
532+
// TODO: check if not alreasdy in the config
519533
single_version_post_install(
520534
path_to_create_activation_script,
521535
idf_location,

0 commit comments

Comments
 (0)