rvmrc_env 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. # Read variables from file, if there are any variables asks user to trust it,
  3. #
  4. # Usage:
  5. # __rvm_file_load_env_and_trust <file_to_load> [variables_prefix]
  6. #
  7. # @param file_to_load the file to load variables from
  8. # @param variables_prefix optional filter for variables, ex: "#ruby-env-"
  9. # @env __file_env_variables contains the variables loaded from file, empty if user does not trust the file
  10. #
  11. __rvm_file_load_env_and_trust()
  12. {
  13. [[ -f "$1" ]] || return 0
  14. __rvm_file_load_env "$1" "${2:-}"
  15. if
  16. (( ${#__file_env_variables[@]} == 0 )) ||
  17. __rvm_check_rvmrc_trustworthiness "$1"
  18. then
  19. true
  20. else
  21. rvm_debug "Envirionment variables variables from '$1' wont be loaded because of lack of trust (status=$?)."
  22. __file_env_variables=()
  23. fi
  24. }
  25. # Read variables from file,
  26. #
  27. # Usage:
  28. # __rvm_file_load_env_and_trust <file_to_load> [variables_prefix]
  29. #
  30. # @param file_to_load the file to load variables from
  31. # @param variables_prefix optional filter for variables, ex: "#ruby-env-"
  32. # @env __file_env_variables contains the variables loaded from file,
  33. #
  34. __rvm_file_load_env()
  35. {
  36. \typeset -a __sed_commands
  37. __sed_commands=()
  38. if [[ -n "${2:-}" ]]
  39. then __sed_commands+=( -e "/^$2/ !d" -e "s/^$2//" ) # filter other content and remove prefix
  40. else __sed_commands+=( -e "/^#/ d" -e "/^$/ d" ) # remove comments and empty lines
  41. fi
  42. __rvm_read_lines __file_env_variables <( { cat "$1"; echo ""; } | __rvm_sed "${__sed_commands[@]}" )
  43. }
  44. # Resets previously loaded environment,
  45. # if any new variables are available - loads them,
  46. #
  47. # @env rvm_saved_env used to restore variables, emptied out on the end
  48. # @env __file_env_variables used to load new variables
  49. #
  50. __rvm_file_set_env()
  51. {
  52. __rvm_file_env_check_unload
  53. __rvm_set_env "rvm_saved_env" "${__file_env_variables[@]}"
  54. }
  55. # Resets previously loaded environment,
  56. #
  57. # @env rvm_saved_env used to restore variables, emptied out on the end
  58. #
  59. __rvm_file_env_check_unload()
  60. {
  61. if (( ${#rvm_saved_env[@]} > 0 ))
  62. then __rvm_set_env "" "${rvm_saved_env[@]}"
  63. fi
  64. rvm_saved_env=()
  65. }
  66. # Sets environment variables,
  67. #
  68. # Usage:
  69. # __rvm_set_env <save_to> [var1 [var2]...]
  70. #
  71. # @param save_to store previous values of environment variables in this variable if it's provided
  72. # @param var... "key=value" pairs for the environment to set to
  73. #
  74. # @env ${save_to} "key=value" pairs of saved environment
  75. #
  76. __rvm_set_env()
  77. {
  78. \typeset __save_to __set __key __value
  79. __save_to="$1"
  80. shift
  81. for __set in "$@"
  82. do
  83. __key="${__set%%=*}"
  84. __value="${__set#*=}"
  85. case "$__value" in
  86. (\"*\")
  87. __value="${__value#\"}"
  88. __value="${__value%\"}"
  89. ;;
  90. (\'*\')
  91. __value="${__value#\'}"
  92. __value="${__value%\'}"
  93. ;;
  94. esac
  95. rvm_debug "key=$__key; value=$__value;"
  96. if [[ -n "${__save_to}" ]]
  97. then eval "${__save_to}+=( \"\${__key}=\${${__key}}\" )"
  98. fi
  99. if [[ -n "${__value}" ]]
  100. then eval "export \${__key}=\"\${__value}\""
  101. else eval "unset \${__key}"
  102. fi
  103. done
  104. }