diff --git a/completion/available/gem.completion.bash b/completion/available/gem.completion.bash index de986e08..1967d38d 100644 --- a/completion/available/gem.completion.bash +++ b/completion/available/gem.completion.bash @@ -1,36 +1,32 @@ #!/usr/bin/env bash # Completion for gem -_installcomp() { - if [ -z "$REMOTE_GEMS" ] - then +__gem_installcomp() { + if [[ -z "$REMOTE_GEMS" ]] ; then REMOTE_GEMS=( $(gem list --remote --no-versions | tr '\n' ' ') ) fi - local cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W "${REMOTE_GEMS[*]}" -- $cur) ) } -_uninstallcomp() { - if [ -z "$LOCAL_GEMS" ] - then +__gem_uninstallcomp() { + if [[ -z "$LOCAL_GEMS" ]] ; then LOCAL_GEMS=( $(gem list --no-versions | sed 's/\*\*\* LOCAL GEMS \*\*\*//' | tr '\n' ' ') ) fi - local cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W "${LOCAL_GEMS[*]}" -- $cur) ) } -_gem() { +__gem_comp() { local cur=${COMP_WORDS[COMP_CWORD]} local prev=${COMP_WORDS[COMP_CWORD-1]} case $prev in install) - _installcomp + __gem_installcomp return 0 ;; uninstall) - _uninstallcomp + __gem_uninstallcomp return 0 ;; esac @@ -38,4 +34,4 @@ _gem() { COMPREPLY=( $(compgen -W "${commands[*]}" -- $cur) ) } -complete -F _gem gem +complete -F __gem_comp gem diff --git a/completion/available/rbenv.completion.bash b/completion/available/rbenv.completion.bash new file mode 100644 index 00000000..5a10cef8 --- /dev/null +++ b/completion/available/rbenv.completion.bash @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +# Bash completion support for rbenv + +if _command_exists rbenv && [[ -r "$(dirname $(readlink -f $(which rbenv)))/../completions/rbenv.bash" ]] ; then + source "$(dirname $(readlink -f $(which rbenv)))/../completions/rbenv.bash" +fi diff --git a/plugins/available/rbenv.plugin.bash b/plugins/available/rbenv.plugin.bash index 2b01669d..1ac85a7e 100644 --- a/plugins/available/rbenv.plugin.bash +++ b/plugins/available/rbenv.plugin.bash @@ -1,7 +1,25 @@ cite about-plugin about-plugin 'load rbenv, if you are using it' -export RBENV_ROOT="$HOME/.rbenv" -pathmunge "$RBENV_ROOT/bin" +# Load after basher +# BASH_IT_LOAD_PRIORITY: 275 -[[ `which rbenv 2>/dev/null` ]] && eval "$(rbenv init - bash)" +# Don't modify the environment if we can't find the tool: +# - Check if in $PATH already +# - Check if installed manually to $RBENV_ROOT +# - Check if installed manually to $HOME +_command_exists rbenv || + [[ -n "$RBENV_ROOT" && -x "$RBENV_ROOT/bin/rbenv" ]] || + [[ -x "$HOME/.rbenv/bin/rbenv" ]] || + return 0 + +# Set RBENV_ROOT, if not already set +export RBENV_ROOT="${RBENV_ROOT:-$HOME/.rbenv}" + +# Add RBENV_ROOT/bin to PATH, if that's where it's installed +if ! _command_exists rbenv && [[ -x "$RBENV_ROOT/bin/rbenv" ]] ; then + pathmunge "$RBENV_ROOT/bin" +fi + +# Initialize rbenv +eval "$(rbenv init - bash)" diff --git a/plugins/available/ruby.plugin.bash b/plugins/available/ruby.plugin.bash index 4ab891d9..e1c3fe85 100644 --- a/plugins/available/ruby.plugin.bash +++ b/plugins/available/ruby.plugin.bash @@ -1,16 +1,23 @@ cite about-plugin about-plugin 'ruby and rubygems specific functions and settings' -# Make commands installed with 'gem install --user-install' available -# ~/.gem/ruby/${RUBY_VERSION}/bin/ -if which ruby >/dev/null && which gem >/dev/null; then - pathmunge "$(ruby -e 'print Gem.user_dir')/bin" after -fi +# Load after rbenv +# BASH_IT_LOAD_PRIORITY: 285 + +# Check ruby version to ensure rbenv can find ruby +{ _command_exists ruby && ruby --version &>/dev/null ; } || return 0 + +# Check gem version to ensure rbenv can find gem +{ _command_exists gem && gem --version &>/dev/null ; } || return 0 function remove_gem { about 'removes installed gem' param '1: installed gem name' group 'ruby' - gem list | grep $1 | awk '{ print $1; }' | xargs sudo gem uninstall + { _command_exists gem && gem --version &>/dev/null ; } || return 1 + gem list | grep $1 | awk '{ print $1; }' | xargs gem uninstall } + +# Make commands installed with 'gem install --user-install' are available +pathmunge "$(ruby -e 'print Gem.user_dir')/bin" diff --git a/test/plugins/ruby.plugin.bats b/test/plugins/ruby.plugin.bats index a45411e2..a820dbc0 100755 --- a/test/plugins/ruby.plugin.bats +++ b/test/plugins/ruby.plugin.bats @@ -3,13 +3,19 @@ load ../test_helper load ../../lib/helpers load ../../lib/composure -load ../../plugins/available/ruby.plugin function local_setup { setup_test_fixture export OLD_PATH="$PATH" export PATH="/usr/bin:/bin:/usr/sbin" + + if ! _command_exists ruby || ! ruby --version &>/dev/null ; then + function ruby { echo -n "${HOME}/.gem/ruby/0.0.0" ; } + fi + if ! _command_exists gem || ! gem --version &>/dev/null ; then + function gem { return 0 ; } + fi } function local_teardown { @@ -18,17 +24,15 @@ function local_teardown { } @test "plugins ruby: remove_gem is defined" { + load ../../plugins/available/ruby.plugin run type remove_gem assert_line -n 1 "remove_gem () " } @test "plugins ruby: PATH includes ~/.gem/ruby/bin" { - if ! which ruby >/dev/null; then - skip 'ruby not installed' - fi - load ../../plugins/available/ruby.plugin - - local last_path_entry=$(echo $PATH | tr ":" "\n" | tail -1) - [[ "${last_path_entry}" == "${HOME}"/.gem/ruby/*/bin ]] + local last_path_entry=$(echo $PATH | tr ":" "\n" | head -1) + # check that the path matches expectations, without getting caught up on the version number + assert_equal "${last_path_entry##/*/}" "bin" + assert_equal "${last_path_entry%/*/*}" "${HOME}/.gem/ruby" }