2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# Gem::StubSpecification reads the stub: line from the gemspec. This prevents
|
|
|
|
# us having to eval the entire gemspec in order to find out certain
|
|
|
|
# information.
|
|
|
|
|
|
|
|
class Gem::StubSpecification < Gem::BasicSpecification
|
|
|
|
# :nodoc:
|
|
|
|
PREFIX = "# stub: "
|
|
|
|
|
|
|
|
OPEN_MODE = # :nodoc:
|
|
|
|
if Object.const_defined? :Encoding then
|
|
|
|
'r:UTF-8:-'
|
|
|
|
else
|
|
|
|
'r'
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
class StubLine # :nodoc: all
|
|
|
|
attr_reader :parts
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def initialize(data)
|
|
|
|
@parts = data[PREFIX.length..-1].split(" ")
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def name
|
|
|
|
@parts[0]
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def version
|
|
|
|
Gem::Version.new @parts[1]
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def platform
|
|
|
|
Gem::Platform.new @parts[2]
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def require_paths
|
|
|
|
@parts[3..-1].join(" ").split("\0")
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
2013-08-26 16:24:51 -04:00
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def initialize(filename)
|
|
|
|
self.loaded_from = filename
|
|
|
|
@data = nil
|
|
|
|
@spec = nil
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# True when this gem has been activated
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def activated?
|
|
|
|
loaded = Gem.loaded_specs[name]
|
|
|
|
loaded && loaded.version == version
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# If the gemspec contains a stubline, returns a StubLine instance. Otherwise
|
|
|
|
# returns the full Gem::Specification.
|
|
|
|
|
|
|
|
def data
|
|
|
|
unless @data
|
|
|
|
open loaded_from, OPEN_MODE do |file|
|
|
|
|
begin
|
|
|
|
file.readline # discard encoding line
|
|
|
|
stubline = file.readline.chomp
|
|
|
|
@data = StubLine.new(stubline) if stubline.start_with?(PREFIX)
|
|
|
|
rescue EOFError
|
|
|
|
end
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
@data ||= to_spec
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
private :data
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# Name of the gem
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def name
|
|
|
|
@name ||= data.name
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# Platform of the gem
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def platform
|
|
|
|
@platform ||= data.platform
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# Require paths of the gem
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def require_paths
|
|
|
|
@require_paths ||= data.require_paths
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# The full Gem::Specification for this gem, loaded from evalling its gemspec
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def to_spec
|
|
|
|
@spec ||= Gem::Specification.load(loaded_from)
|
|
|
|
end
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
##
|
|
|
|
# Is this StubSpecification valid? i.e. have we found a stub line, OR does
|
|
|
|
# the filename contain a valid gemspec?
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
def valid?
|
|
|
|
data
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
2013-08-26 16:24:51 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Version of the gem
|
|
|
|
|
|
|
|
def version
|
|
|
|
@version ||= data.version
|
|
|
|
end
|
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
2013-08-26 16:24:51 -04:00
|
|
|
|