module WWTD::Ruby

Public Class Methods

available?(version) click to toggle source
# File lib/wwtd/ruby.rb, line 4
def available?(version)
  !version || switch_statement(version)
end
switch_statement(version, options={}) click to toggle source
  • rvm: “rvm xxx do”

  • chruby: “chruby-exec xxx –”

  • others: env hash

  • unknown: nil

# File lib/wwtd/ruby.rb, line 12
def switch_statement(version, options={})
  return unless version
  version = normalize_ruby_version(version)
  if rvm_executable
    command = "rvm-exec #{version} "
    command if cache_command("#{command} ruby -v")
  elsif chruby_executable
    command = "chruby-exec #{version} -- "
    command if cache_command("#{command} ruby -v")
  elsif options[:rerun]
    if rbenv_executable
      # cannot call different ruby from inside ruby, but ok for copy-paste
      "RBENV_VERSION=#{version} "
    else
      # don't print giant path hack :/
      "USE-RUBY-#{version}"
    end
  else
    if ruby_root = ENV["RUBY_ROOT"] # chruby or RUBY_ROOT set
      switch_via_env(File.dirname(ruby_root), version)
    elsif rbenv_executable
      rubies_root = cache_command("rbenv root") + "/versions"
      switch_via_env(rubies_root, version)
    end
  end
end

Private Class Methods

cache(key) { || ... } click to toggle source
# File lib/wwtd/ruby.rb, line 79
def cache(key)
  @cache ||= {}
  if @cache.key?(key)
    @cache[key]
  else
    @cache[key] = yield
  end
end
cache_command(command) click to toggle source
# File lib/wwtd/ruby.rb, line 65
def cache_command(command)
  cache(command) do
    if result = capture(command)
      result.strip
    end
  end
end
capture(command) click to toggle source
# File lib/wwtd/ruby.rb, line 99
def capture(command)
  result = `#{command}`
  $?.success? ? result : nil
end
chruby_executable() click to toggle source
# File lib/wwtd/ruby.rb, line 61
def chruby_executable
  !ENV["PATH"].include?("/rbenv/") && cache_command("which chruby-exec")
end
extract_jruby_rbenv_options!(version) click to toggle source

set ruby-opts for jruby flavors

# File lib/wwtd/ruby.rb, line 89
def extract_jruby_rbenv_options!(version)
  if version.sub!("-d19", "")
    { "JRUBY_OPTS" => "--1.9" }
  elsif version.sub!("-d18", "")
    { "JRUBY_OPTS" => "--1.8" }
  else
    {}
  end
end
normalize_ruby_version(rvm) click to toggle source

Taken from github.com/travis-ci/travis-build/blob/master/lib/travis/build/script/rvm.rb

# File lib/wwtd/ruby.rb, line 105
def normalize_ruby_version(rvm)
  rvm.to_s.
    gsub(/-(\d{2})mode$/, '-d\1').
    gsub(/^rbx$/, 'rbx-weekly-d18').
    gsub(/^rbx-d(\d{2})$/, 'rbx-weekly-d\1')
end
rbenv_executable() click to toggle source
# File lib/wwtd/ruby.rb, line 57
def rbenv_executable
  cache_command("which rbenv")
end
ruby_root(root, version) click to toggle source
# File lib/wwtd/ruby.rb, line 73
def ruby_root(root, version)
  Dir.glob("#{root}/*").detect do |p|
    File.basename(p).sub(/^ruby-/,"").start_with?(version)
  end
end
rvm_executable() click to toggle source
# File lib/wwtd/ruby.rb, line 53
def rvm_executable
  cache_command("which rvm")
end
switch_via_env(rubies_root, version) click to toggle source
# File lib/wwtd/ruby.rb, line 41
def switch_via_env(rubies_root, version)
  base = extract_jruby_rbenv_options!(version)
  if ruby_root = ruby_root(rubies_root, version)
    gem_home = Dir["#{ruby_root}/lib/ruby/gems/*"].first
    base.merge(
      "PATH" => "#{ruby_root}/bin:#{ENV["PATH"]}",
      "GEM_HOME" => gem_home,
      "GEM_PATH" => gem_home
    )
  end
end