From fca65df1afdfaf813ce74543a6c068afadd9393a Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Wed, 7 Jul 2021 11:49:15 +0900 Subject: [PATCH] Support table format stdin for Bundler 2.2 or higher --- lib/bundle_outdated_formatter/formatter.rb | 41 ++++++++++- spec/bundle_outdated_formatter/cli_spec.rb | 84 ++++++++++++++++++++++ 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/lib/bundle_outdated_formatter/formatter.rb b/lib/bundle_outdated_formatter/formatter.rb index cb97061..a45194b 100644 --- a/lib/bundle_outdated_formatter/formatter.rb +++ b/lib/bundle_outdated_formatter/formatter.rb @@ -8,6 +8,7 @@ class Formatter INSTALLED_REGEXP = /installed (?[\d.]+)/.freeze REQUESTED_REGEXP = /requested (?.+)\)/.freeze GROUPS_REGEXP = /in groups? "(?.+)"/.freeze + TABLE_FORMAT_REGEXP = /Gem +Current +Latest +Requested +Groups/.freeze def initialize(options) @pretty = options[:pretty] @@ -19,10 +20,17 @@ def initialize(options) def read_stdin @outdated_gems = STDIN.each.to_a.map(&:strip).reject(&:empty?) - @outdated_gems.map! do |line| - find_gem(line) + if (header_index = find_table_header_index(@outdated_gems)) + header = @outdated_gems[header_index] + pos = table_pos(header) + @outdated_gems.map!.with_index do |line, index| + find_gem_for_table_format(line, pos) if header_index < index + end + else + @outdated_gems.map! do |line| + find_gem(line) + end end - @outdated_gems.compact! end @@ -57,6 +65,33 @@ def gem_text(text, name) text ? text[name] : '' end + def find_table_header_index(lines) + lines.find_index { |line| line =~ TABLE_FORMAT_REGEXP } + end + + def table_pos(header) + current_pos = header.index('Current') + latest_pos = header.index('Latest') + requested_pos = header.index('Requested') + groups_pos = header.index('Groups') + { + 'gem' => 0..current_pos.pred, + 'newest' => latest_pos..requested_pos.pred, + 'installed' => current_pos..latest_pos.pred, + 'requested' => requested_pos..groups_pos.pred, + 'groups' => groups_pos..-1 + } + end + + def find_gem_for_table_format(line, pos) + gems = {} + @columns.each do |column| + range = pos[column] + gems[column] = line[range].to_s.strip + end + gems + end + def xml_formatter return REXML::Formatters::Default.new unless @pretty diff --git a/spec/bundle_outdated_formatter/cli_spec.rb b/spec/bundle_outdated_formatter/cli_spec.rb index 75a2d35..7c7759c 100644 --- a/spec/bundle_outdated_formatter/cli_spec.rb +++ b/spec/bundle_outdated_formatter/cli_spec.rb @@ -14,6 +14,18 @@ EOS end + let(:stdin_with_table_format) do + <<-EOS +Fetching gem metadata from https://rubygems.org/.......... +Resolving dependencies.... + +Gem Current Latest Requested Groups +faker 1.6.5 1.6.6 ~> 1.4 development, test +hashie 1.2.0 3.4.6 = 1.2.0 default +headless 2.2.3 2.3.1 + EOS + end + let(:stdin_without_outdated) do <<-EOS Fetching gem metadata from https://rubygems.org/.......... @@ -453,6 +465,12 @@ it_behaves_like 'terminal format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'terminal format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_terminal_unicode) { stdout_terminal_unicode_without_outdated } @@ -504,6 +522,12 @@ it_behaves_like 'terminal format', style: :ascii + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'terminal format', style: :ascii + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_terminal_ascii) { stdout_terminal_ascii_without_outdated } @@ -535,6 +559,12 @@ it_behaves_like 'markdown format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'markdown format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_markdown) { stdout_markdown_without_outdated } @@ -610,6 +640,12 @@ it_behaves_like 'json format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'json format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_json) { stdout_json_without_outdated } @@ -637,6 +673,12 @@ it_behaves_like 'json format', pretty: true + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'json format', pretty: true + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_json_pretty) { stdout_json_pretty_without_outdated } @@ -692,6 +734,12 @@ it_behaves_like 'yaml format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'yaml format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_yaml) { stdout_yaml_without_outdated } @@ -767,6 +815,12 @@ it_behaves_like 'csv format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'csv format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_csv) { stdout_csv_without_outdated } @@ -842,6 +896,12 @@ it_behaves_like 'tsv format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'tsv format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_tsv) { stdout_tsv_without_outdated } @@ -917,6 +977,12 @@ it_behaves_like 'xml format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'xml format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_xml) { stdout_xml_without_outdated } @@ -944,6 +1010,12 @@ it_behaves_like 'xml format', pretty: true + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'xml format', pretty: true + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_xml_pretty) { stdout_xml_pretty_without_outdated } @@ -999,6 +1071,12 @@ it_behaves_like 'html format' + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'html format' + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_html) { stdout_html_without_outdated } @@ -1026,6 +1104,12 @@ it_behaves_like 'html format', pretty: true + context 'with table format' do + let(:stdin) { stdin_with_table_format } + + it_behaves_like 'html format', pretty: true + end + context 'without outdated' do let(:stdin) { stdin_without_outdated } let(:stdout_html_pretty) { stdout_html_pretty_without_outdated }