Class: MetaCon::Loaders::RVM

Inherits:
Object
  • Object
show all
Includes:
CLIHelpers, Shorthand
Defined in:
lib/metacon/loaders/rvm.rb

Constant Summary collapse

RVMS =
"source $HOME/.rvm/scripts/rvm && "

Constants included from CLIHelpers

CLIHelpers::ANSI, CLIHelpers::CONTEXT_OS, CLIHelpers::ESCS

Class Method Summary collapse

Methods included from CLIHelpers

#best_profile_file, #cfail, #chd, #check_tool, #color_puts, #command_exists?, #cstr2, #cwarn, #darwin?, #fj, #included, #linux?, #mac?, #result, #shellescape, #status

Methods included from Shorthand

#common_prefix, #included, #relative_path, #shcmd

Class Method Details

.check_gemset_installed(ruby, gemset, opts) ⇒ Object



49
50
51
52
# File 'lib/metacon/loaders/rvm.rb', line 49

def self.check_gemset_installed(ruby, gemset, opts)
  o,e,s = shc("rvm use '#{ruby}'@'#{gemset}'", false)
  return (s == 0 && e.strip.length == 0)
end

.check_installed(ruby, opts) ⇒ Object



44
45
46
47
# File 'lib/metacon/loaders/rvm.rb', line 44

def self.check_installed(ruby, opts)
  o,e,s = shc("rvm use '#{ruby}@'", false)
  return (s==0 && o =~ /using/i)
end

.create_gemset(ruby, gemset, opts) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/metacon/loaders/rvm.rb', line 59

def self.create_gemset(ruby, gemset, opts)
  o,e,s = shc("rvm use '#{ruby}' && rvm gemset create '#{gemset}'", opts[:verbose])
  res = (o =~ /created/i) && (s == 0) && (e.strip.length == 0)
  return res
  #return false unless res
  # TODO: Make sure any "permanent" prereqs are loaded (possibly
  # metacon?)
end

.install(ruby, opts) ⇒ Object



54
55
56
57
# File 'lib/metacon/loaders/rvm.rb', line 54

def self.install(ruby, opts)
  o,e,s = shc("rvm install #{ruby}", opts[:verbose])
  return (s == 0 && check_installed(ruby,opts))
end

.load_dependency(dependency_parts, state, proj, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/metacon/loaders/rvm.rb', line 6

def self.load_dependency(dependency_parts, state, proj, opts)
  kind = dependency_parts.shift
  if kind == 'ruby'
    # TODO: check for and install rvm for when this is used outside of
    # the main installed command context.
    ruby = fix_ruby_version(dependency_parts)
    return switch_ruby(ruby, state, opts)
  elsif kind == 'gem'

  else
    raise "I don't handle #{kind} dependencies... am I missing something?"
  end
end

.shc(cmdstr, v) ⇒ Object



22
# File 'lib/metacon/loaders/rvm.rb', line 22

def self.shc(cmdstr,v); return shcmd("#{RVMS} #{cmdstr}", v) end

.switch(ruby, gemset, opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/metacon/loaders/rvm.rb', line 68

def self.switch(ruby, gemset, opts)
  # TODO: if the --shell flag is sent in, essentially do the following:
  #       * Use rvm info to figure out the correct root directory
  #       * Find .rvm/environments/... for the currently selected ruby+gemset
  #       * Compare actual $PATH to what it would need to change to so that we
  #         don't keep prepending to PATH and growing it needlessly on every
  #         switch.
  #       * Create replacement 'export path' stmt
  #       * Take all stmts and concatenate w/ ';' and shell escape where
  #         appropriate (esp. newlines at least)
  #       * Prepend full string w/ 'bash: ' so that it gets evalled in the
  #         current context.
  #       * Enjoy!
  o,e,s = shc("rvm '#{ruby}'@'#{gemset}' do rvm tools identifier", false)
  identstr = o.strip
  envsettings = "~/.rvm/environments/#{identstr}"
  cmds = process_env_commands(IO.readlines(File.expand_path(envsettings)))
  puts cmds.join("\n") if opts[:shell]
  return true
end

.switch_ruby(ruby, state, opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/metacon/loaders/rvm.rb', line 24

def self.switch_ruby(ruby, state, opts={})
  # TODO: shortcircuit all of this by trying to construct the environment path
  # and look there first - then do all of this if it doesn't exist.
  unless check_installed(ruby,opts)
    unless install(ruby,opts)
      cfail "Failed to install ruby '#{ruby}'"
      return false
    end
  end
  gemset = fix_gemset_name(state)
  unless check_gemset_installed(ruby, gemset, opts)
    unless create_gemset(ruby, gemset, opts)
      cfail "Failed to create a gemset '#{gemset}' for '#{ruby}'"
      return false
    end
  end
  return switch(ruby, gemset, opts)
end