Class: Bundler::Source::Rubygems

Inherits:
Bundler::Source show all
Defined in:
lib/bundler/source/rubygems.rb,
lib/bundler/source/rubygems/remote.rb

Direct Known Subclasses

Plugin::Installer::Rubygems

Defined Under Namespace

Classes: Remote

Constant Summary collapse

API_REQUEST_LIMIT =

Use the API when installing less than X gems

500
API_REQUEST_SIZE =

Ask for X gems per API request

50

Instance Attribute Summary collapse

Attributes inherited from Bundler::Source

#dependency_names

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Bundler::Source

#add_dependency_names, #extension_cache_path, #inspect, #path?, #version_message

Constructor Details

#initialize(options = {}) ⇒ Rubygems

Returns a new instance of Rubygems.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bundler/source/rubygems.rb', line 17

def initialize(options = {})
  @options = options
  @remotes = []
  @dependency_names = []
  @allow_remote = false
  @allow_cached = false
  @allow_local = options["allow_local"] || false
  @caches = [cache_path, *Bundler.rubygems.gem_cache]

  Array(options["remotes"]).reverse_each {|r| add_remote(r) }
end

Instance Attribute Details

#cachesObject (readonly)

Returns the value of attribute caches.



15
16
17
# File 'lib/bundler/source/rubygems.rb', line 15

def caches
  @caches
end

#remotesObject (readonly)

Returns the value of attribute remotes.



15
16
17
# File 'lib/bundler/source/rubygems.rb', line 15

def remotes
  @remotes
end

Class Method Details

.from_lock(options) ⇒ Object



89
90
91
# File 'lib/bundler/source/rubygems.rb', line 89

def self.from_lock(options)
  new(options)
end

Instance Method Details

#add_remote(source) ⇒ Object



232
233
234
235
# File 'lib/bundler/source/rubygems.rb', line 232

def add_remote(source)
  uri = normalize_uri(source)
  @remotes.unshift(uri) unless @remotes.include?(uri)
end

#cache(spec, custom_path = nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
# File 'lib/bundler/source/rubygems.rb', line 208

def cache(spec, custom_path = nil)
  cached_path = Bundler.settings[:cache_all_platforms] ? fetch_gem_if_possible(spec) : cached_gem(spec)
  raise GemNotFound, "Missing gem file '#{spec.file_name}'." unless cached_path
  return if File.dirname(cached_path) == Bundler.app_cache.to_s
  Bundler.ui.info "  * #{File.basename(cached_path)}"
  FileUtils.cp(cached_path, Bundler.app_cache(custom_path))
rescue Errno::EACCES => e
  Bundler.ui.debug(e)
  raise InstallError, e.message
end

#cached!Object



50
51
52
53
54
55
56
# File 'lib/bundler/source/rubygems.rb', line 50

def cached!
  return if @allow_cached

  @specs = nil
  @allow_local = true
  @allow_cached = true
end

#cached_built_in_gem(spec) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/bundler/source/rubygems.rb', line 219

def cached_built_in_gem(spec)
  cached_path = cached_path(spec)
  if cached_path.nil?
    remote_spec = remote_specs.search(spec).first
    if remote_spec
      cached_path = fetch_gem(remote_spec)
    else
      Bundler.ui.warn "#{spec.full_name} is built in to Ruby, and can't be cached because your Gemfile doesn't have any sources that contain it."
    end
  end
  cached_path
end

#can_lock?(spec) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/bundler/source/rubygems.rb', line 80

def can_lock?(spec)
  return super unless multiple_remotes?
  include?(spec.source)
end

#dependency_api_available?Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/bundler/source/rubygems.rb', line 294

def dependency_api_available?
  @allow_remote && api_fetchers.any?
end

#dependency_names_to_double_checkObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/bundler/source/rubygems.rb', line 279

def dependency_names_to_double_check
  names = []
  remote_specs.each do |spec|
    case spec
    when EndpointSpecification, Gem::Specification, StubSpecification, LazySpecification
      names.concat(spec.runtime_dependencies.map(&:name))
    when RemoteSpecification # from the full index
      return nil
    else
      raise "unhandled spec type (#{spec.inspect})"
    end
  end
  names
end

#double_check_for(unmet_dependency_names) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/bundler/source/rubygems.rb', line 260

def double_check_for(unmet_dependency_names)
  return unless @allow_remote
  return unless dependency_api_available?

  unmet_dependency_names = unmet_dependency_names.call
  unless unmet_dependency_names.nil?
    if api_fetchers.size <= 1
      # can't do this when there are multiple fetchers because then we might not fetch from _all_
      # of them
      unmet_dependency_names -= remote_specs.spec_names # avoid re-fetching things we've already gotten
    end
    return if unmet_dependency_names.empty?
  end

  Bundler.ui.debug "Double checking for #{unmet_dependency_names || "all specs (due to the size of the request)"} in #{self}"

  fetch_names(api_fetchers, unmet_dependency_names, specs, false)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


62
63
64
# File 'lib/bundler/source/rubygems.rb', line 62

def eql?(other)
  other.is_a?(Rubygems) && other.credless_remotes == credless_remotes
end

#fetchersObject



253
254
255
256
257
258
# File 'lib/bundler/source/rubygems.rb', line 253

def fetchers
  @fetchers ||= remotes.map do |uri|
    remote = Source::Rubygems::Remote.new(uri)
    Bundler::Fetcher.new(remote)
  end
end

#hashObject



58
59
60
# File 'lib/bundler/source/rubygems.rb', line 58

def hash
  @remotes.hash
end

#identifierObject Also known as: name



117
118
119
120
121
122
123
# File 'lib/bundler/source/rubygems.rb', line 117

def identifier
  if remotes.empty?
    "locally installed gems"
  else
    "rubygems repository #{remote_names}"
  end
end

#include?(o) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/bundler/source/rubygems.rb', line 68

def include?(o)
  o.is_a?(Rubygems) && (o.credless_remotes - credless_remotes).empty?
end

#install(spec, options = {}) ⇒ Object

Raises:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/bundler/source/rubygems.rb', line 138

def install(spec, options = {})
  force = options[:force]
  ensure_builtin_gems_cached = options[:ensure_builtin_gems_cached]

  if ensure_builtin_gems_cached && spec.default_gem? && !cached_path(spec)
    cached_built_in_gem(spec) unless spec.remote
    force = true
  end

  if installed?(spec) && !force
    print_using_message "Using #{version_message(spec, options[:previous_spec])}"
    return nil # no post-install message
  end

  if spec.remote
    # Check for this spec from other sources
    uris = [spec.remote, *remotes_for_spec(spec)].map(&:anonymized_uri).uniq
    Installer.ambiguous_gems << [spec.name, *uris] if uris.length > 1
  end

  path = fetch_gem_if_possible(spec, options[:previous_spec])
  raise GemNotFound, "Could not find #{spec.file_name} for installation" unless path

  return if Bundler.settings[:no_install]

  install_path = rubygems_dir
  bin_path     = Bundler.system_bindir

  require_relative "../rubygems_gem_installer"

  installer = Bundler::RubyGemsGemInstaller.at(
    path,
    :security_policy => Bundler.rubygems.security_policies[Bundler.settings["trust-policy"]],
    :install_dir => install_path.to_s,
    :bin_dir => bin_path.to_s,
    :ignore_dependencies => true,
    :wrappers => true,
    :env_shebang => true,
    :build_args => options[:build_args],
    :bundler_expected_checksum => spec.respond_to?(:checksum) && spec.checksum,
    :bundler_extension_cache_path => extension_cache_path(spec)
  )

  if spec.remote
    s = begin
      installer.spec
    rescue Gem::Package::FormatError
      Bundler.rm_rf(path)
      raise
    rescue Gem::Security::Exception => e
      raise SecurityError,
       "The gem #{File.basename(path, ".gem")} can't be installed because " \
       "the security policy didn't allow it, with the message: #{e.message}"
    end

    spec.__swap__(s)
  end

  message = "Installing #{version_message(spec, options[:previous_spec])}"
  message += " with native extensions" if spec.extensions.any?
  Bundler.ui.confirm message

  installed_spec = installer.install

  spec.full_gem_path = installed_spec.full_gem_path
  spec.loaded_from = installed_spec.loaded_from

  spec.post_install_message
end

#local!Object



36
37
38
39
40
41
# File 'lib/bundler/source/rubygems.rb', line 36

def local!
  return if @allow_local

  @specs = nil
  @allow_local = true
end

#local_only!Object



29
30
31
32
33
34
# File 'lib/bundler/source/rubygems.rb', line 29

def local_only!
  @specs = nil
  @allow_local = true
  @allow_cached = false
  @allow_remote = false
end

#multiple_remotes?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/bundler/source/rubygems.rb', line 72

def multiple_remotes?
  @remotes.size > 1
end

#no_remotes?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/bundler/source/rubygems.rb', line 76

def no_remotes?
  @remotes.size == 0
end

#optionsObject



85
86
87
# File 'lib/bundler/source/rubygems.rb', line 85

def options
  { "remotes" => @remotes.map(&:to_s) }
end

#remote!Object



43
44
45
46
47
48
# File 'lib/bundler/source/rubygems.rb', line 43

def remote!
  return if @allow_remote

  @specs = nil
  @allow_remote = true
end

#spec_namesObject



237
238
239
240
241
242
243
# File 'lib/bundler/source/rubygems.rb', line 237

def spec_names
  if @allow_remote && dependency_api_available?
    remote_specs.spec_names
  else
    []
  end
end

#specsObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bundler/source/rubygems.rb', line 126

def specs
  @specs ||= begin
    # remote_specs usually generates a way larger Index than the other
    # sources, and large_idx.use small_idx is way faster than
    # small_idx.use large_idx.
    idx = @allow_remote ? remote_specs.dup : Index.new
    idx.use(cached_specs, :override_dupes) if @allow_cached || @allow_remote
    idx.use(installed_specs, :override_dupes) if @allow_local
    idx
  end
end

#to_lockObject



93
94
95
96
97
98
99
# File 'lib/bundler/source/rubygems.rb', line 93

def to_lock
  out = String.new("GEM\n")
  remotes.reverse_each do |remote|
    out << "  remote: #{suppress_configured_credentials remote}\n"
  end
  out << "  specs:\n"
end

#to_sObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bundler/source/rubygems.rb', line 101

def to_s
  if remotes.empty?
    "locally installed gems"
  elsif @allow_remote && @allow_cached && @allow_local
    "rubygems repository #{remote_names}, cached gems or installed locally"
  elsif @allow_remote && @allow_local
    "rubygems repository #{remote_names} or installed locally"
  elsif @allow_remote
    "rubygems repository #{remote_names}"
  elsif @allow_cached && @allow_local
    "cached gems or installed locally"
  else
    "locally installed gems"
  end
end

#unmet_depsObject



245
246
247
248
249
250
251
# File 'lib/bundler/source/rubygems.rb', line 245

def unmet_deps
  if @allow_remote && dependency_api_available?
    remote_specs.unmet_dependency_names
  else
    []
  end
end