Class: Gemtronics::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gemtronics/manager.rb

Constant Summary collapse

GLOBAL_DEFAULT_OPTIONS =

A Hash of the default options that are applied to all the gems. These options can be overidden at both the group and the individual gem level.

{:load => true, :version => '>=0.0.0', :ri => false, :rdoc => false}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

:nodoc:



14
15
16
# File 'lib/gemtronics/manager.rb', line 14

def initialize # :nodoc:
  reset!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Allows you to chain method calls together. An important note is that the *args are only passed into the first method in the chain.

Example:

Gemtronics.find_and_require_gem('gem1', :group => :production)

This will call the find method on Gemtronics::Manager and pass it the arguments {:group => :production}. It will then call the require_gem method on the Gemtronics::Definition class returned by the find method.

Raises:

  • (NoMethodError)


200
201
202
203
204
205
206
207
208
# File 'lib/gemtronics/manager.rb', line 200

def method_missing(sym, *args)
  chain = sym.to_s.split('_and_')
  raise NoMethodError.new(sym.to_s) if chain.nil? || chain.empty? || chain.size == 1
  res = self.send(chain[0], *args)
  chain[1..chain.size].each do |meth|
    res = res.send(meth)
  end
  return res
end

Instance Attribute Details

#groupsObject

A Hash of all the groups that have been defined.



11
12
13
# File 'lib/gemtronics/manager.rb', line 11

def groups
  @groups
end

#installed_gemsObject

:nodoc:



12
13
14
# File 'lib/gemtronics/manager.rb', line 12

def installed_gems
  @installed_gems
end

Instance Method Details

#alias_group(name, src) ⇒ Object

Aliases one gem group to another group.

Example:

group(:development) do |g|
  g.add('gem1')
  g.add('gem2')
end

alias_group :test, :development

The result would be the same as if you had done this:

group(:development) do |g|
  g.add('gem1')
  g.add('gem2')
end
group(:test, :dependencies => :development) do |g|
end


57
58
59
# File 'lib/gemtronics/manager.rb', line 57

def alias_group(name, src)
  group(name, :dependencies => src)
end

#find(name, options = {}) ⇒ Object

Finds and returns a Gemtronics::Definition for the specified gem, if one exists.

Search Order: If the option :group is passed in it will search only within that group.

If RAILS_ENV is defined it will search in the group that matches the current Rails environment

Finally it will search all gems and return the first one it finds. There is no guarantee as to the order it searches through the groups.

Gemtronics.find('gem1')
Gemtronics.find('gem1', :group => :production)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/gemtronics/manager.rb', line 171

def find(name, options = {})
  group = options[:group]
  if group
    group = self.groups[group.to_sym]
    return nil if group.nil?
    gemdef = group.search(name, options)
    return gemdef unless gemdef.nil?        
  elsif defined?(RAILS_ENV)
    return find(name, {:group => RAILS_ENV.to_sym}.merge(options))
  else
    self.groups.each do |key, group|
      gemdef = group.search(name, options)
      return gemdef unless gemdef.nil?
    end        
  end
  raise "#{name} has not been defined!"
end

#find_out_of_date_gems(indicate = false) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/gemtronics/manager.rb', line 210

def find_out_of_date_gems(indicate = false)
  tested = []
  outdated = []
  self.groups.each do |name, group|
    group.gems.each do |g|
      unless tested.include?("#{g.name}-#{g.version}")
        if indicate
          $stdout.write('.')
          $stdout.flush
        end
        v = g.has_update?
        # puts "!!!#{g.name} #{v}"
        if v
          outdated << v
        end
        tested << "#{g.name}-#{g.version}"
      end
    end
  end
  return outdated.sort
end

#for_rails(config = nil, options = {}) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/gemtronics/manager.rb', line 232

def for_rails(config = nil, options = {})
  options = {:gemtronics_path => File.join(RAILS_ROOT, 'config', 'gemtronics.rb'),
             :group => RAILS_ENV}.merge(options)
  [options.delete(:gemtronics_path)].flatten.each do |path|
    self.load(path)
  end
  group_list = [options.delete(:group)].flatten
  
  if config.nil?
    for_rails_without_config(group_list, options)
  else
    for_rails_with_config(group_list, config, options)
  end
  
end

#group(name, options = {}) ⇒ Object

Creates, or reopens a new group of gems. It takes the name of the group, and any options you would like all of the gems in that group to inherit.

Example:

group(:development, :source => 'http://gems.example.com') do |g|
  g.add('gem1')
  g.add('gem2')
end

For more information see Gemtronics::Grouper



29
30
31
32
33
34
35
36
37
# File 'lib/gemtronics/manager.rb', line 29

def group(name, options = {})
  name = name.to_sym
  g = (self.groups[name] ||= Gemtronics::Grouper.new(name, options || {}))
  if block_given?
    yield g
  else
    return g
  end
end

#install_all_gemsObject

:nodoc:



144
145
146
147
148
# File 'lib/gemtronics/manager.rb', line 144

def install_all_gems # :nodoc:
  self.groups.each do |name, value|
    self.install_gems(name)
  end
end

#install_gems(group = :default) ⇒ Object

This will install only the gems in the group that are not currently installed in the system.

Example:

group(:default) do |g|
  g.add('gem1')
  g.add('gem2', :version => '1.2.3')
  g.add('gem3', :load => false)
  g.add('gem4', :require => 'gem-four')
  g.add('gem5', :require => ['gem-five', 'gemfive'])
  g.add('gem6', :load => false, :source => 'http://gems.example.com')
end

Assuming gems gem3, gem4, gem5 are previously installed:

Gemtronics.install_gems(:default)
# gem install gem1 --source=http://gems.rubyforge.org
# gem install gem2 --source=http://gems.rubyforge.org --version=1.2.3
# gem install gem6 --source=http://gems.example.com


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gemtronics/manager.rb', line 126

def install_gems(group = :default)
  if group == 'everything!'
    install_all_gems
    return
  end
  group = self.groups[group.to_sym]
  return if group.nil?
  
  group.gems.each do |g|
    unless gem_installed?(g)
      cmd = g.install_command
      puts cmd
      system cmd
      self.installed_gems << g.to_s
    end
  end
end

#load(file = File.join(FileUtils.pwd, 'config', 'gemtronics.rb')) ⇒ Object

Reads in a file and sets up the gems appropriately. The default path is '<pwd>/config/gemtronics.rb'

See README for an example file.



65
66
67
# File 'lib/gemtronics/manager.rb', line 65

def load(file = File.join(FileUtils.pwd, 'config', 'gemtronics.rb'))
  eval(File.read(file), binding)
end

#require_gems(group = :default, options = {}) ⇒ Object

Requires all the gems and the specified files for the group. It will not require any gems that have the :load options set to false

Example:

group(:default) do |g|
  g.add('gem1')
  g.add('gem2', :version => '1.2.3')
  g.add('gem3', :load => false)
  g.add('gem4', :require => 'gem-four')
  g.add('gem5', :require => ['gem-five', 'gemfive'])
  g.add('gem6', :load => false)
end

Gemtronics.require_gems(:default)

In this example it would do the following:

gem('gem1', '>=0.0.0')
require 'gem1'
gem('gem2', '1.2.3')
require 'gem2'
gem('gem4', '>=0.0.0')
require 'gem-four'
gem('gem5', '>=0.0.0')
require 'gem-five'
require 'gemfive'


96
97
98
99
100
101
102
103
104
105
# File 'lib/gemtronics/manager.rb', line 96

def require_gems(group = :default, options = {})
  group = self.groups[group.to_sym]
  return if group.nil?
  options = {:verbose => false}.merge(options)
  group.gems.each do |g|
    if g.load? == true
      g.require_gem(options)
    end
  end
end

#reset!Object

:nodoc:



150
151
152
153
# File 'lib/gemtronics/manager.rb', line 150

def reset! # :nodoc:
  self.groups = {}
  self.installed_gems = []
end