Object
TODO: Refactor this class
# File lib/bundler/source.rb, line 46
46: def self.from_lock(options)
47: s = new(options)
48: Array(options["remote"]).each { |r| s.add_remote(r) }
49: s
50: end
# File lib/bundler/source.rb, line 14
14: def initialize(options = {})
15: @options = options
16: @remotes = (options["remotes"] || []).map { |r| normalize_uri(r) }
17: @allow_remote = false
18: @allow_cached = false
19: # Hardcode the paths for now
20: @caches = [ Bundler.app_cache ] + Gem.path.map { |p| File.expand_path("#{p}/cache") }
21: @spec_fetch_map = {}
22: end
# File lib/bundler/source.rb, line 123
123: def add_remote(source)
124: @remotes << normalize_uri(source)
125: end
# File lib/bundler/source.rb, line 115
115: def cache(spec)
116: cached_path = cached_gem(spec)
117: raise GemNotFound, "Missing gem file '#{spec.full_name}.gem'." unless cached_path
118: return if File.dirname(cached_path) == Bundler.app_cache.to_s
119: Bundler.ui.info " * #{File.basename(cached_path)}"
120: FileUtils.cp(cached_path, Bundler.app_cache)
121: end
# File lib/bundler/source.rb, line 28
28: def cached!
29: @allow_cached = true
30: end
# File lib/bundler/source.rb, line 36
36: def eql?(o)
37: Rubygems === o
38: end
# File lib/bundler/source.rb, line 68
68: def fetch(spec)
69: spec, uri = @spec_fetch_map[spec.full_name]
70: if spec
71: path = download_gem_from_uri(spec, uri)
72: s = Gem::Format.from_file_by_path(path).spec
73: spec.__swap__(s)
74: end
75: end
# File lib/bundler/source.rb, line 32
32: def hash
33: Rubygems.hash
34: end
# File lib/bundler/source.rb, line 77
77: def install(spec)
78: path = cached_gem(spec)
79:
80: if installed_specs[spec].any?
81: Bundler.ui.info "Using #{spec.name} (#{spec.version}) "
82: return
83: end
84:
85: Bundler.ui.info "Installing #{spec.name} (#{spec.version}) "
86:
87: install_path = Bundler.requires_sudo? ? Bundler.tmp : Gem.dir
88: options = { :install_dir => install_path,
89: :ignore_dependencies => true,
90: :wrappers => true,
91: :env_shebang => true }
92: options.merge!(:bin_dir => "#{install_path}/bin") unless spec.executables.nil? || spec.executables.empty?
93:
94: installer = Gem::Installer.new path, options
95: installer.install
96:
97: # SUDO HAX
98: if Bundler.requires_sudo?
99: sudo "mkdir -p #{Gem.dir}/gems #{Gem.dir}/specifications"
100: sudo "cp -R #{Bundler.tmp}/gems/#{spec.full_name} #{Gem.dir}/gems/"
101: sudo "cp -R #{Bundler.tmp}/specifications/#{spec.full_name}.gemspec #{Gem.dir}/specifications/"
102: spec.executables.each do |exe|
103: sudo "mkdir -p #{Gem.bindir}"
104: sudo "cp -R #{Bundler.tmp}/bin/#{exe} #{Gem.bindir}"
105: end
106: end
107:
108: spec.loaded_from = "#{Gem.dir}/specifications/#{spec.full_name}.gemspec"
109: end
# File lib/bundler/source.rb, line 127
127: def merge_remotes(source)
128: @remotes = []
129: source.remotes.each do |r|
130: add_remote r.to_s
131: end
132: end
# File lib/bundler/source.rb, line 42
42: def options
43: { "remotes" => @remotes.map { |r| r.to_s } }
44: end
# File lib/bundler/source.rb, line 24
24: def remote!
25: @allow_remote = true
26: end
# File lib/bundler/source.rb, line 64
64: def specs
65: @specs ||= fetch_specs
66: end
# File lib/bundler/source.rb, line 111
111: def sudo(str)
112: Bundler.sudo(str)
113: end
# File lib/bundler/source.rb, line 52
52: def to_lock
53: out = "GEM\n"
54: out << remotes.map {|r| " remote: #{r}\n" }.join
55: out << " specs:\n"
56: end
# File lib/bundler/source.rb, line 58
58: def to_s
59: remote_names = self.remotes.map { |r| r.to_s }.join(', ')
60: "rubygems repository #{remote_names}"
61: end
# File lib/bundler/source.rb, line 136
136: def cached_gem(spec)
137: possibilities = @caches.map { |p| "#{p}/#{spec.full_name}.gem" }
138: possibilities.find { |p| File.exist?(p) }
139: end
# File lib/bundler/source.rb, line 186
186: def cached_specs
187: @cached_specs ||= begin
188: idx = installed_specs.dup
189:
190: path = Bundler.app_cache
191: Dir["#{path}/*.gem"].each do |gemfile|
192: next if gemfile =~ /bundler\-[\d\.]+?\.gem/
193:
194: begin
195: s ||= Gem::Format.from_file_by_path(gemfile).spec
196: rescue Gem::Package::FormatError
197: raise GemspecError, "Could not read gem at #{gemfile}. It may be corrupted."
198: end
199:
200: s.source = self
201: idx << s
202: end
203: end
204:
205: idx
206: end
# File lib/bundler/source.rb, line 247
247: def download_gem_from_uri(spec, uri)
248: spec.fetch_platform
249:
250: download_path = Bundler.requires_sudo? ? Bundler.tmp : Gem.dir
251: gem_path = "#{Gem.dir}/cache/#{spec.full_name}.gem"
252:
253: FileUtils.mkdir_p("#{download_path}/cache")
254: Gem::RemoteFetcher.fetcher.download(spec, uri, download_path)
255:
256: if Bundler.requires_sudo?
257: sudo "mkdir -p #{Gem.dir}/cache"
258: sudo "mv #{Bundler.tmp}/cache/#{spec.full_name}.gem #{gem_path}"
259: end
260:
261: gem_path
262: end
# File lib/bundler/source.rb, line 232
232: def fetch_all_remote_specs(&blk)
233: begin
234: # Fetch all specs, minus prerelease specs
235: Gem::SpecFetcher.new.list(true, false).each(&blk)
236: # Then fetch the prerelease specs
237: begin
238: Gem::SpecFetcher.new.list(false, true).each(&blk)
239: rescue Gem::RemoteFetcher::FetchError
240: Bundler.ui.warn "Could not fetch prerelease specs from #{self}"
241: end
242: rescue Gem::RemoteFetcher::FetchError
243: Bundler.ui.warn "Could not reach #{self}"
244: end
245: end
# File lib/bundler/source.rb, line 149
149: def fetch_specs
150: Index.build do |idx|
151: idx.use installed_specs
152: idx.use cached_specs if @allow_cached || @allow_remote
153: idx.use remote_specs if @allow_remote
154: end
155: end
# File lib/bundler/source.rb, line 157
157: def installed_specs
158: @installed_specs ||= begin
159: idx = Index.new
160: have_bundler = false
161: Gem.source_index.to_a.reverse.each do |dont_use_this_var, spec|
162: next if spec.name == 'bundler' && spec.version.to_s != VERSION
163: have_bundler = true if spec.name == 'bundler'
164: spec.source = self
165: idx << spec
166: end
167:
168: # Always have bundler locally
169: unless have_bundler
170: # We're running bundler directly from the source
171: # so, let's create a fake gemspec for it (it's a path)
172: # gemspec
173: bundler = Gem::Specification.new do |s|
174: s.name = 'bundler'
175: s.version = VERSION
176: s.platform = Gem::Platform::RUBY
177: s.source = self
178: s.loaded_from = File.expand_path("..", __FILE__)
179: end
180: idx << bundler
181: end
182: idx
183: end
184: end
# File lib/bundler/source.rb, line 141
141: def normalize_uri(uri)
142: uri = uri.to_s
143: uri = "#{uri}/" unless uri =~ %/$'
144: uri = URI(uri)
145: raise ArgumentError, "The source must be an absolute URI" unless uri.absolute?
146: uri
147: end
# File lib/bundler/source.rb, line 208
208: def remote_specs
209: @remote_specs ||= begin
210: idx = Index.new
211: old = Gem.sources
212:
213: remotes.each do |uri|
214: Bundler.ui.info "Fetching source index for #{uri}"
215: Gem.sources = ["#{uri}"]
216: fetch_all_remote_specs do |n,v|
217: v.each do |name, version, platform|
218: next if name == 'bundler'
219: spec = RemoteSpecification.new(name, version, platform, uri)
220: spec.source = self
221: @spec_fetch_map[spec.full_name] = [spec, uri]
222: idx << spec
223: end
224: end
225: end
226: idx
227: ensure
228: Gem.sources = old
229: end
230: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.