@@ -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) ) ]
@@ -687,6 +688,7 @@ pub fn install_non_db_version(
687
688
688
689
pub fn garbage_collect_versions (
689
690
prune_linked : bool ,
691
+ prune_orphans : bool ,
690
692
config_data : & mut JuliaupConfig ,
691
693
paths : & GlobalPaths ,
692
694
) -> Result < ( ) > {
@@ -712,22 +714,79 @@ pub fn garbage_collect_versions(
712
714
config_data. installed_versions . remove ( & version_to_delete) ;
713
715
}
714
716
717
+ // GC for DirectDownloadChannel channels
718
+ let jl_dirs: Vec < _ > = std:: fs:: read_dir ( & paths. juliauphome ) ?
719
+ . into_iter ( )
720
+ . filter_map_ok ( |r| {
721
+ if r. path ( ) . is_dir ( ) {
722
+ Some ( r. path ( ) )
723
+ } else {
724
+ None
725
+ }
726
+ } )
727
+ . filter_map_ok ( |r| {
728
+ let dirname = r. file_name ( ) ?. to_str ( ) ?;
729
+ if dirname. starts_with ( "julia-" ) {
730
+ Some ( dirname. to_owned ( ) )
731
+ } else {
732
+ None
733
+ }
734
+ } )
735
+ . filter ( |r| r. is_ok ( ) )
736
+ . map ( |r| r. unwrap ( ) ) // This is safe, since we only have the Ok variants
737
+ . collect ( ) ;
738
+
739
+ if prune_orphans {
740
+ for jl_dir in jl_dirs {
741
+ if config_data
742
+ . installed_channels
743
+ . iter ( )
744
+ . all ( |( _, detail) | match & detail {
745
+ JuliaupConfigChannel :: SystemChannel { version } => {
746
+ let channel_path = & config_data. installed_versions [ version]
747
+ . path
748
+ . replace ( "./" , "" ) ;
749
+ * channel_path != jl_dir
750
+ }
751
+ JuliaupConfigChannel :: DirectDownloadChannel {
752
+ path,
753
+ url : _,
754
+ local_etag : _,
755
+ server_etag : _,
756
+ version : _,
757
+ } => {
758
+ let channel_path = path. replace ( "./" , "" ) ;
759
+ channel_path != jl_dir
760
+ }
761
+ JuliaupConfigChannel :: LinkedChannel {
762
+ command : _,
763
+ args : _,
764
+ } => true ,
765
+ } )
766
+ {
767
+ if std:: fs:: remove_dir_all ( paths. juliauphome . join ( & jl_dir) ) . is_err ( ) {
768
+ eprintln ! ( "WARNING: Failed to delete {}. You can try to delete at a later point by running `juliaup gc`." , & jl_dir)
769
+ }
770
+ }
771
+ }
772
+ }
773
+
715
774
if prune_linked {
716
- let mut channels_to_uninstall : Vec < String > = Vec :: new ( ) ;
775
+ let mut linked_channels_to_uninstall : Vec < String > = Vec :: new ( ) ;
717
776
for ( installed_channel, detail) in & config_data. installed_channels {
718
777
match & detail {
719
778
JuliaupConfigChannel :: LinkedChannel {
720
779
command : cmd,
721
780
args : _,
722
781
} => {
723
782
if !is_valid_julia_path ( & PathBuf :: from ( cmd) ) {
724
- channels_to_uninstall . push ( installed_channel. clone ( ) ) ;
783
+ linked_channels_to_uninstall . push ( installed_channel. clone ( ) ) ;
725
784
}
726
785
}
727
786
_ => ( ) ,
728
787
}
729
788
}
730
- for channel in channels_to_uninstall {
789
+ for channel in linked_channels_to_uninstall {
731
790
remove_symlink ( & format ! ( "julia-{}" , & channel) ) ?;
732
791
config_data. installed_channels . remove ( & channel) ;
733
792
}
0 commit comments