diff --git a/fn/czruby_purge b/fn/czruby_purge index 1b9a76b..fcba484 100644 --- a/fn/czruby_purge +++ b/fn/czruby_purge @@ -9,12 +9,17 @@ czruby_purge() { return 1 fi - # Store current default ruby - default_ruby="$(greadlink -f "$czruby_datadir/default" 2>/dev/null)" + # Store current default ruby (use readlink on Linux, greadlink on macOS) + if command -v greadlink &>/dev/null; then + default_ruby="$(greadlink -f "$czruby_datadir/default" 2>/dev/null)" + else + default_ruby="$(readlink -f "$czruby_datadir/default" 2>/dev/null)" + fi default_ruby="${default_ruby:t}" # Process each file in czruby_datadir - for file in "$czruby_datadir"/*; do + # Use (N) glob qualifier to handle empty directory without error + for file in "$czruby_datadir"/*(N); do [[ -f "$file" ]] || continue key="${file:t}" @@ -44,10 +49,14 @@ czruby_purge() { rm "$file" ((purged++)) - # If this was the default, remove the default symlink + # If this was the default, reset to system if [[ "$key" == "$default_ruby" ]]; then - rm "$czruby_datadir/default" - czruby_set_default system + rm -f "$czruby_datadir/default" + RUBIES_DEFAULT="system" + # Create symlink to system config if it exists + if [[ -f "$czruby_datadir/system" ]]; then + ln -fs "$czruby_datadir/system" "$czruby_datadir/default" + fi fi fi done diff --git a/fn/czruby_reset b/fn/czruby_reset index 5c4b800..5b07321 100644 --- a/fn/czruby_reset +++ b/fn/czruby_reset @@ -6,13 +6,23 @@ local ruby_root="${2:-$RUBY_ROOT}" local excludes=() - # TODO consider replacing loop - for place in "$gem_path"; do + # Remove old RUBY_ROOT/bin from path + if [[ -n "$RUBY_ROOT" ]]; then + excludes+=("$RUBY_ROOT/bin") + fi + + # Remove old GEM_HOME/bin from path + if [[ -n "$GEM_HOME" ]]; then + excludes+=("$GEM_HOME/bin") + fi + + # Remove all gem_path bin directories from path + for place in $gem_path; do local bin="$place/bin" - excludes=("$bin" "${excludes[@]}") # Append to the array correctly + excludes+=("$bin") done - if [[ $#gem_path > 0 ]]; then + if [[ ${#excludes[@]} -gt 0 ]]; then # remove any excluded paths path=(${path:|excludes}) fi diff --git a/fn/czruby_set_default b/fn/czruby_set_default index 8256b91..79d6741 100644 --- a/fn/czruby_set_default +++ b/fn/czruby_set_default @@ -1,7 +1,8 @@ # Sets the default Ruby environment to use when no specific version is specified. { + # Check for empty argument before applying default + [[ -z "$1" ]] && return 1 local choice="${1:-system}" - [[ -z $choice ]] && return 1 if [[ -z ${(M)rubies:#*$choice} ]]; then print "That ruby is not available\n\n" czruby