| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
class CLI::Outdated |
| 5 |
attr_reader :options, :gems, :options_include_groups, :filter_options_patch, :sources, :strict |
| 6 |
attr_accessor :outdated_gems |
| 7 |
|
| 8 |
def initialize(options, gems) |
| 9 |
@options = options |
| 10 |
@gems = gems |
| 11 |
@sources = Array(options[:source]) |
| 12 |
|
| 13 |
@filter_options_patch = options.keys & %w[filter-major filter-minor filter-patch] |
| 14 |
|
| 15 |
@outdated_gems = [] |
| 16 |
|
| 17 |
@options_include_groups = [:group, :groups].any? do |v| |
| 18 |
options.keys.include?(v.to_s) |
| 19 |
end |
| 20 |
|
| 21 |
# the patch level options imply strict is also true. It wouldn't make |
| 22 |
# sense otherwise. |
| 23 |
@strict = options["filter-strict"] || Bundler::CLI::Common.patch_level_options(options).any? |
| 24 |
end |
| 25 |
|
| 26 |
def run |
| 27 |
check_for_deployment_mode! |
| 28 |
|
| 29 |
gems.each do |gem_name| |
| 30 |
Bundler::CLI::Common.select_spec(gem_name) |
| 31 |
end |
| 32 |
|
| 33 |
Bundler.definition.validate_runtime! |
| 34 |
current_specs = Bundler.ui.silence { Bundler.definition.resolve } |
| 35 |
|
| 36 |
current_dependencies = Bundler.ui.silence do |
| 37 |
Bundler.load.dependencies.map {|dep| [dep.name, dep] }.to_h |
| 38 |
end |
| 39 |
|
| 40 |
definition = if gems.empty? && sources.empty? |
| 41 |
# We're doing a full update |
| 42 |
Bundler.definition(true) |
| 43 |
else |
| 44 |
Bundler.definition(gems: gems, sources: sources) |
| 45 |
end |
| 46 |
|
| 47 |
Bundler::CLI::Common.configure_gem_version_promoter( |
| 48 |
Bundler.definition, |
| 49 |
options.merge(strict: @strict) |
| 50 |
) |
| 51 |
|
| 52 |
definition_resolution = proc do |
| 53 |
options[:local] ? definition.resolve_with_cache! : definition.resolve_remotely! |
| 54 |
end |
| 55 |
|
| 56 |
if options[:parseable] |
| 57 |
Bundler.ui.progress(&definition_resolution) |
| 58 |
else |
| 59 |
definition_resolution.call |
| 60 |
end |
| 61 |
|
| 62 |
Bundler.ui.info "" |
| 63 |
|
| 64 |
# Loop through the current specs |
| 65 |
gemfile_specs, dependency_specs = current_specs.partition do |spec| |
| 66 |
current_dependencies.key? spec.name |
| 67 |
end |
| 68 |
|
| 69 |
specs = if options["only-explicit"] |
| 70 |
gemfile_specs |
| 71 |
else |
| 72 |
gemfile_specs + dependency_specs |
| 73 |
end |
| 74 |
|
| 75 |
specs.sort_by(&:name).uniq(&:name).each do |current_spec| |
| 76 |
next unless gems.empty? || gems.include?(current_spec.name) |
| 77 |
|
| 78 |
active_spec = retrieve_active_spec(definition, current_spec) |
| 79 |
next unless active_spec |
| 80 |
|
| 81 |
next unless filter_options_patch.empty? || update_present_via_semver_portions(current_spec, active_spec, options) |
| 82 |
|
| 83 |
gem_outdated = Gem::Version.new(active_spec.version) > Gem::Version.new(current_spec.version) |
| 84 |
next unless gem_outdated || (current_spec.git_version != active_spec.git_version) |
| 85 |
|
| 86 |
dependency = current_dependencies[current_spec.name] |
| 87 |
groups = "" |
| 88 |
if dependency && !options[:parseable] |
| 89 |
groups = dependency.groups.join(", ") |
| 90 |
end |
| 91 |
|
| 92 |
outdated_gems << { |
| 93 |
active_spec: active_spec, |
| 94 |
current_spec: current_spec, |
| 95 |
dependency: dependency, |
| 96 |
groups: groups, |
| 97 |
} |
| 98 |
end |
| 99 |
|
| 100 |
relevant_outdated_gems = if options_include_groups |
| 101 |
outdated_gems.group_by {|g| g[:groups] }.sort.flat_map do |groups, gems| |
| 102 |
contains_group = groups.split(", ").include?(options[:group]) |
| 103 |
next unless options[:groups] || contains_group |
| 104 |
|
| 105 |
gems |
| 106 |
end.compact |
| 107 |
else |
| 108 |
outdated_gems |
| 109 |
end |
| 110 |
|
| 111 |
if relevant_outdated_gems.empty? |
| 112 |
unless options[:parseable] |
| 113 |
Bundler.ui.info(nothing_outdated_message) |
| 114 |
end |
| 115 |
else |
| 116 |
if options[:parseable] |
| 117 |
print_gems(relevant_outdated_gems) |
| 118 |
else |
| 119 |
print_gems_table(relevant_outdated_gems) |
| 120 |
end |
| 121 |
|
| 122 |
exit 1 |
| 123 |
end |
| 124 |
end |
| 125 |
|
| 126 |
private |
| 127 |
|
| 128 |
def loaded_from_for(spec) |
| 129 |
return unless spec.respond_to?(:loaded_from) |
| 130 |
|
| 131 |
spec.loaded_from |
| 132 |
end |
| 133 |
|
| 134 |
def groups_text(group_text, groups) |
| 135 |
"#{group_text}#{groups.split(",").size > 1 ? "s" : ""} \"#{groups}\"" |
| 136 |
end |
| 137 |
|
| 138 |
def nothing_outdated_message |
| 139 |
if filter_options_patch.any? |
| 140 |
display = filter_options_patch.map do |o| |
| 141 |
o.sub("filter-", "") |
| 142 |
end.join(" or ") |
| 143 |
|
| 144 |
"No #{display} updates to display.\n" |
| 145 |
else |
| 146 |
"Bundle up to date!\n" |
| 147 |
end |
| 148 |
end |
| 149 |
|
| 150 |
def retrieve_active_spec(definition, current_spec) |
| 151 |
active_spec = definition.resolve.find_by_name_and_platform(current_spec.name, current_spec.platform) |
| 152 |
return unless active_spec |
| 153 |
|
| 154 |
return active_spec if strict |
| 155 |
|
| 156 |
active_specs = active_spec.source.specs.search(current_spec.name).select {|spec| spec.match_platform(current_spec.platform) }.sort_by(&:version) |
| 157 |
if !current_spec.version.prerelease? && !options[:pre] && active_specs.size > 1 |
| 158 |
active_specs.delete_if {|b| b.respond_to?(:version) && b.version.prerelease? } |
| 159 |
end |
| 160 |
active_specs.last |
| 161 |
end |
| 162 |
|
| 163 |
def print_gems(gems_list) |
| 164 |
gems_list.each do |gem| |
| 165 |
print_gem( |
| 166 |
gem[:current_spec], |
| 167 |
gem[:active_spec], |
| 168 |
gem[:dependency], |
| 169 |
gem[:groups], |
| 170 |
) |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
def print_gems_table(gems_list) |
| 175 |
data = gems_list.map do |gem| |
| 176 |
gem_column_for( |
| 177 |
gem[:current_spec], |
| 178 |
gem[:active_spec], |
| 179 |
gem[:dependency], |
| 180 |
gem[:groups], |
| 181 |
) |
| 182 |
end |
| 183 |
|
| 184 |
print_indented([table_header] + data) |
| 185 |
end |
| 186 |
|
| 187 |
def print_gem(current_spec, active_spec, dependency, groups) |
| 188 |
spec_version = "#{active_spec.version}#{active_spec.git_version}" |
| 189 |
if Bundler.ui.debug? |
| 190 |
loaded_from = loaded_from_for(active_spec) |
| 191 |
spec_version += " (from #{loaded_from})" if loaded_from |
| 192 |
end |
| 193 |
current_version = "#{current_spec.version}#{current_spec.git_version}" |
| 194 |
|
| 195 |
if dependency&.specific? |
| 196 |
dependency_version = %(, requested #{dependency.requirement}) |
| 197 |
end |
| 198 |
|
| 199 |
spec_outdated_info = "#{active_spec.name} (newest #{spec_version}, " \ |
| 200 |
"installed #{current_version}#{dependency_version})" |
| 201 |
|
| 202 |
output_message = if options[:parseable] |
| 203 |
spec_outdated_info.to_s |
| 204 |
elsif options_include_groups || groups.empty? |
| 205 |
" * #{spec_outdated_info}" |
| 206 |
else |
| 207 |
" * #{spec_outdated_info} in #{groups_text("group", groups)}" |
| 208 |
end |
| 209 |
|
| 210 |
Bundler.ui.info output_message.rstrip |
| 211 |
end |
| 212 |
|
| 213 |
def gem_column_for(current_spec, active_spec, dependency, groups) |
| 214 |
current_version = "#{current_spec.version}#{current_spec.git_version}" |
| 215 |
spec_version = "#{active_spec.version}#{active_spec.git_version}" |
| 216 |
dependency = dependency.requirement if dependency |
| 217 |
|
| 218 |
ret_val = [active_spec.name, current_version, spec_version, dependency.to_s, groups.to_s] |
| 219 |
ret_val << loaded_from_for(active_spec).to_s if Bundler.ui.debug? |
| 220 |
ret_val |
| 221 |
end |
| 222 |
|
| 223 |
def check_for_deployment_mode! |
| 224 |
return unless Bundler.frozen_bundle? |
| 225 |
suggested_command = if Bundler.settings.locations("frozen").keys.&([:global, :local]).any? |
| 226 |
"bundle config unset frozen" |
| 227 |
elsif Bundler.settings.locations("deployment").keys.&([:global, :local]).any? |
| 228 |
"bundle config unset deployment" |
| 229 |
end |
| 230 |
raise ProductionError, "You are trying to check outdated gems in " \ |
| 231 |
"deployment mode. Run `bundle outdated` elsewhere.\n" \ |
| 232 |
"\nIf this is a development machine, remove the " \ |
| 233 |
"#{Bundler.default_gemfile} freeze" \ |
| 234 |
"\nby running `#{suggested_command}`." |
| 235 |
end |
| 236 |
|
| 237 |
def update_present_via_semver_portions(current_spec, active_spec, options) |
| 238 |
current_major = current_spec.version.segments.first |
| 239 |
active_major = active_spec.version.segments.first |
| 240 |
|
| 241 |
update_present = false |
| 242 |
update_present = active_major > current_major if options["filter-major"] |
| 243 |
|
| 244 |
if !update_present && (options["filter-minor"] || options["filter-patch"]) && current_major == active_major |
| 245 |
current_minor = get_version_semver_portion_value(current_spec, 1) |
| 246 |
active_minor = get_version_semver_portion_value(active_spec, 1) |
| 247 |
|
| 248 |
update_present = active_minor > current_minor if options["filter-minor"] |
| 249 |
|
| 250 |
if !update_present && options["filter-patch"] && current_minor == active_minor |
| 251 |
current_patch = get_version_semver_portion_value(current_spec, 2) |
| 252 |
active_patch = get_version_semver_portion_value(active_spec, 2) |
| 253 |
|
| 254 |
update_present = active_patch > current_patch |
| 255 |
end |
| 256 |
end |
| 257 |
|
| 258 |
update_present |
| 259 |
end |
| 260 |
|
| 261 |
def get_version_semver_portion_value(spec, version_portion_index) |
| 262 |
version_section = spec.version.segments[version_portion_index, 1] |
| 263 |
version_section.to_a[0].to_i |
| 264 |
end |
| 265 |
|
| 266 |
def print_indented(matrix) |
| 267 |
header = matrix[0] |
| 268 |
data = matrix[1..-1] |
| 269 |
|
| 270 |
column_sizes = Array.new(header.size) do |index| |
| 271 |
matrix.max_by {|row| row[index].length }[index].length |
| 272 |
end |
| 273 |
|
| 274 |
Bundler.ui.info justify(header, column_sizes) |
| 275 |
|
| 276 |
data.sort_by! {|row| row[0] } |
| 277 |
|
| 278 |
data.each do |row| |
| 279 |
Bundler.ui.info justify(row, column_sizes) |
| 280 |
end |
| 281 |
end |
| 282 |
|
| 283 |
def table_header |
| 284 |
header = ["Gem", "Current", "Latest", "Requested", "Groups"] |
| 285 |
header << "Path" if Bundler.ui.debug? |
| 286 |
header |
| 287 |
end |
| 288 |
|
| 289 |
def justify(row, sizes) |
| 290 |
row.each_with_index.map do |element, index| |
| 291 |
element.ljust(sizes[index]) |
| 292 |
end.join(" ").strip + "\n" |
| 293 |
end |
| 294 |
end |
| 295 |
end |