Module: Stable::Services::Ruby

Defined in:
lib/stable/services/ruby.rb

Overview

Ruby version management utilities

Class Method Summary collapse

Class Method Details

.clean_rvm_exec(ruby, gemset, cmd) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/stable/services/ruby.rb', line 102

def self.clean_rvm_exec(ruby, gemset, cmd)
  "    bash -lc '\n      source \#{rvm_script};\n      rvm \#{ruby}@\#{gemset} --create do \#{cmd}\n    '\n  CMD\nend\n"

.detect_ruby_version(path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/stable/services/ruby.rb', line 72

def self.detect_ruby_version(path)
  rv = File.join(path, '.ruby-version')
  return File.read(rv).strip if File.exist?(rv)

  gemfile = File.join(path, 'Gemfile')
  if File.exist?(gemfile)
    ruby_line = File.read(gemfile)[/^ruby ['"](.+?)['"]/, 1]
    return ruby_line if ruby_line
  end

  nil
end

.ensure_rbenv_ruby!(version) ⇒ Object



57
58
59
# File 'lib/stable/services/ruby.rb', line 57

def self.ensure_rbenv_ruby!(version)
  system("rbenv versions | grep -q #{version} || rbenv install #{version}")
end

.ensure_ruby_installed!(version) ⇒ Object



46
47
48
49
50
51
# File 'lib/stable/services/ruby.rb', line 46

def self.ensure_ruby_installed!(version)
  return if system("rvm list strings | grep ruby-#{version} > /dev/null")

  puts "Installing Ruby #{version}..."
  system("rvm install #{version}") || abort("Failed to install Ruby #{version}")
end

.ensure_rvm!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stable/services/ruby.rb', line 27

def self.ensure_rvm!
  return if rvm_available?

  puts 'RVM not found. Installing RVM...'

  install_cmd = "    curl -sSL https://get.rvm.io | bash -s stable\n  CMD\n\n  abort 'RVM installation failed' unless system(install_cmd)\n\n  rvm_script = File.expand_path('~/.rvm/scripts/rvm')\n  abort 'RVM installed but could not be loaded' unless File.exist?(rvm_script)\n\n  ENV['PATH'] = \"\#{File.expand_path('~/.rvm/bin')}:\#{ENV.fetch('PATH', nil)}\"\n\n  system(%(bash -lc \"source \#{rvm_script} && rvm --version\")) || abort('RVM installed but not functional')\nend\n"

.ensure_rvm_ruby!(version) ⇒ Object



53
54
55
# File 'lib/stable/services/ruby.rb', line 53

def self.ensure_rvm_ruby!(version)
  system("bash -lc 'rvm list strings | grep -q #{version} || rvm install #{version}'")
end

.ensure_version(version) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/stable/services/ruby.rb', line 7

def self.ensure_version(version)
  return if version.nil? || version.to_s.strip.empty?

  # Check if version exists via rvm
  installed = system("bash -lc 'rvm list strings | grep -q #{version}'")
  return if installed

  puts "Installing Ruby #{version}..."
  success = system("bash -lc 'rvm install #{version}'")
  raise "Failed to install Ruby #{version}" unless success
end

.gemset_for(app) ⇒ Object



85
86
87
88
89
90
# File 'lib/stable/services/ruby.rb', line 85

def self.gemset_for(app)
  gemset_file = File.join(app[:path], '.ruby-gemset')
  return File.read(gemset_file).strip if File.exist?(gemset_file)

  nil
end

.rbenv_available?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/stable/services/ruby.rb', line 23

def self.rbenv_available?
  system('command -v rbenv > /dev/null')
end

.rvm_available?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/stable/services/ruby.rb', line 19

def self.rvm_available?
  system("bash -lc 'command -v rvm > /dev/null'")
end

.rvm_exec(app, ruby) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/stable/services/ruby.rb', line 92

def self.rvm_exec(app, ruby)
  gemset = gemset_for(app)

  if gemset
    "rvm #{ruby}@#{gemset} do"
  else
    "rvm #{ruby} do"
  end
end

.rvm_prefix(ruby, gemset = nil) ⇒ Object

Return a command prefix that sources RVM and executes the given ruby@gemset Example: “source /Users/me/.rvm/scripts/rvm && rvm 3.4.4@myapp do”



67
68
69
70
# File 'lib/stable/services/ruby.rb', line 67

def self.rvm_prefix(ruby, gemset = nil)
  gemset_part = gemset ? "@#{gemset}" : ''
  "source #{rvm_script} && rvm #{ruby}#{gemset_part} do"
end

.rvm_scriptObject



61
62
63
# File 'lib/stable/services/ruby.rb', line 61

def self.rvm_script
  File.expand_path('~/.rvm/scripts/rvm')
end