Skip to content

Commit

Permalink
[rubygems/rubygems] Add --json bundle-outdated flag to produce json-p…
Browse files Browse the repository at this point in the history
…arseable output

rubygems/rubygems@65efa44bc0
  • Loading branch information
nevinera authored and matzbot committed Nov 23, 2023
1 parent a54c98a commit c424d15
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/bundler/cli.rb
Expand Up @@ -420,6 +420,7 @@ def add(*gems)
method_option "filter-patch", :type => :boolean, :banner => "Only list patch newer versions"
method_option "parseable", :aliases => "--porcelain", :type => :boolean, :banner =>
"Use minimal formatting for more parseable output"
method_option "json", :type => :boolean, :banner => "Produce parseable json output"
method_option "only-explicit", :type => :boolean, :banner =>
"Only list gems specified in your Gemfile, not their dependencies"
def outdated(*gems)
Expand Down
48 changes: 44 additions & 4 deletions lib/bundler/cli/outdated.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "json"

module Bundler
class CLI::Outdated
attr_reader :options, :gems, :options_include_groups, :filter_options_patch, :sources, :strict
Expand Down Expand Up @@ -53,13 +55,13 @@ def run
options[:local] ? definition.resolve_with_cache! : definition.resolve_remotely!
end

if options[:parseable]
if options[:parseable] || options[:json]
Bundler.ui.silence(&definition_resolution)
else
definition_resolution.call
end

Bundler.ui.info ""
Bundler.ui.info "" unless options[:json]

# Loop through the current specs
gemfile_specs, dependency_specs = current_specs.partition do |spec|
Expand Down Expand Up @@ -98,7 +100,9 @@ def run
end

if outdated_gems.empty?
unless options[:parseable]
if options[:json]
print_gems_json([])
elsif !options[:parseable]
Bundler.ui.info(nothing_outdated_message)
end
else
Expand All @@ -108,7 +112,9 @@ def run
outdated_gems
end

if options[:parseable]
if options[:json]
print_gems_json(relevant_outdated_gems)
elsif options[:parseable]
print_gems(relevant_outdated_gems)
else
print_gems_table(relevant_outdated_gems)
Expand Down Expand Up @@ -173,6 +179,20 @@ def print_gems(gems_list)
end
end

def print_gems_json(gems_list)
data = gems_list.map do |gem|
gem_data_for(
gem[:current_spec],
gem[:active_spec],
gem[:dependency],
gem[:groups]
)
end

data = { :outdated_count => gems_list.count, :outdated_gems => data }
Bundler.ui.info data.to_json
end

def print_gems_table(gems_list)
data = gems_list.map do |gem|
gem_column_for(
Expand Down Expand Up @@ -212,6 +232,26 @@ def print_gem(current_spec, active_spec, dependency, groups)
Bundler.ui.info output_message.rstrip
end

def gem_data_for(current_spec, active_spec, dependency, groups)
{
:current_spec => spec_data_for(current_spec),
:active_spec => spec_data_for(active_spec),
:dependency => dependency&.to_s,
:groups => (groups || "").split(", "),
}
end

def spec_data_for(spec)
{
:name => spec.name,
:version => spec.version.to_s,
:platform => spec.platform,
:source => spec.source.to_s,
:required_ruby_version => spec.required_ruby_version.to_s,
:required_rubygems_version => spec.required_rubygems_version.to_s,
}
end

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}"
Expand Down
21 changes: 21 additions & 0 deletions spec/bundler/bundler/cli_spec.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "bundler/cli"
require "json"

RSpec.describe "bundle executable" do
it "returns non-zero exit status when passed unrecognized options" do
Expand Down Expand Up @@ -154,6 +155,26 @@ def out_with_macos_man_workaround
end
end

context "with --json" do
let(:flags) { "--json" }

it "prints json output data when there are outdated gems" do
run_command
out_data = JSON.parse(out)
expect(out_data.keys).to contain_exactly("outdated_count", "outdated_gems")
expect(out_data["outdated_count"]).to eq(1)
expect(out_data["outdated_gems"].length).to eq(1)

gem_data = out_data["outdated_gems"].first
expect(gem_data).to include({
"current_spec" => hash_including("name" => "rack", "version" => "0.9.1"),
"active_spec" => hash_including("name" => "rack", "version" => "1.0.0"),
"dependency" => "rack (= 0.9.1)",
"groups" => ["default"],
})
end
end

context "with --parseable" do
let(:flags) { "--parseable" }

Expand Down

0 comments on commit c424d15

Please sign in to comment.