Methods
- ==
- add_frozen_gem_path
- add_load_paths
- build
- built?
- dependencies
- framework_gem?
- from_directory_name
- frozen?
- install
- installed?
- load
- load_paths_added?
- loaded?
- new
- refresh
- requirement
- requirement
- specification
- specification=
- unpack
- unpacked_path
- vendor_gem?
- vendor_rails?
- write_specification
Attributes
| [RW] | dep | |
| [RW] | lib | |
| [RW] | source |
Public Class methods
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 19
19: def self.add_frozen_gem_path
20: @@paths_loaded ||= begin
21: source_index = Rails::VendorGemSourceIndex.new(Gem.source_index)
22: Gem.clear_paths
23: Gem.source_index = source_index
24: # loaded before us - we can't change them, so mark them
25: Gem.loaded_specs.each do |name, spec|
26: @@framework_gems[name] = spec
27: end
28: true
29: end
30: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 32
32: def self.from_directory_name(directory_name, load_spec=true)
33: directory_name_parts = File.basename(directory_name).split('-')
34:
35: version = directory_name_parts.find { |s| s.match(/^\d(\.\d|\.\w+)*$/) }
36: name = directory_name_parts[0..directory_name_parts.index(version)-1].join('-') if version
37:
38: result = self.new(name, :version => version)
39: spec_filename = File.join(directory_name, '.specification')
40: if load_spec
41: raise "Missing specification file in #{File.dirname(spec_filename)}. Perhaps you need to do a 'rake gems:refresh_specs'?" unless File.exists?(spec_filename)
42: spec = YAML::load_file(spec_filename)
43: result.specification = spec
44: end
45: result
46: rescue ArgumentError => e
47: raise "Unable to determine gem name and version from '#{directory_name}'"
48: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 50
50: def initialize(name, options = {})
51: require 'rubygems' unless Object.const_defined?(:Gem)
52:
53: if options[:requirement]
54: req = options[:requirement]
55: elsif options[:version]
56: req = Gem::Requirement.create(options[:version])
57: else
58: req = Gem::Requirement.default
59: end
60:
61: @lib = options[:lib]
62: @source = options[:source]
63: @loaded = @frozen = @load_paths_added = false
64:
65: super(name, req)
66: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 13
13: def self.unpacked_path
14: @unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
15: end
Public Instance methods
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 276
276: def ==(other)
277: self.name == other.name && self.requirement == other.requirement
278: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 68
68: def add_load_paths
69: self.class.add_frozen_gem_path
70: return if @loaded || @load_paths_added
71: if framework_gem?
72: @load_paths_added = @loaded = @frozen = true
73: return
74: end
75: gem self
76: @spec = Gem.loaded_specs[name]
77: @frozen = @spec.loaded_from.include?(self.class.unpacked_path) if @spec
78: @load_paths_added = true
79: rescue Gem::LoadError
80: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 195
195: def build(options={})
196: require 'rails/gem_builder'
197: return if specification.nil?
198: if options[:force] || !built?
199: return unless File.exists?(unpacked_specification_filename)
200: spec = YAML::load_file(unpacked_specification_filename)
201: Rails::GemBuilder.new(spec, unpacked_gem_directory).build_extensions
202: puts "Built gem: '#{unpacked_gem_directory}'"
203: end
204: dependencies.each { |dep| dep.build(options) }
205: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 132
132: def built?
133: return false unless frozen?
134:
135: if vendor_gem?
136: specification.extensions.each do |ext|
137: makefile = File.join(unpacked_gem_directory, File.dirname(ext), 'Makefile')
138: return false unless File.exists?(makefile)
139: end
140: end
141:
142: true
143: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 82
82: def dependencies
83: return [] if framework_gem?
84: return [] unless installed?
85: specification.dependencies.reject do |dependency|
86: dependency.type == :development
87: end.map do |dependency|
88: GemDependency.new(dependency.name, :requirement => (dependency.respond_to?(:requirement) ? dependency.requirement : dependency.version_requirements))
89: end
90: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 145
145: def framework_gem?
146: @@framework_gems.has_key?(name)
147: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 149
149: def frozen?
150: @frozen ||= vendor_rails? || vendor_gem?
151: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 207
207: def install
208: unless installed?
209: cmd = "#{gem_command} #{install_command.join(' ')}"
210: puts cmd
211: puts %x(#{cmd})
212: end
213: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 153
153: def installed?
154: Gem.loaded_specs.keys.include?(name)
155: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 215
215: def load
216: return if @loaded || @load_paths_added == false
217: require(@lib || name) unless @lib == false
218: @loaded = true
219: rescue LoadError
220: puts $!.to_s
221: $!.backtrace.each { |b| puts b }
222: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 157
157: def load_paths_added?
158: # always try to add load paths - even if a gem is loaded, it may not
159: # be a compatible version (ie random_gem 0.4 is loaded and a later spec
160: # needs >= 0.5 - gem 'random_gem' will catch this and error out)
161: @load_paths_added
162: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 164
164: def loaded?
165: @loaded ||= begin
166: if vendor_rails?
167: true
168: elsif specification.nil?
169: false
170: else
171: # check if the gem is loaded by inspecting $"
172: # specification.files lists all the files contained in the gem
173: gem_files = specification.files
174: # select only the files contained in require_paths - typically in bin and lib
175: require_paths_regexp = Regexp.new("^(#{specification.require_paths*'|'})/")
176: gem_lib_files = gem_files.select { |f| require_paths_regexp.match(f) }
177: # chop the leading directory off - a typical file might be in
178: # lib/gem_name/file_name.rb, but it will be 'require'd as gem_name/file_name.rb
179: gem_lib_files.map! { |f| f.split('/', 2)[1] }
180: # if any of the files from the above list appear in $", the gem is assumed to
181: # have been loaded
182: !(gem_lib_files & $").empty?
183: end
184: end
185: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 224
224: def refresh
225: Rails::VendorGemSourceIndex.silence_spec_warnings = true
226: real_gems = Gem.source_index.installed_source_index
227: exact_dep = Gem::Dependency.new(name, "= #{specification.version}")
228: matches = real_gems.search(exact_dep)
229: installed_spec = matches.first
230: if frozen?
231: if installed_spec
232: # we have a real copy
233: # get a fresh spec - matches should only have one element
234: # note that there is no reliable method to check that the loaded
235: # spec is the same as the copy from real_gems - Gem.activate changes
236: # some of the fields
237: real_spec = Gem::Specification.load(matches.first.loaded_from)
238: write_specification(real_spec)
239: puts "Reloaded specification for #{name} from installed gems."
240: else
241: # the gem isn't installed locally - write out our current specs
242: write_specification(specification)
243: puts "Gem #{name} not loaded locally - writing out current spec."
244: end
245: else
246: if framework_gem?
247: puts "Gem directory for #{name} not found - check if it's loading before rails."
248: else
249: puts "Something bad is going on - gem directory not found for #{name}."
250: end
251: end
252: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 121
121: def requirement
122: req = super
123: req unless req == Gem::Requirement.default
124: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 126
126: def requirement
127: req = version_requirements
128: req unless req == Gem::Requirement.default
129: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 92
92: def specification
93: # code repeated from Gem.activate. Find a matching spec, or the currently loaded version.
94: # error out if loaded version and requested version are incompatible.
95: @spec ||= begin
96: matches = Gem.source_index.search(self)
97: matches << @@framework_gems[name] if framework_gem?
98: if Gem.loaded_specs[name] then
99: # This gem is already loaded. If the currently loaded gem is not in the
100: # list of candidate gems, then we have a version conflict.
101: existing_spec = Gem.loaded_specs[name]
102: unless matches.any? { |spec| spec.version == existing_spec.version } then
103: raise Gem::Exception,
104: "can't activate #{@dep}, already activated #{existing_spec.full_name}"
105: end
106: # we're stuck with it, so change to match
107: version_requirements = Gem::Requirement.create("=#{existing_spec.version}")
108: existing_spec
109: else
110: # new load
111: matches.last
112: end
113: end
114: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 116
116: def specification=(s)
117: @spec = s
118: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 254
254: def unpack(options={})
255: unless frozen? || framework_gem?
256: FileUtils.mkdir_p unpack_base
257: Dir.chdir unpack_base do
258: Gem::GemRunner.new.run(unpack_command)
259: end
260: # Gem.activate changes the spec - get the original
261: real_spec = Gem::Specification.load(specification.loaded_from)
262: write_specification(real_spec)
263: end
264: dependencies.each { |dep| dep.unpack(options) } if options[:recursive]
265: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 191
191: def vendor_gem?
192: specification && File.exists?(unpacked_gem_directory)
193: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 187
187: def vendor_rails?
188: Gem.loaded_specs.has_key?(name) && Gem.loaded_specs[name].loaded_from.empty?
189: end
[ show source ]
# File railties/lib/rails/gem_dependency.rb, line 267
267: def write_specification(spec)
268: # copy the gem's specification into GEMDIR/.specification so that
269: # we can access information about the gem on deployment systems
270: # without having the gem installed
271: File.open(unpacked_specification_filename, 'w') do |file|
272: file.puts spec.to_yaml
273: end
274: end