renv.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2039
  3. renv() {
  4. case "$1" in
  5. status | s)
  6. [ -n "${GEM_HOME:-}" ] && echo "GEM_HOME='$GEM_HOME'"
  7. [ -n "${GEM_PATH:-}" ] && echo "GEM_PATH='$GEM_PATH'"
  8. echo "PATH='$PATH'" && return
  9. ;;
  10. reset | r)
  11. echo ">>>> renv not set. Try: \`renv'"
  12. if [ -z "${RENV_ORIG_PATH:-}" ]; then
  13. return 3
  14. fi
  15. if [ -n "${RENV_ORIG_GEM_HOME:-}" ]; then
  16. GEM_HOME=$RENV_ORIG_GEM_HOME
  17. else
  18. unset GEM_HOME
  19. fi
  20. if [ -n "${RENV_ORIG_GEM_PATH:-}" ]; then
  21. GEM_PATH=$RENV_ORIG_GEM_PATH
  22. else
  23. unset GEM_PATH
  24. fi
  25. if [ -n "${RENV_ORIG_PATH:-}" ]; then
  26. PATH=$RENV_ORIG_PATH
  27. fi
  28. unset RENV_ORIG_GEM_HOME RENV_ORIG_GEM_PATH RENV_ORIG_PATH
  29. echo "---> renv is reset, GEM_HOME is ${GEM_HOME:-"<unset>"}"
  30. ;;
  31. "")
  32. echo ">>>> renv already set, GEM_HOME is $GEM_HOME. Try: \`renv reset'"
  33. if [ -n "${RENV_ORIG_PATH:-}" ]; then
  34. return 9
  35. fi
  36. if ! command -v ruby >/dev/null; then
  37. echo ">>>> 'ruby' program not found in PATH='${PATH:-}', aborting"
  38. return 10
  39. fi
  40. eval "$(
  41. ruby -rrubygems - <<-'EOF'
  42. puts "local ruby_engine=#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'}"
  43. puts "local ruby_version=#{RUBY_VERSION}"
  44. puts "local gem_path='#{Gem.path.join(':')}'"
  45. EOF
  46. )"
  47. # shellcheck disable=SC2154
  48. local gem_dir="$PWD/.gem/$ruby_engine/$ruby_version"
  49. export RENV_ORIG_PATH="$PATH"
  50. export RENV_ORIG_GEM_HOME="$GEM_HOME"
  51. export RENV_ORIG_GEM_PATH="$GEM_PATH"
  52. export PATH="$gem_dir/bin:$PATH"
  53. export GEM_HOME="$gem_dir"
  54. # shellcheck disable=SC2154
  55. export GEM_PATH="$gem_dir:$gem_path"
  56. echo "---> renv is set, GEM_HOME is $GEM_HOME"
  57. ;;
  58. help | h | usage | --help | -h | *)
  59. echo "usage: renv [reset|status]"
  60. return
  61. ;;
  62. esac
  63. }