@@ -368,7 +368,11 @@ fn extract_tools_path_from_python_env_path(path: &str) -> Option<PathBuf> {
368368/// It also logs errors if the IDF installation configuration cannot be updated.
369369pub fn parse_tool_set_config ( config_path : & str ) -> Result < ( ) > {
370370 let config_path = Path :: new ( config_path) ;
371- let json_str = std:: fs:: read_to_string ( config_path) . unwrap ( ) ;
371+ let json_str = match std:: fs:: read_to_string ( config_path) {
372+ Ok ( content) => content,
373+ Err ( e) => return Err ( anyhow ! ( "Failed to read config file: {}" , e) ) ,
374+ } ;
375+ debug ! ( "Parsing tool set config from: {}" , config_path. display( ) ) ;
372376 let config: Vec < IdfToolsConfig > = match serde_json:: from_str ( & json_str) {
373377 Ok ( config) => config,
374378 Err ( e) => return Err ( anyhow ! ( "Failed to parse config file: {}" , e) ) ,
@@ -384,21 +388,62 @@ pub fn parse_tool_set_config(config_path: &str) -> Result<()> {
384388 let new_export_paths = vec ! [ tool_set. env_vars. get( "PATH" ) . unwrap( ) . to_string( ) ] ;
385389 let tmp = PathBuf :: from ( tool_set. idf_location . clone ( ) ) ;
386390 let version_path = tmp. parent ( ) . unwrap ( ) ;
387- match import_single_version (
391+ single_version_post_install (
388392 version_path. to_str ( ) . unwrap ( ) ,
389393 & tool_set. idf_location ,
390394 & tool_set. idf_version ,
391395 & new_idf_tools_path,
392396 new_export_paths,
393- Some ( tool_set. system_python_executable_path ) ,
394- ) {
395- Ok ( _) => {
396- debug ! ( "Successfully imported tool set" ) ;
397- }
397+ None ,
398+ ) ;
399+
400+ let new_activation_script = match std:: env:: consts:: OS {
401+ "windows" => format ! (
402+ "{}\\ Microsoft.PowerShell_profile.ps1" ,
403+ version_path. to_str( ) . unwrap( )
404+ ) ,
405+ _ => format ! (
406+ "{}/activate_idf_{}.sh" ,
407+ version_path. to_str( ) . unwrap( ) ,
408+ tool_set. idf_version
409+ ) ,
410+ } ;
411+ let installation = IdfInstallation {
412+ id : tool_set. id . to_string ( ) ,
413+ activation_script : new_activation_script,
414+ path : tool_set. idf_location ,
415+ name : tool_set. idf_version ,
416+ python : tool_set. system_python_executable_path ,
417+ idf_tools_path : new_idf_tools_path. clone ( ) ,
418+ } ;
419+ let config_path = get_default_config_path ( ) ;
420+ let mut current_config = match IdfConfig :: from_file ( & config_path) {
421+ Ok ( config) => config,
398422 Err ( e) => {
399- return Err ( anyhow ! ( "Failed to import tool set: {}" , e) ) ;
423+ debug ! ( "Config file not found, creating a new one: {}" , e) ;
424+ IdfConfig :: default ( )
400425 }
426+ } ;
427+ match current_config. idf_installed . iter ( ) . find ( |install| install. path == new_idf_tools_path) {
428+ Some ( _) => {
429+ debug ! ( "IDF installation already exists in config, skipping" ) ;
430+ return Ok ( ( ) ) ;
431+ }
432+ None => {
433+ debug ! ( "Adding new IDF installation to config" ) ;
434+ current_config. idf_installed . push ( installation) ;
435+ match current_config. to_file ( config_path, true , false ) {
436+ Ok ( _) => {
437+ debug ! ( "Updated config file with new tool set" ) ;
438+ return Ok ( ( ) ) ;
439+ }
440+ Err ( e) => {
441+ return Err ( anyhow ! ( "Failed to update config file: {}" , e) ) ;
442+ }
443+ }
444+ }
401445 }
446+
402447 }
403448 Ok ( ( ) )
404449}
0 commit comments