Skip to content

Commit 05248da

Browse files
Merge pull request #2422 from coreinfrastructure/print_percent_outdated
Add rake task percent_gems_up_to_date
2 parents 6d7c3c9 + b69076b commit 05248da

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

lib/tasks/default.rake

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ task(:default).clear.enhance %w[
3737
html_from_markdown
3838
eslint
3939
report_code_statistics
40+
percent_gems_up_to_date
4041
test:all
4142
]
4243
# Temporarily removed fasterer
@@ -124,17 +125,17 @@ task :report_code_statistics do
124125
end
125126
end
126127

128+
# Helper function to check network availability
129+
def network_available?
130+
require 'socket'
131+
Socket.tcp('github.com', 443, connect_timeout: 5) { true }
132+
rescue StandardError
133+
false
134+
end
135+
127136
# rubocop:disable Metrics/BlockLength
128137
desc 'Run bundle-audit - check for known vulnerabilities in dependencies'
129138
task :bundle_audit do
130-
require 'socket'
131-
132-
def network_available?
133-
Socket.tcp('github.com', 443, connect_timeout: 5) { true }
134-
rescue StandardError
135-
false
136-
end
137-
138139
# rubocop:disable Metrics/MethodLength
139140
def bundle_audit_update_successful?
140141
max_retries = 4
@@ -186,6 +187,51 @@ task :bundle_audit do
186187
end
187188
# rubocop:enable Metrics/BlockLength
188189

190+
# We don't normally print a list of out-of-date gems, that would create
191+
# a lot of output that is normally useless. The developer can always run
192+
# "bundle outdated" if that's important.
193+
desc 'Report percentage of gems that are up-to-date if network available'
194+
task :percent_gems_up_to_date do
195+
if network_available?
196+
begin
197+
bundle_list_output = `bundle list 2>/dev/null`
198+
if $CHILD_STATUS.success?
199+
total_gems =
200+
bundle_list_output.lines.count do |line|
201+
line.match?(/^\s+\*\s/)
202+
end
203+
outdated_output = `bundle outdated 2>/dev/null`
204+
outdated_count =
205+
$CHILD_STATUS.success? ? 0 : count_outdated_gems(outdated_output)
206+
up_to_date = total_gems - outdated_count
207+
percent =
208+
if total_gems.zero?
209+
100.0
210+
else
211+
(up_to_date.to_f / total_gems * 100).round(1)
212+
end
213+
214+
puts "Gem stats: out-of-date=#{outdated_count}, " \
215+
"up-to-date=#{up_to_date}, total=#{total_gems}, " \
216+
"%up-to-date=#{percent}%"
217+
else
218+
puts 'Unable to determine total gem count'
219+
end
220+
rescue StandardError => e
221+
puts "Error checking gem status: #{e.message}"
222+
end
223+
else
224+
puts 'Network not available, skipping gem status check'
225+
end
226+
end
227+
228+
# Helper function to count outdated gems from bundle outdated output
229+
def count_outdated_gems(outdated_output)
230+
lines = outdated_output.lines
231+
gem_header_index = lines.find_index { |line| line.start_with?('Gem') }
232+
gem_header_index ? lines.length - gem_header_index - 1 : 0
233+
end
234+
189235
# Use markdownlint to examine the usual markdown files.
190236
# NOTE: If you don't want mdl to be run on a markdown file, rename it to
191237
# end in ".markdown" instead. (E.g., for markdown fragments.)

0 commit comments

Comments
 (0)