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)


229
230
231
232
233
234
235
236
237
# File 'lib/gemtronics/manager.rb', line 229

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


86
87
88
# File 'lib/gemtronics/manager.rb', line 86

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)


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/gemtronics/manager.rb', line 200

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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/gemtronics/manager.rb', line 239

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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/gemtronics/manager.rb', line 261

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:



173
174
175
176
177
# File 'lib/gemtronics/manager.rb', line 173

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


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/gemtronics/manager.rb', line 155

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.



94
95
96
# File 'lib/gemtronics/manager.rb', line 94

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'


125
126
127
128
129
130
131
132
133
134
# File 'lib/gemtronics/manager.rb', line 125

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:



179
180
181
182
# File 'lib/gemtronics/manager.rb', line 179

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

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

Adds support for WarpDrives within Gemtronics:

Example:

warp_drive('authenticator', :version => '1.0.1')

This will add the WarpDrive gem to the list, and then load the WarpDrive’s gemtronics file.

github.com/markbates/warp_drive



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gemtronics/manager.rb', line 48

def warp_drive(name, options = {})
  options = {:load => false}.merge(options)
  group(:warp_drives) {|g| g.add(name, options)}
  begin
    g = find(name, :group => :warp_drives)
    g.require_gem
    load(WarpDrive::Path.config.gemtronics.rb)
  rescue Exception => e
    if e.message.match(/Could not find RubyGem #{name}/)
      g = find(name, :group => :warp_drives)
      cmd = g.install_command
      puts cmd
      system cmd
      Gem.clear_paths
      g.require_gem
      load(WarpDrive::Path.config.gemtronics.rb)
    end
  end
end