@@ -2,24 +2,28 @@ use crate::dfxvm;
22use crate :: dfxvm_init:: {
33 plan:: { DfxVersion , Plan , PlanOptions } ,
44 ui,
5- ui:: Confirmation ,
5+ ui:: { select_deletion_strategy , Confirmation , DeletionStrategy } ,
66} ;
77use crate :: error:: {
88 dfxvm_init,
9- dfxvm_init:: { ExecutePlanError , UpdateProfileScriptsError } ,
10- fs:: WriteFileError ,
9+ dfxvm_init:: {
10+ DeleteDfxOnPathError , DeleteDfxOnPathError :: CallSudoRm , ExecutePlanError ,
11+ UpdateProfileScriptsError ,
12+ } ,
13+ fs:: { RemoveFileError , WriteFileError } ,
1114} ;
12- use crate :: fs:: { append_to_file, create_dir_all, read_to_string} ;
15+ use crate :: fs:: { append_to_file, create_dir_all, read_to_string, remove_file } ;
1316use crate :: installation:: { env_file_contents, install_binaries, ProfileScript } ;
1417use crate :: locations:: Locations ;
15- use std:: path:: Path ;
18+ use std:: path:: { Path , PathBuf } ;
19+ use std:: process:: Command ;
1620
1721pub async fn initialize (
1822 options : PlanOptions ,
1923 confirmation : Option < Confirmation > ,
2024 locations : & Locations ,
2125) -> Result < ( ) , dfxvm_init:: Error > {
22- let mut plan = Plan :: new ( options, locations) ;
26+ let mut plan = Plan :: new ( options, locations) ? ;
2327
2428 ui:: display:: introduction ( & plan) ;
2529
@@ -46,6 +50,11 @@ pub async fn initialize(
4650}
4751
4852pub async fn execute ( plan : & Plan , locations : & Locations ) -> Result < ( ) , ExecutePlanError > {
53+ remove_uninstall_script ( locations) ?;
54+ if plan. options . delete_dfx_on_path {
55+ delete_dfx_on_path ( plan) ?;
56+ }
57+
4958 create_dir_all ( & plan. bin_dir ) ?;
5059
5160 create_env_file ( & plan. env_path ) ?;
@@ -64,6 +73,60 @@ pub async fn execute(plan: &Plan, locations: &Locations) -> Result<(), ExecutePl
6473 Ok ( ( ) )
6574}
6675
76+ fn remove_uninstall_script ( locations : & Locations ) -> Result < ( ) , RemoveFileError > {
77+ let path = locations. dfinity_cache_dir ( ) . join ( "uninstall.sh" ) ;
78+ if path. exists ( ) {
79+ remove_file ( & path) ?;
80+ info ! ( "deleted: {}" , path. display( ) ) ;
81+ }
82+ Ok ( ( ) )
83+ }
84+
85+ fn delete_dfx_on_path ( plan : & Plan ) -> Result < ( ) , DeleteDfxOnPathError > {
86+ loop {
87+ let remaining = delete_and_filter ( & plan. dfx_on_path ) ;
88+
89+ if remaining. is_empty ( ) {
90+ break Ok ( ( ) ) ;
91+ }
92+
93+ ui:: display:: need_to_delete_old_dfx ( plan) ;
94+
95+ match select_deletion_strategy ( ) ? {
96+ DeletionStrategy :: Manual => { }
97+ DeletionStrategy :: CallSudo => sudo_rm ( remaining) ?,
98+ DeletionStrategy :: DontDelete => break Ok ( ( ) ) ,
99+ }
100+ }
101+ }
102+
103+ fn delete_and_filter ( dfx_on_path : & [ PathBuf ] ) -> Vec < PathBuf > {
104+ dfx_on_path
105+ . iter ( )
106+ . filter ( |p| {
107+ if !p. exists ( ) {
108+ false
109+ } else if remove_file ( p) . is_ok ( ) {
110+ info ! ( "deleted: {}" , p. display( ) ) ;
111+ false
112+ } else {
113+ true
114+ }
115+ } )
116+ . cloned ( )
117+ . collect ( )
118+ }
119+
120+ fn sudo_rm ( remaining : Vec < PathBuf > ) -> Result < ( ) , DeleteDfxOnPathError > {
121+ let _ = Command :: new ( "sudo" )
122+ . arg ( "rm" )
123+ . arg ( "-f" )
124+ . args ( remaining. iter ( ) . map ( |p| ( * p) . clone ( ) . into_os_string ( ) ) )
125+ . status ( )
126+ . map_err ( CallSudoRm ) ?;
127+ Ok ( ( ) )
128+ }
129+
67130fn create_env_file ( path : & Path ) -> Result < ( ) , WriteFileError > {
68131 info ! ( "creating {}" , path. display( ) ) ;
69132 crate :: fs:: write ( path, env_file_contents ( ) )
0 commit comments