2008-03-29 22:17:28 -04:00
|
|
|
module Rails
|
|
|
|
class GemDependency
|
|
|
|
attr_accessor :name, :requirement, :version, :lib, :source
|
|
|
|
|
|
|
|
def self.unpacked_path
|
|
|
|
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(name, options = {})
|
2008-04-09 12:24:32 -04:00
|
|
|
require 'rubygems' unless Object.const_defined?(:Gem)
|
2008-03-29 22:17:28 -04:00
|
|
|
@name = name.to_s
|
|
|
|
if options[:version]
|
|
|
|
@requirement = Gem::Requirement.create(options[:version])
|
2008-04-01 12:54:30 -04:00
|
|
|
@version = @requirement.instance_variable_get("@requirements").first.last
|
2008-03-29 22:17:28 -04:00
|
|
|
end
|
|
|
|
@lib = options[:lib]
|
|
|
|
@source = options[:source]
|
2008-04-08 00:21:32 -04:00
|
|
|
@loaded = @frozen = @load_paths_added = false
|
2008-03-29 22:17:28 -04:00
|
|
|
@unpack_directory = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_load_paths
|
|
|
|
return if @loaded || @load_paths_added
|
|
|
|
unpacked_paths = Dir[File.join(self.class.unpacked_path, "#{@name}-#{@version || "*"}")]
|
|
|
|
if unpacked_paths.empty?
|
|
|
|
args = [@name]
|
|
|
|
args << @requirement.to_s if @requirement
|
|
|
|
gem *args
|
|
|
|
else
|
|
|
|
$LOAD_PATH << File.join(unpacked_paths.first, 'lib')
|
2008-04-08 00:21:32 -04:00
|
|
|
@frozen = true
|
2008-03-29 22:17:28 -04:00
|
|
|
end
|
|
|
|
@load_paths_added = true
|
|
|
|
rescue Gem::LoadError
|
|
|
|
puts $!.to_s
|
|
|
|
end
|
2008-04-02 13:48:30 -04:00
|
|
|
|
|
|
|
def gem_dir(base_directory)
|
|
|
|
File.join(base_directory, specification.full_name)
|
|
|
|
end
|
2008-03-29 22:17:28 -04:00
|
|
|
|
|
|
|
def load
|
2008-03-29 22:35:08 -04:00
|
|
|
return if @loaded || @load_paths_added == false
|
2008-03-29 22:17:28 -04:00
|
|
|
require(@lib || @name)
|
|
|
|
@loaded = true
|
|
|
|
rescue LoadError
|
|
|
|
puts $!.to_s
|
|
|
|
$!.backtrace.each { |b| puts b }
|
|
|
|
end
|
|
|
|
|
2008-04-08 00:21:32 -04:00
|
|
|
def frozen?
|
|
|
|
@frozen
|
|
|
|
end
|
|
|
|
|
2008-03-29 22:17:28 -04:00
|
|
|
def loaded?
|
|
|
|
@loaded
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_paths_added?
|
|
|
|
@load_paths_added
|
|
|
|
end
|
|
|
|
|
|
|
|
def install
|
|
|
|
Gem::GemRunner.new.run(install_command)
|
|
|
|
end
|
2008-04-02 13:48:30 -04:00
|
|
|
|
|
|
|
def specification
|
|
|
|
@spec ||= Gem.source_index.search(Gem::Dependency.new(@name, @requirement)).sort_by { |s| s.version }.last
|
|
|
|
end
|
2008-03-29 22:17:28 -04:00
|
|
|
|
|
|
|
def unpack_to(directory)
|
|
|
|
FileUtils.mkdir_p directory
|
|
|
|
Dir.chdir directory do
|
|
|
|
Gem::GemRunner.new.run(unpack_command)
|
|
|
|
end
|
2008-04-02 13:48:30 -04:00
|
|
|
|
|
|
|
# copy the gem's specification into GEMDIR/.specification so that
|
|
|
|
# we can access information about the gem on deployment systems
|
|
|
|
# without having the gem installed
|
|
|
|
spec_filename = File.join(gem_dir(directory), '.specification')
|
|
|
|
File.open(spec_filename, 'w') do |file|
|
|
|
|
file.puts specification.to_yaml
|
|
|
|
end
|
2008-03-29 22:17:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def install_command
|
|
|
|
cmd = %w(install) << @name
|
|
|
|
cmd << "--version" << "#{@requirement.to_s}" if @requirement
|
|
|
|
cmd << "--source" << @source if @source
|
|
|
|
cmd
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpack_command
|
|
|
|
cmd = %w(unpack) << @name
|
|
|
|
cmd << "--version" << "#{@requirement.to_s}" if @requirement
|
|
|
|
cmd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|