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.



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

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

  run(&block)
end

Instance Method Details

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



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

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



86
87
88
89
90
91
92
93
# File 'lib/ramaze/setup.rb', line 86

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



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

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

#setupObject

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



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

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

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

#setup_gem(name, options) ⇒ Object

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ramaze/setup.rb', line 69

def setup_gem(name, options)
  version = [options[:version]].compact
  lib_name = options[:lib] || name

  log "activating #{name}"

  Gem.activate(name, *version)
  require(lib_name)

rescue LoadError

  install_gem(name, options)
  Gem.activate(name, *version)
  require(lib_name)
end

#temp_argv(extconf) ⇒ Object

prepare ARGV for rubygems installer



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ramaze/setup.rb', line 96

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