renv.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. eval "$(
  37. ruby -rrubygems - <<-'EOF'
  38. puts "local ruby_engine=#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'}"
  39. puts "local ruby_version=#{RUBY_VERSION}"
  40. puts "local gem_path='#{Gem.path.join(':')}'"
  41. EOF
  42. )"
  43. # shellcheck disable=SC2154
  44. local gem_dir="$PWD/.gem/$ruby_engine/$ruby_version"
  45. export RENV_ORIG_PATH="$PATH"
  46. export RENV_ORIG_GEM_HOME="$GEM_HOME"
  47. export RENV_ORIG_GEM_PATH="$GEM_PATH"
  48. export PATH="$gem_dir/bin:$PATH"
  49. export GEM_HOME="$gem_dir"
  50. # shellcheck disable=SC2154
  51. export GEM_PATH="$gem_dir:$gem_path"
  52. echo "---> renv is set, GEM_HOME is $GEM_HOME"
  53. ;;
  54. help | h | usage | --help | -h | *)
  55. echo "usage: renv [reset|status]"
  56. return
  57. ;;
  58. esac
  59. }