-
Notifications
You must be signed in to change notification settings - Fork 9
/
ruby_selector.rb
67 lines (57 loc) · 2 KB
/
ruby_selector.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'pathname'
require 'which_works'
module SchemaDev
module RubySelector
def self.command(ruby)
@@selector ||= [Chruby, Rvm, Rbenv].find(&:installed?)&.new || abort("No ruby version manager found")
@@selector.command ruby
end
def self._reset # for rspec, to avoid stickiness
@@selector = nil
end
class ManagerBase
def self.installed?
Which.which const_get :CORE_COMMAND
end
end
class Chruby < ManagerBase
CORE_COMMAND = "chruby-exec"
def initialize
@rubies = Pathname.new(ENV['HOME'])
.join(".rubies")
.entries()
.map { |e| e.basename.to_s }
end
def command(ruby)
bash = Which.which 'bash' || abort("no bash shell found")
ruby = @rubies.select { |e| e =~ /^(ruby-)?#{ruby}(-p.*)?$/ }
.last || ruby
"SHELL=#{bash} #{CORE_COMMAND} #{ruby} --"
end
end
class Rvm < ManagerBase
CORE_COMMAND = "rvm"
def command(ruby)
"#{CORE_COMMAND} #{ruby} do"
end
end
class Rbenv < ManagerBase
CORE_COMMAND = "rbenv"
def initialize
# because we're running within a ruby program that was launched by
# rbenv, we already have various environment variables set up. need
# strip those out so that the forked shell can run a diifferent ruby
# version than the one we're in now.
ENV['PATH'] = ENV['PATH'].split(':').reject{|dir| dir =~ %r{/\.?rbenv/(?!shims)}}.join(':')
ENV['GEM_PATH'] = ENV['GEM_PATH'].split(':').reject{|dir| dir =~ %r{/\.?rbenv}}.join(':') unless ENV['GEM_PATH'].nil?
ENV['RBENV_DIR'] = nil
ENV['RBENV_HOOK_PATH'] = nil
@versions ||= `rbenv versions --bare`.split
end
def command(ruby)
version = @versions.select{|v| v.start_with? ruby}.last || abort("no ruby version '#{ruby}' installed in rbenv")
"RBENV_VERSION=#{version}"
end
end
end
end