Module: Tap::Env::Gems

Defined in:
lib/tap/env/gems.rb

Overview

Methods for working with RubyGems.

Class Method Summary collapse

Class Method Details

.gemspec(gem_name) ⇒ Object

Returns the gemspec for the specified gem. A gem version can be specified in the name, like ‘gem >= 1.2’. The gem is not activated by this method.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tap/env/gems.rb', line 13

def gemspec(gem_name)
  return gem_name if gem_name.kind_of?(Gem::Specification)
  
  dependency = if gem_name.kind_of?(Gem::Dependency)
    gem_name
  else
    # figure the version of the gem, by default >= 0.0.0
    gem_name.to_s =~ /^([^~<=>]*)(.*)$/
    name, version = $1.strip, $2
    return nil if name.empty?
    version = Gem::Requirement.default if version.empty?

    # note the last gem matching the dependency requirements
    # is the latest matching gem
    Gem::Dependency.new(name, version)
  end
  
  Gem.source_index.search(dependency).last
end

.select_gems(latest = true) ⇒ Object

Selects gem specs for which the block returns true. If latest is specified, only the latest version of each gem will be passed to the block.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tap/env/gems.rb', line 36

def select_gems(latest=true)
  specs = latest ?
    Gem.source_index.latest_specs :
    Gem.source_index.gems.collect {|(name, spec)| spec }
  
  # this song and dance is to ensure that specs are sorted
  # by name (ascending) then version (descending) so that
  # the latest version of a spec appears first
  specs_by_name = {}
  specs.each do |spec|
    next unless !block_given? || yield(spec) 
    (specs_by_name[spec.name] ||= []) << spec
  end
  
  specs = []
  specs_by_name.keys.sort.each do |name|
    specs_by_name[name].sort_by do |spec| 
      spec.version
    end.reverse_each do |spec|
      specs << spec
    end
  end
  
  specs
end