Class: Ramaze::GemSetup

Inherits:
Object show all
Defined in:
lib/ramaze/setup.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ GemSetup

Returns a new instance of GemSetup.



32
33
34
35
36
37
38
# File 'lib/ramaze/setup.rb', line 32

def initialize(options = {}, &block)
  @gems = []
  @options = options.dup
  @verbose = @options.delete(:verbose)

  run(&block)
end

Instance Method Details

#gem(name, version = nil, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ramaze/setup.rb', line 46

def gem(name, version = nil, options = {})
  if version.respond_to?(:merge!)
    options = version
  else
    options[:version] = version
  end

  @gems << [name, options]
end

#install_gem(name, options) ⇒ Object

tell rubygems to install a gem



79
80
81
82
83
84
85
86
# File 'lib/ramaze/setup.rb', line 79

def install_gem(name, options)
  installer = Gem::DependencyInstaller.new(options)

  temp_argv(options[:extconf]) do
    log "Installing #{name}"
    installer.install(name, options[:version])
  end
end

#run(&block) ⇒ Object



40
41
42
43
44
# File 'lib/ramaze/setup.rb', line 40

def run(&block)
  return unless block_given?
  instance_eval(&block)
  setup
end

#setupObject

all gems defined, let’s try to load/install them



57
58
59
60
61
62
63
64
# File 'lib/ramaze/setup.rb', line 57

def setup
  require 'rubygems'
  require 'rubygems/dependency_installer'

  @gems.each do |name, options|
    setup_gem(name, options)
  end
end

#setup_gem(name, options, try_install = true) ⇒ Object

first try to activate, install and try to activate again if activation fails the first time



68
69
70
71
72
73
74
75
76
# File 'lib/ramaze/setup.rb', line 68

def setup_gem(name, options, try_install = true)
  log "activating #{name}"
  Gem.activate(name, *[options[:version]].compact)
  require(options[:lib] || name)
rescue LoadError => exception
  puts exception
  install_gem(name, options) if try_install
  setup_gem(name, options, try_install = false)
end

#temp_argv(extconf) ⇒ Object

prepare ARGV for rubygems installer



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ramaze/setup.rb', line 89

def temp_argv(extconf)
  if extconf ||= @options[:extconf]
    old_argv = ARGV.clone
    ARGV.replace(extconf.split(' '))
  end

  yield

ensure
  ARGV.replace(old_argv) if extconf
end