2018-11-02 19:07:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Bundler
|
|
|
|
class CLI::Outdated
|
2019-07-24 07:00:30 -04:00
|
|
|
attr_reader :options, :gems, :options_include_groups, :filter_options_patch, :sources, :strict
|
2020-05-08 01:19:04 -04:00
|
|
|
attr_accessor :outdated_gems
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
def initialize(options, gems)
|
|
|
|
@options = options
|
|
|
|
@gems = gems
|
2019-07-24 07:38:47 -04:00
|
|
|
@sources = Array(options[:source])
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
@filter_options_patch = options.keys & %w[filter-major filter-minor filter-patch]
|
2019-07-24 07:38:47 -04:00
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
@outdated_gems = []
|
2019-07-24 07:30:56 -04:00
|
|
|
|
2019-07-24 07:34:52 -04:00
|
|
|
@options_include_groups = [:group, :groups].any? do |v|
|
2019-07-24 07:30:56 -04:00
|
|
|
options.keys.include?(v.to_s)
|
|
|
|
end
|
2019-07-24 07:00:30 -04:00
|
|
|
|
|
|
|
# the patch level options imply strict is also true. It wouldn't make
|
|
|
|
# sense otherwise.
|
2020-05-08 01:19:04 -04:00
|
|
|
@strict = options["filter-strict"] || Bundler::CLI::Common.patch_level_options(options).any?
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2019-07-24 07:00:46 -04:00
|
|
|
check_for_deployment_mode!
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
gems.each do |gem_name|
|
|
|
|
Bundler::CLI::Common.select_spec(gem_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
Bundler.definition.validate_runtime!
|
|
|
|
current_specs = Bundler.ui.silence { Bundler.definition.resolve }
|
2019-04-12 12:52:52 -04:00
|
|
|
|
|
|
|
current_dependencies = Bundler.ui.silence do
|
|
|
|
Bundler.load.dependencies.map {|dep| [dep.name, dep] }.to_h
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
definition = if gems.empty? && sources.empty?
|
|
|
|
# We're doing a full update
|
|
|
|
Bundler.definition(true)
|
|
|
|
else
|
|
|
|
Bundler.definition(:gems => gems, :sources => sources)
|
|
|
|
end
|
|
|
|
|
|
|
|
Bundler::CLI::Common.configure_gem_version_promoter(
|
|
|
|
Bundler.definition,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
|
|
|
|
definition_resolution = proc do
|
|
|
|
options[:local] ? definition.resolve_with_cache! : definition.resolve_remotely!
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:parseable]
|
|
|
|
Bundler.ui.silence(&definition_resolution)
|
|
|
|
else
|
|
|
|
definition_resolution.call
|
|
|
|
end
|
|
|
|
|
|
|
|
Bundler.ui.info ""
|
|
|
|
|
|
|
|
# Loop through the current specs
|
|
|
|
gemfile_specs, dependency_specs = current_specs.partition do |spec|
|
|
|
|
current_dependencies.key? spec.name
|
|
|
|
end
|
|
|
|
|
|
|
|
specs = if options["only-explicit"]
|
|
|
|
gemfile_specs
|
|
|
|
else
|
|
|
|
gemfile_specs + dependency_specs
|
|
|
|
end
|
|
|
|
|
2021-04-14 23:47:04 -04:00
|
|
|
specs.sort_by(&:name).uniq(&:name).each do |current_spec|
|
2020-05-08 01:19:04 -04:00
|
|
|
next unless gems.empty? || gems.include?(current_spec.name)
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2019-07-24 07:00:30 -04:00
|
|
|
active_spec = retrieve_active_spec(definition, current_spec)
|
2020-12-14 18:32:54 -05:00
|
|
|
next unless active_spec
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
next unless filter_options_patch.empty? || update_present_via_semver_portions(current_spec, active_spec, options)
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
gem_outdated = Gem::Version.new(active_spec.version) > Gem::Version.new(current_spec.version)
|
|
|
|
next unless gem_outdated || (current_spec.git_version != active_spec.git_version)
|
2020-05-08 01:19:04 -04:00
|
|
|
|
|
|
|
dependency = current_dependencies[current_spec.name]
|
|
|
|
groups = ""
|
2018-11-02 19:07:56 -04:00
|
|
|
if dependency && !options[:parseable]
|
|
|
|
groups = dependency.groups.join(", ")
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
outdated_gems << {
|
|
|
|
:active_spec => active_spec,
|
|
|
|
:current_spec => current_spec,
|
|
|
|
:dependency => dependency,
|
|
|
|
:groups => groups,
|
|
|
|
}
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
if outdated_gems.empty?
|
2018-11-02 19:07:56 -04:00
|
|
|
unless options[:parseable]
|
2020-05-08 01:19:04 -04:00
|
|
|
Bundler.ui.info(nothing_outdated_message)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
2020-05-08 01:19:04 -04:00
|
|
|
else
|
2019-07-24 07:34:52 -04:00
|
|
|
if options_include_groups
|
2020-05-08 01:19:04 -04:00
|
|
|
relevant_outdated_gems = outdated_gems.group_by {|g| g[:groups] }.sort.flat_map do |groups, gems|
|
|
|
|
contains_group = groups.split(", ").include?(options[:group])
|
|
|
|
next unless options[:groups] || contains_group
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
gems
|
|
|
|
end.compact
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
if options[:parseable]
|
|
|
|
relevant_outdated_gems.each do |gems|
|
|
|
|
print_gems(gems)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
2020-05-08 01:19:04 -04:00
|
|
|
else
|
|
|
|
print_gems_table(relevant_outdated_gems)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
2020-05-08 01:19:04 -04:00
|
|
|
elsif options[:parseable]
|
|
|
|
print_gems(outdated_gems)
|
2018-11-02 19:07:56 -04:00
|
|
|
else
|
2020-05-08 01:19:04 -04:00
|
|
|
print_gems_table(outdated_gems)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-15 00:20:25 -04:00
|
|
|
private
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2019-06-01 05:49:40 -04:00
|
|
|
def groups_text(group_text, groups)
|
|
|
|
"#{group_text}#{groups.split(",").size > 1 ? "s" : ""} \"#{groups}\""
|
|
|
|
end
|
|
|
|
|
2019-07-24 10:09:35 -04:00
|
|
|
def nothing_outdated_message
|
|
|
|
if filter_options_patch.any?
|
|
|
|
display = filter_options_patch.map do |o|
|
|
|
|
o.sub("filter-", "")
|
|
|
|
end.join(" or ")
|
|
|
|
|
|
|
|
"No #{display} updates to display.\n"
|
|
|
|
else
|
|
|
|
"Bundle up to date!\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-24 07:00:30 -04:00
|
|
|
def retrieve_active_spec(definition, current_spec)
|
2021-05-28 06:47:49 -04:00
|
|
|
active_spec = definition.resolve.find_by_name_and_platform(current_spec.name, current_spec.platform)
|
|
|
|
return unless active_spec
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2021-05-28 06:47:49 -04:00
|
|
|
return active_spec if strict
|
|
|
|
|
|
|
|
active_specs = active_spec.source.specs.search(current_spec.name).select {|spec| spec.match_platform(current_spec.platform) }.sort_by(&:version)
|
|
|
|
if !current_spec.version.prerelease? && !options[:pre] && active_specs.size > 1
|
|
|
|
active_specs.delete_if {|b| b.respond_to?(:version) && b.version.prerelease? }
|
|
|
|
end
|
|
|
|
active_specs.last
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
2019-04-12 12:59:35 -04:00
|
|
|
def print_gems(gems_list)
|
|
|
|
gems_list.each do |gem|
|
|
|
|
print_gem(
|
|
|
|
gem[:current_spec],
|
|
|
|
gem[:active_spec],
|
|
|
|
gem[:dependency],
|
|
|
|
gem[:groups],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def print_gems_table(gems_list)
|
|
|
|
data = gems_list.map do |gem|
|
|
|
|
gem_column_for(
|
|
|
|
gem[:current_spec],
|
|
|
|
gem[:active_spec],
|
|
|
|
gem[:dependency],
|
|
|
|
gem[:groups],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
print_indented([table_header] + data)
|
|
|
|
end
|
|
|
|
|
2019-07-24 07:34:52 -04:00
|
|
|
def print_gem(current_spec, active_spec, dependency, groups)
|
2018-11-02 19:07:56 -04:00
|
|
|
spec_version = "#{active_spec.version}#{active_spec.git_version}"
|
|
|
|
spec_version += " (from #{active_spec.loaded_from})" if Bundler.ui.debug? && active_spec.loaded_from
|
|
|
|
current_version = "#{current_spec.version}#{current_spec.git_version}"
|
|
|
|
|
|
|
|
if dependency && dependency.specific?
|
|
|
|
dependency_version = %(, requested #{dependency.requirement})
|
|
|
|
end
|
|
|
|
|
|
|
|
spec_outdated_info = "#{active_spec.name} (newest #{spec_version}, " \
|
|
|
|
"installed #{current_version}#{dependency_version})"
|
|
|
|
|
|
|
|
output_message = if options[:parseable]
|
|
|
|
spec_outdated_info.to_s
|
2020-05-08 01:19:04 -04:00
|
|
|
elsif options_include_groups || groups.empty?
|
2018-11-02 19:07:56 -04:00
|
|
|
" * #{spec_outdated_info}"
|
|
|
|
else
|
2019-06-01 05:49:40 -04:00
|
|
|
" * #{spec_outdated_info} in #{groups_text("group", groups)}"
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Bundler.ui.info output_message.rstrip
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def gem_column_for(current_spec, active_spec, dependency, groups)
|
|
|
|
current_version = "#{current_spec.version}#{current_spec.git_version}"
|
|
|
|
spec_version = "#{active_spec.version}#{active_spec.git_version}"
|
|
|
|
dependency = dependency.requirement if dependency
|
|
|
|
|
|
|
|
ret_val = [active_spec.name, current_version, spec_version, dependency.to_s, groups.to_s]
|
|
|
|
ret_val << active_spec.loaded_from.to_s if Bundler.ui.debug?
|
|
|
|
ret_val
|
|
|
|
end
|
|
|
|
|
2019-07-24 07:00:46 -04:00
|
|
|
def check_for_deployment_mode!
|
2018-11-02 19:07:56 -04:00
|
|
|
return unless Bundler.frozen_bundle?
|
2020-05-25 06:18:44 -04:00
|
|
|
suggested_command = if Bundler.settings.locations("frozen").keys.&([:global, :local]).any?
|
2019-04-14 02:01:35 -04:00
|
|
|
"bundle config unset frozen"
|
2018-11-02 19:07:56 -04:00
|
|
|
elsif Bundler.settings.locations("deployment").keys.&([:global, :local]).any?
|
2019-04-14 02:01:35 -04:00
|
|
|
"bundle config unset deployment"
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
raise ProductionError, "You are trying to check outdated gems in " \
|
|
|
|
"deployment mode. Run `bundle outdated` elsewhere.\n" \
|
|
|
|
"\nIf this is a development machine, remove the " \
|
|
|
|
"#{Bundler.default_gemfile} freeze" \
|
|
|
|
"\nby running `#{suggested_command}`."
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_present_via_semver_portions(current_spec, active_spec, options)
|
|
|
|
current_major = current_spec.version.segments.first
|
|
|
|
active_major = active_spec.version.segments.first
|
|
|
|
|
|
|
|
update_present = false
|
|
|
|
update_present = active_major > current_major if options["filter-major"]
|
|
|
|
|
|
|
|
if !update_present && (options["filter-minor"] || options["filter-patch"]) && current_major == active_major
|
|
|
|
current_minor = get_version_semver_portion_value(current_spec, 1)
|
|
|
|
active_minor = get_version_semver_portion_value(active_spec, 1)
|
|
|
|
|
|
|
|
update_present = active_minor > current_minor if options["filter-minor"]
|
|
|
|
|
|
|
|
if !update_present && options["filter-patch"] && current_minor == active_minor
|
|
|
|
current_patch = get_version_semver_portion_value(current_spec, 2)
|
|
|
|
active_patch = get_version_semver_portion_value(active_spec, 2)
|
|
|
|
|
|
|
|
update_present = active_patch > current_patch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
update_present
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_version_semver_portion_value(spec, version_portion_index)
|
|
|
|
version_section = spec.version.segments[version_portion_index, 1]
|
2019-04-12 12:52:52 -04:00
|
|
|
version_section.to_a[0].to_i
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
2020-05-08 01:19:04 -04:00
|
|
|
|
|
|
|
def print_indented(matrix)
|
|
|
|
header = matrix[0]
|
|
|
|
data = matrix[1..-1]
|
|
|
|
|
|
|
|
column_sizes = Array.new(header.size) do |index|
|
|
|
|
matrix.max_by {|row| row[index].length }[index].length
|
|
|
|
end
|
|
|
|
|
|
|
|
Bundler.ui.info justify(header, column_sizes)
|
|
|
|
|
|
|
|
data.sort_by! {|row| row[0] }
|
|
|
|
|
|
|
|
data.each do |row|
|
|
|
|
Bundler.ui.info justify(row, column_sizes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def table_header
|
|
|
|
header = ["Gem", "Current", "Latest", "Requested", "Groups"]
|
|
|
|
header << "Path" if Bundler.ui.debug?
|
|
|
|
header
|
|
|
|
end
|
|
|
|
|
|
|
|
def justify(row, sizes)
|
|
|
|
row.each_with_index.map do |element, index|
|
|
|
|
element.ljust(sizes[index])
|
|
|
|
end.join(" ").strip + "\n"
|
|
|
|
end
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|