Skip to content

Commit

Permalink
add 'rvm export' and 'rvm unexport' actions.
Browse files Browse the repository at this point in the history
these actions allow environment variables to be set per-project, by
setting them in the project rvmrc. unlike when using the 'export'
builtin, the changes to the environment are reverted when leaving
the environment or resetting rvm.
  • Loading branch information
adammck committed Jan 8, 2011
1 parent b92a773 commit e01bb51
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
24 changes: 22 additions & 2 deletions scripts/cli
Expand Up @@ -35,7 +35,7 @@ __rvm_parse_args()
if [[ $# -gt 0 ]] ; then next_token="$1" ; shift ; else next_token="" ; fi

case "$rvm_token" in
fetch|version|srcdir|reset|debug|reload|update|monitor|notes|implode|seppuku|question|answer|env)
fetch|version|srcdir|reset|debug|reload|update|monitor|notes|implode|seppuku|question|answer|env|unexport)
rvm_action=$rvm_token
;;

Expand Down Expand Up @@ -265,6 +265,18 @@ __rvm_parse_args()
rvm_ruby_args="${rvm_token/%ss/s}"
;;

export)
if [[ ! -z "$next_token" ]] ; then
rvm_export_args="$next_token $@"
rvm_action="export"
rvm_parse_break=1

else
rvm_action="error"
rvm_error_message="rvm export must be followed by a NAME=VALUE argument"
fi
;;

-v|--version)
if [[ -z "$next_token" ]] ; then
rvm_action="version"
Expand Down Expand Up @@ -658,7 +670,7 @@ rvm()

rvm_action="${rvm_action:-usage}"

export GEM_HOME GEM_PATH rvm_action rvm_archflags rvm_bin_flag rvm_bin_path rvm_clang_flag rvm_configure_flags rvm_debug_flag rvm_delete_flag rvm_docs_type rvm_dump_environment_flag rvm_error_message rvm_file_name rvm_gemdir_flag rvm_gemset_name rvm_head_flag rvm_install_arguments rvm_install_on_use_flag rvm_interactive_flag rvm_llvm_flag rvm_loaded_flag rvm_make_flags rvm_niceness rvm_only_path_flag rvm_parse_break rvm_patch_names rvm_patch_original_pwd rvm_pretty_print_flag rvm_prior_cc rvm_proxy rvm_quiet_flag rvm_ree_options rvm_reload_flag rvm_remove_flag rvm_ruby_alias rvm_ruby_aliases rvm_ruby_args rvm_ruby_file rvm_ruby_gem_home rvm_ruby_interpreter rvm_ruby_load_path rvm_ruby_make rvm_ruby_make_install rvm_ruby_patch_level rvm_ruby_repo_url rvm_ruby_repo_branch rvm_ruby_require rvm_ruby_string rvm_ruby_strings rvm_ruby_version rvm_script_name rvm_sdk rvm_silent_flag rvm_system_flag rvm_token rvm_trace_flag rvm_use_flag rvm_user_flag rvm_verbose_flag rvm_wrapper_name rvm_version rvm_static_flag
export GEM_HOME GEM_PATH rvm_action rvm_archflags rvm_bin_flag rvm_bin_path rvm_clang_flag rvm_configure_flags rvm_debug_flag rvm_delete_flag rvm_docs_type rvm_dump_environment_flag rvm_error_message rvm_file_name rvm_gemdir_flag rvm_gemset_name rvm_head_flag rvm_install_arguments rvm_install_on_use_flag rvm_interactive_flag rvm_llvm_flag rvm_loaded_flag rvm_make_flags rvm_niceness rvm_only_path_flag rvm_parse_break rvm_patch_names rvm_patch_original_pwd rvm_pretty_print_flag rvm_prior_cc rvm_proxy rvm_quiet_flag rvm_ree_options rvm_reload_flag rvm_remove_flag rvm_ruby_alias rvm_ruby_aliases rvm_ruby_args rvm_ruby_file rvm_ruby_gem_home rvm_ruby_interpreter rvm_ruby_load_path rvm_ruby_make rvm_ruby_make_install rvm_ruby_patch_level rvm_ruby_repo_url rvm_ruby_repo_branch rvm_ruby_require rvm_ruby_string rvm_ruby_strings rvm_ruby_version rvm_script_name rvm_sdk rvm_silent_flag rvm_system_flag rvm_token rvm_trace_flag rvm_use_flag rvm_user_flag rvm_verbose_flag rvm_wrapper_name rvm_version rvm_static_flag rvm_export_args

case "$rvm_action" in
use) __rvm_use ;;
Expand Down Expand Up @@ -760,6 +772,14 @@ rvm()
fi
;;

export)
__rvm_export "$rvm_export_args"
;;

unexport)
__rvm_unset_exports
;;

error) false ;;

*)
Expand Down
48 changes: 47 additions & 1 deletion scripts/utility
Expand Up @@ -48,7 +48,7 @@ __rvm_teardown()

fi

unset rvm_ruby_strings rvm_head_flag rvm_prior_cc next_token rvm_bin_path rvm_error_message rvm_gems_cache_path rvm_gems_path rvm_gemset_name rvm_interactive_flag rvm_man_path rvm_parse_break rvm_rc_files rvm_ruby_binary rvm_ruby_gem_home rvm_ruby_gem_path rvm_ruby_home rvm_ruby_interpreter rvm_ruby_irbrc rvm_ruby_log_path rvm_ruby_major_version rvm_ruby_minor_version rvm_ruby_package_name rvm_ruby_patch_level rvm_ruby_release_version rvm_ruby_repo_url rvm_ruby_repo_branch rvm_ruby_revision rvm_ruby_selected_flag rvm_ruby_tag rvm_ruby_version rvm_selfcontained rvm_token rvm_ruby_load_path rvm_path_flag rvm_ruby_require rvm_action rvm_ruby_package_file rvm_ruby_name rvm_static_flag
unset rvm_ruby_strings rvm_head_flag rvm_prior_cc next_token rvm_bin_path rvm_error_message rvm_gems_cache_path rvm_gems_path rvm_gemset_name rvm_interactive_flag rvm_man_path rvm_parse_break rvm_rc_files rvm_ruby_binary rvm_ruby_gem_home rvm_ruby_gem_path rvm_ruby_home rvm_ruby_interpreter rvm_ruby_irbrc rvm_ruby_log_path rvm_ruby_major_version rvm_ruby_minor_version rvm_ruby_package_name rvm_ruby_patch_level rvm_ruby_release_version rvm_ruby_repo_url rvm_ruby_repo_branch rvm_ruby_revision rvm_ruby_selected_flag rvm_ruby_tag rvm_ruby_version rvm_selfcontained rvm_token rvm_ruby_load_path rvm_path_flag rvm_ruby_require rvm_action rvm_ruby_package_file rvm_ruby_name rvm_static_flag rvm_export_args

[[ ${rvm_dump_environment_flag:-0} -eq 1 ]] && __rvm_dump_environment

Expand Down Expand Up @@ -688,6 +688,8 @@ __rvm_reset()

__rvm_unset_ruby_variables

__rvm_unset_exports

configs=(system_ruby system_gem_path system_user_gem_path)

for system_config in "${configs[@]}" ; do
Expand Down Expand Up @@ -1421,6 +1423,8 @@ __rvm_project_rvmrc()

unset rvm_rvmrc_cwd rvm_previous_environment

__rvm_unset_exports

fi
break
else
Expand All @@ -1438,6 +1442,8 @@ __rvm_project_rvmrc()
rvm_previous_environment="$(__rvm_environment_identifier)"

rvm_rvmrc_cwd="$cwd"

__rvm_unset_exports

source "$cwd/.rvmrc"

Expand Down Expand Up @@ -1599,3 +1605,43 @@ __rvm_expand_ruby_string()

return $?
}

__rvm_export()
{

# extract the variable name from the first arg.
local name=${1%%=*}

# store the current value, to be restored later.
builtin export rvm_old_$name=${!name}

# pass-through the return value of the builtin.
export "$@"
return $?
}

__rvm_unset_exports()
{
for pair in $(env); do

# ignore everything except for wrapped variables.
if [[ $pair =~ "rvm_old_" ]]; then

local wrap_name=${pair%%=*}
local name=${wrap_name#rvm_old_}
local value=${!wrap_name}

# if the old value is non-empty, restore it.
if [[ -n $value ]]; then
export $name=${value}

# otherwise, just remove it.
else
unset $name
fi

# unset the wrapped variable.
unset $wrap_name
fi
done
}

0 comments on commit e01bb51

Please sign in to comment.