Skip to content

Instantly share code, notes, and snippets.

@Eric-Guo
Forked from borama/ruby_gems_age.rb
Created August 18, 2019 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eric-Guo/b1c9992bb04a75115db4677b747a222b to your computer and use it in GitHub Desktop.
Save Eric-Guo/b1c9992bb04a75115db4677b747a222b to your computer and use it in GitHub Desktop.
A script that collects and prints info about the age of all ruby gems in your project, see https://dev.to/borama/how-old-are-dependencies-in-your-ruby-project-3jia
#!/bin/env ruby
#
# Collects info about the age of all gems in the project
#
require "json"
require "date"
bundle = `bundle list`
yearly_stats = {}
bundle.split(/\n/).each do |line|
next unless line =~ /\s+\*\s+[^ ]+\s+\(.*\)/
gem_name, gem_version = /\s+\*\s+([^ ]+)\s+\((.*)\)/.match(line).to_a[1..-1]
begin
gem_json = `curl -s https://rubygems.org/api/v2/rubygems/#{gem_name}/versions/#{gem_version}.json`
built_at = JSON.parse(gem_json)["built_at"]
release_date = Date.parse(built_at).to_date
puts "#{gem_name},#{gem_version},#{release_date.strftime("%Y-%m-%d")}"
release_year = release_date.strftime("%Y")
yearly_stats[release_year] ||= { count: 0, gems: [] }
yearly_stats[release_year][:count] += 1
yearly_stats[release_year][:gems] << gem_name
rescue => e
puts "#{gem_name},#{gem_version},???"
end
end
puts
puts "Yearly summary:"
yearly_stats.sort.each do |year, data|
puts "#{year},#{data[:count]},#{data[:gems].join(' ')}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment