@@ -22,6 +22,7 @@ use console::style;
22
22
use flate2:: read:: GzDecoder ;
23
23
use indicatif:: { ProgressBar , ProgressStyle } ;
24
24
use indoc:: formatdoc;
25
+ use itertools:: Itertools ;
25
26
use regex:: Regex ;
26
27
use semver:: Version ;
27
28
#[ cfg( not( windows) ) ]
@@ -748,6 +749,7 @@ pub fn install_non_db_version(
748
749
749
750
pub fn garbage_collect_versions (
750
751
prune_linked : bool ,
752
+ prune_orphans : bool ,
751
753
config_data : & mut JuliaupConfig ,
752
754
paths : & GlobalPaths ,
753
755
) -> Result < ( ) > {
@@ -773,22 +775,79 @@ pub fn garbage_collect_versions(
773
775
config_data. installed_versions . remove ( & version_to_delete) ;
774
776
}
775
777
778
+ // GC for DirectDownloadChannel channels
779
+ let jl_dirs: Vec < _ > = std:: fs:: read_dir ( & paths. juliauphome ) ?
780
+ . into_iter ( )
781
+ . filter_map_ok ( |r| {
782
+ if r. path ( ) . is_dir ( ) {
783
+ Some ( r. path ( ) )
784
+ } else {
785
+ None
786
+ }
787
+ } )
788
+ . filter_map_ok ( |r| {
789
+ let dirname = r. file_name ( ) ?. to_str ( ) ?;
790
+ if dirname. starts_with ( "julia-" ) {
791
+ Some ( dirname. to_owned ( ) )
792
+ } else {
793
+ None
794
+ }
795
+ } )
796
+ . filter ( |r| r. is_ok ( ) )
797
+ . map ( |r| r. unwrap ( ) ) // This is safe, since we only have the Ok variants
798
+ . collect ( ) ;
799
+
800
+ if prune_orphans {
801
+ for jl_dir in jl_dirs {
802
+ if config_data
803
+ . installed_channels
804
+ . iter ( )
805
+ . all ( |( _, detail) | match & detail {
806
+ JuliaupConfigChannel :: SystemChannel { version } => {
807
+ let channel_path = & config_data. installed_versions [ version]
808
+ . path
809
+ . replace ( "./" , "" ) ;
810
+ * channel_path != jl_dir
811
+ }
812
+ JuliaupConfigChannel :: DirectDownloadChannel {
813
+ path,
814
+ url : _,
815
+ local_etag : _,
816
+ server_etag : _,
817
+ version : _,
818
+ } => {
819
+ let channel_path = path. replace ( "./" , "" ) ;
820
+ channel_path != jl_dir
821
+ }
822
+ JuliaupConfigChannel :: LinkedChannel {
823
+ command : _,
824
+ args : _,
825
+ } => true ,
826
+ } )
827
+ {
828
+ if std:: fs:: remove_dir_all ( paths. juliauphome . join ( & jl_dir) ) . is_err ( ) {
829
+ eprintln ! ( "WARNING: Failed to delete {}. You can try to delete at a later point by running `juliaup gc`." , & jl_dir)
830
+ }
831
+ }
832
+ }
833
+ }
834
+
776
835
if prune_linked {
777
- let mut channels_to_uninstall : Vec < String > = Vec :: new ( ) ;
836
+ let mut linked_channels_to_uninstall : Vec < String > = Vec :: new ( ) ;
778
837
for ( installed_channel, detail) in & config_data. installed_channels {
779
838
match & detail {
780
839
JuliaupConfigChannel :: LinkedChannel {
781
840
command : cmd,
782
841
args : _,
783
842
} => {
784
843
if !is_valid_julia_path ( & PathBuf :: from ( cmd) ) {
785
- channels_to_uninstall . push ( installed_channel. clone ( ) ) ;
844
+ linked_channels_to_uninstall . push ( installed_channel. clone ( ) ) ;
786
845
}
787
846
}
788
847
_ => ( ) ,
789
848
}
790
849
}
791
- for channel in channels_to_uninstall {
850
+ for channel in linked_channels_to_uninstall {
792
851
remove_symlink ( & format ! ( "julia-{}" , & channel) ) ?;
793
852
config_data. installed_channels . remove ( & channel) ;
794
853
}
0 commit comments