Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoize ruby env on disk #373

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 21 additions & 6 deletions share/chruby/chruby.sh
Expand Up @@ -30,6 +30,22 @@ function chruby_reset()
hash -r
}

function chruby_generate_cached_env()
{
"$SHELL" -c "unset JAVACMD JRUBY_OPTS; RUBYGEMS_GEMDEPS='' '$RUBY_ROOT/bin/ruby' -" <<'SCRIPT'
env = {
:RUBY_ENGINE => Object.const_defined?(:RUBY_ENGINE) ? RUBY_ENGINE : 'ruby',
:RUBY_VERSION => RUBY_VERSION,
}
begin
require 'rubygems'
env[:GEM_ROOT] = Gem.default_dir
rescue LoadError
end
env.each { |k,v| puts "#{k}='#{v}'; export #{k}" }
SCRIPT
}

function chruby_use()
{
if [[ ! -x "$1/bin/ruby" ]]; then
Expand All @@ -42,13 +58,12 @@ function chruby_use()
export RUBY_ROOT="$1"
export RUBYOPT="$2"
export PATH="$RUBY_ROOT/bin:$PATH"
local ruby_cached_env="$RUBY_ROOT/.ruby_env"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't always have write access to the ruby's root.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this were made configurable by the user (opting out of it e.g. by not providing a RUBY_ENV_CACHE), could it be merged then?

if [[ ! -e "$ruby_cached_env" ]]; then
chruby_generate_cached_env > "$ruby_cached_env"
fi
. "$ruby_cached_env"

eval "$(RUBYGEMS_GEMDEPS="" "$RUBY_ROOT/bin/ruby" - <<EOF
puts "export RUBY_ENGINE=#{Object.const_defined?(:RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'};"
puts "export RUBY_VERSION=#{RUBY_VERSION};"
begin; require 'rubygems'; puts "export GEM_ROOT=#{Gem.default_dir.inspect};"; rescue LoadError; end
EOF
)"
export PATH="${GEM_ROOT:+$GEM_ROOT/bin:}$PATH"

if (( UID != 0 )); then
Expand Down