|
1 | 1 | 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 |
7 | 3 | }; |
8 | 4 | use anyhow::{anyhow, Result}; |
9 | 5 | use log::{debug, warn}; |
10 | 6 | use rust_search::SearchBuilder; |
11 | 7 | use serde::{Deserialize, Serialize}; |
| 8 | +use uuid::Uuid; |
12 | 9 | #[cfg(not(windows))] |
13 | 10 | use std::os::unix::fs::MetadataExt; |
14 | 11 | 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} |
19 | 13 | }; |
20 | 14 | /// This function retrieves the path to the git executable. |
21 | 15 | /// |
@@ -369,6 +363,99 @@ pub fn parse_tool_set_config(config_path: &str) -> Result<()> { |
369 | 363 | Ok(()) |
370 | 364 | } |
371 | 365 |
|
| 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 | + |
372 | 459 | 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 | 460 | single_version_post_install( |
374 | 461 | path_to_create_activation_script, |
|
0 commit comments