Class: Proscenium::RubyGems

Inherits:
Object show all
Defined in:
lib/proscenium/ruby_gems.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version = nil) ⇒ RubyGems

Returns a new instance of RubyGems.



12
13
14
15
# File 'lib/proscenium/ruby_gems.rb', line 12

def initialize(name, version = nil)
  @name = name
  @version = version
end

Class Method Details

.path_for(name, version = nil) ⇒ Object



8
9
10
# File 'lib/proscenium/ruby_gems.rb', line 8

def self.path_for(name, version = nil)
  Pathname new(name, version).path
end

Instance Method Details

#find_in_cache(filename) ⇒ Object

Find cached filename in Gem.path. Returns nil if the file cannot be found.



32
33
34
35
36
37
38
39
# File 'lib/proscenium/ruby_gems.rb', line 32

def find_in_cache(filename)
  Gem.path.each do |path|
    this_path = File.join(path, 'cache', filename)
    return this_path if File.exist? this_path
  end

  nil
end

#gem_path(dependency) ⇒ Object

Return the full path to the cached gem file matching the given name and version requirement. Returns ‘nil’ if no match.

Example:

get_path 'rake', '> 0.4' # "/usr/lib/ruby/gems/1.8/cache/rake-0.4.2.gem"
get_path 'rake', '< 0.1' # nil
get_path 'rak'           # nil (exact name required)


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

def gem_path(dependency)
  return dependency.name if /\.gem$/i.match?(dependency.name)

  specs = dependency.matching_specs
  selected = specs.max_by(&:version)

  return Gem::RemoteFetcher.fetcher.download_to_cache(dependency) unless selected
  return unless /^#{selected.name}$/i.match?(dependency.name)

  # We expect to find (basename).gem in the 'cache' directory. Furthermore,
  # the name match must be exact (ignoring case).
  path = find_in_cache File.basename selected.cache_file

  return Gem::RemoteFetcher.fetcher.download_to_cache(dependency) unless path

  path
end

#pathObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/proscenium/ruby_gems.rb', line 17

def path
  dependency = Gem::Dependency.new @name, @version
  path = gem_path dependency

  raise "Gem '#{@name}' not installed nor fetchable." unless path

  basename = File.basename path, '.gem'
  target_dir = File.expand_path basename, Rails.root.join('tmp', 'unpacked_gems')

  Gem::Package.new(path).extract_files target_dir

  target_dir
end