Method: Bundler::RubygemsIntegration#replace_gem

Defined in:
lib/bundler/rubygems_integration.rb

#replace_gem(specs_by_name) ⇒ Object



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
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/bundler/rubygems_integration.rb', line 180

def replace_gem(specs_by_name)
  executables = nil

  [::Kernel.singleton_class, ::Kernel].each do |kernel_class|
    redefine_method(kernel_class, :gem) do |dep, *reqs|
      if executables&.include?(File.basename(caller_locations(1, 1).first.path))
        break
      end

      reqs.pop if reqs.last.is_a?(Hash)

      unless dep.respond_to?(:name) && dep.respond_to?(:requirement)
        dep = Gem::Dependency.new(dep, reqs)
      end

      if spec = specs_by_name[dep.name]
        return true if dep.matches_spec?(spec)
      end

      message = if spec.nil?
        target_file = begin
                        Bundler.default_gemfile.basename
                      rescue GemfileNotFound
                        "inline Gemfile"
                      end
        "#{dep.name} is not part of the bundle." \
        " Add it to your #{target_file}."
      else
        "can't activate #{dep}, already activated #{spec.full_name}. " \
        "Make sure all dependencies are added to Gemfile."
      end

      e = Gem::LoadError.new(message)
      e.name = dep.name
      e.requirement = dep.requirement
      raise e
    end
  end
end