Skip to content

Commit cc7274f

Browse files
committed
Support table format stdin for Bundler 2.2 or higher
1 parent 9d12f1c commit cc7274f

File tree

2 files changed

+126
-3
lines changed

2 files changed

+126
-3
lines changed

lib/bundle_outdated_formatter/formatter.rb

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Formatter
88
INSTALLED_REGEXP = /installed (?<installed>[\d.]+)/.freeze
99
REQUESTED_REGEXP = /requested (?<requested>.+)\)/.freeze
1010
GROUPS_REGEXP = /in groups? "(?<groups>.+)"/.freeze
11+
TABLE_FORMAT_REGEXP = /Gem +Current +Latest +Requested +Groups/.freeze
1112

1213
def initialize(options)
1314
@pretty = options[:pretty]
@@ -19,10 +20,17 @@ def initialize(options)
1920
def read_stdin
2021
@outdated_gems = STDIN.each.to_a.map(&:strip).reject(&:empty?)
2122

22-
@outdated_gems.map! do |line|
23-
find_gem(line)
23+
if (header_index = find_table_header_index(@outdated_gems))
24+
header = @outdated_gems[header_index]
25+
pos = table_pos(header)
26+
@outdated_gems.map!.with_index do |line, index|
27+
find_gem_for_table_format(line, pos) if header_index < index
28+
end
29+
else
30+
@outdated_gems.map! do |line|
31+
find_gem(line)
32+
end
2433
end
25-
2634
@outdated_gems.compact!
2735
end
2836

@@ -57,6 +65,33 @@ def gem_text(text, name)
5765
text ? text[name] : ''
5866
end
5967

68+
def find_table_header_index(lines)
69+
lines.find_index { |line| line.match?(TABLE_FORMAT_REGEXP) }
70+
end
71+
72+
def table_pos(header)
73+
current_pos = header.index('Current')
74+
latest_pos = header.index('Latest')
75+
requested_pos = header.index('Requested')
76+
groups_pos = header.index('Groups')
77+
{
78+
'gem' => 0..current_pos.pred,
79+
'newest' => latest_pos..requested_pos.pred,
80+
'installed' => current_pos..latest_pos.pred,
81+
'requested' => requested_pos..groups_pos.pred,
82+
'groups' => groups_pos..-1
83+
}
84+
end
85+
86+
def find_gem_for_table_format(line, pos)
87+
gems = {}
88+
@columns.each do |column|
89+
range = pos[column]
90+
gems[column] = line[range].to_s.strip
91+
end
92+
gems
93+
end
94+
6095
def xml_formatter
6196
return REXML::Formatters::Default.new unless @pretty
6297

spec/bundle_outdated_formatter/cli_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
EOS
1515
end
1616

17+
let(:stdin_with_table_format) do
18+
<<-EOS
19+
Fetching gem metadata from https://rubygems.org/..........
20+
Resolving dependencies....
21+
22+
Gem Current Latest Requested Groups
23+
faker 1.6.5 1.6.6 ~> 1.4 development, test
24+
hashie 1.2.0 3.4.6 = 1.2.0 default
25+
headless 2.2.3 2.3.1
26+
EOS
27+
end
28+
1729
let(:stdin_without_outdated) do
1830
<<-EOS
1931
Fetching gem metadata from https://rubygems.org/..........
@@ -430,6 +442,10 @@
430442
it { expect { command }.to output(help).to_stdout }
431443
end
432444

445+
before do
446+
allow_any_instance_of(StringIO).to receive(:ioctl).and_return(-1)
447+
end
448+
433449
describe '.start' do
434450
subject(:command) { described_class.start(thor_args) }
435451

@@ -453,6 +469,12 @@
453469

454470
it_behaves_like 'terminal format'
455471

472+
context 'with table format' do
473+
let(:stdin) { stdin_with_table_format }
474+
475+
it_behaves_like 'terminal format'
476+
end
477+
456478
context 'without outdated' do
457479
let(:stdin) { stdin_without_outdated }
458480
let(:stdout_terminal_unicode) { stdout_terminal_unicode_without_outdated }
@@ -504,6 +526,12 @@
504526

505527
it_behaves_like 'terminal format', style: :ascii
506528

529+
context 'with table format' do
530+
let(:stdin) { stdin_with_table_format }
531+
532+
it_behaves_like 'terminal format', style: :ascii
533+
end
534+
507535
context 'without outdated' do
508536
let(:stdin) { stdin_without_outdated }
509537
let(:stdout_terminal_ascii) { stdout_terminal_ascii_without_outdated }
@@ -535,6 +563,12 @@
535563

536564
it_behaves_like 'markdown format'
537565

566+
context 'with table format' do
567+
let(:stdin) { stdin_with_table_format }
568+
569+
it_behaves_like 'markdown format'
570+
end
571+
538572
context 'without outdated' do
539573
let(:stdin) { stdin_without_outdated }
540574
let(:stdout_markdown) { stdout_markdown_without_outdated }
@@ -610,6 +644,12 @@
610644

611645
it_behaves_like 'json format'
612646

647+
context 'with table format' do
648+
let(:stdin) { stdin_with_table_format }
649+
650+
it_behaves_like 'json format'
651+
end
652+
613653
context 'without outdated' do
614654
let(:stdin) { stdin_without_outdated }
615655
let(:stdout_json) { stdout_json_without_outdated }
@@ -637,6 +677,12 @@
637677

638678
it_behaves_like 'json format', pretty: true
639679

680+
context 'with table format' do
681+
let(:stdin) { stdin_with_table_format }
682+
683+
it_behaves_like 'json format', pretty: true
684+
end
685+
640686
context 'without outdated' do
641687
let(:stdin) { stdin_without_outdated }
642688
let(:stdout_json_pretty) { stdout_json_pretty_without_outdated }
@@ -692,6 +738,12 @@
692738

693739
it_behaves_like 'yaml format'
694740

741+
context 'with table format' do
742+
let(:stdin) { stdin_with_table_format }
743+
744+
it_behaves_like 'yaml format'
745+
end
746+
695747
context 'without outdated' do
696748
let(:stdin) { stdin_without_outdated }
697749
let(:stdout_yaml) { stdout_yaml_without_outdated }
@@ -767,6 +819,12 @@
767819

768820
it_behaves_like 'csv format'
769821

822+
context 'with table format' do
823+
let(:stdin) { stdin_with_table_format }
824+
825+
it_behaves_like 'csv format'
826+
end
827+
770828
context 'without outdated' do
771829
let(:stdin) { stdin_without_outdated }
772830
let(:stdout_csv) { stdout_csv_without_outdated }
@@ -842,6 +900,12 @@
842900

843901
it_behaves_like 'tsv format'
844902

903+
context 'with table format' do
904+
let(:stdin) { stdin_with_table_format }
905+
906+
it_behaves_like 'tsv format'
907+
end
908+
845909
context 'without outdated' do
846910
let(:stdin) { stdin_without_outdated }
847911
let(:stdout_tsv) { stdout_tsv_without_outdated }
@@ -917,6 +981,12 @@
917981

918982
it_behaves_like 'xml format'
919983

984+
context 'with table format' do
985+
let(:stdin) { stdin_with_table_format }
986+
987+
it_behaves_like 'xml format'
988+
end
989+
920990
context 'without outdated' do
921991
let(:stdin) { stdin_without_outdated }
922992
let(:stdout_xml) { stdout_xml_without_outdated }
@@ -944,6 +1014,12 @@
9441014

9451015
it_behaves_like 'xml format', pretty: true
9461016

1017+
context 'with table format' do
1018+
let(:stdin) { stdin_with_table_format }
1019+
1020+
it_behaves_like 'xml format', pretty: true
1021+
end
1022+
9471023
context 'without outdated' do
9481024
let(:stdin) { stdin_without_outdated }
9491025
let(:stdout_xml_pretty) { stdout_xml_pretty_without_outdated }
@@ -999,6 +1075,12 @@
9991075

10001076
it_behaves_like 'html format'
10011077

1078+
context 'with table format' do
1079+
let(:stdin) { stdin_with_table_format }
1080+
1081+
it_behaves_like 'html format'
1082+
end
1083+
10021084
context 'without outdated' do
10031085
let(:stdin) { stdin_without_outdated }
10041086
let(:stdout_html) { stdout_html_without_outdated }
@@ -1026,6 +1108,12 @@
10261108

10271109
it_behaves_like 'html format', pretty: true
10281110

1111+
context 'with table format' do
1112+
let(:stdin) { stdin_with_table_format }
1113+
1114+
it_behaves_like 'html format', pretty: true
1115+
end
1116+
10291117
context 'without outdated' do
10301118
let(:stdin) { stdin_without_outdated }
10311119
let(:stdout_html_pretty) { stdout_html_pretty_without_outdated }

0 commit comments

Comments
 (0)