blob: 2e768b745b80c1a4bdcfaaa8b98e2612a005b450 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# frozen_string_literal: true
require "bundler/cli/common"
module Bundler
class CLI::Inject
attr_reader :options, :name, :version, :gems
def initialize(options, name, version, gems)
@options = options
@name = name
@version = version || last_version_number
@gems = gems
end
def run
# The required arguments allow Thor to give useful feedback when the arguments
# are incorrect. This adds those first two arguments onto the list as a whole.
gems.unshift(version).unshift(name)
# Build an array of Dependency objects out of the arguments
deps = []
gems.each_slice(2) do |gem_name, gem_version|
deps << Bundler::Dependency.new(gem_name, gem_version)
end
added = Injector.inject(deps)
if added.any?
Bundler.ui.confirm "Added to Gemfile:"
Bundler.ui.confirm added.map {|g| " #{g}" }.join("\n")
else
Bundler.ui.confirm "All injected gems were already present in the Gemfile"
end
end
private
def last_version_number
definition = Bundler.definition(true)
definition.resolve_remotely!
specs = definition.index[name].sort_by(&:version)
spec = specs.delete_if {|b| b.respond_to?(:version) && b.version.prerelease? }
spec = specs.last
spec.version.to_s
end
end
end
|