Last active
December 19, 2023 14:21
-
-
Save garrettdimon/6fe9f3da16b1a4f0e6f89aa0884e9ac6 to your computer and use it in GitHub Desktop.
A rake task to audit gems against latest versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Instructions: | |
# | |
# 1. Make sure to add the `gems` gem to your Gemfile so this can work: | |
# https://rubygems.org/gems/gems | |
# | |
# 2. Register at RubyGems.org and add your API key to ~/.gem/credentials | |
# and chmod the credentials file to 0600 | |
# | |
# 3. Use this content to create `lib/tasks/gemfile_audit.rake` | |
# | |
# 4. Run `bundle exec rake gemfile:audit` | |
# | |
# 5. It will create `gems_data.csv` in the application root. Open it and | |
# go wild organizing your new spreadsheet. | |
# | |
desc 'Run through the gemfile and build a spreadsheet of knowledge of the gems' | |
namespace :gemfile do | |
task :audit => :environment do | |
require 'csv' | |
gems = [] | |
# Let's get the gemfile lines in an array | |
File.open('Gemfile').each do |line| | |
gems << line.strip.gsub(/gem\s/, '') if line =~ /gem\s/ | |
end | |
gems_data = [] | |
# Now let's parse all of the gems and get the relevant meta data | |
gems.each do |line| | |
clean_line = line.delete("'~>=,#\"").strip | |
bits = clean_line.split(' ') | |
gem_name = bits[0] | |
gem_version = bits[1] | |
puts "Fetching #{gem_name}..." | |
begin | |
# Let's fetch the info on the current versions of the gem... | |
latest_version = Gems.versions(gem_name).first | |
info = Gems.info(gem_name) | |
created_at = Date.parse(latest_version["created_at"]) | |
age = ((Date.current - created_at).to_f/365).round(2) | |
values = { | |
name: gem_name, | |
current_version: gem_version, | |
latest_version: latest_version["number"], | |
years_old: age, | |
created_at: created_at.strftime('%b %d, %Y'), | |
latest_prerelease: latest_version["prerelease"], | |
downloads: info["downloads"], | |
summary: latest_version["summary"], | |
string: line | |
} | |
rescue | |
puts " #{gem_name} not found." | |
values = { | |
name: gem_name, | |
current_version: gem_version, | |
latest_version: '', | |
years_old: '', | |
created_at: '', | |
latest_prerelease: '', | |
downloads: '', | |
summary: '', | |
string: line | |
} | |
end | |
gems_data << values | |
end | |
gems_data.compact! | |
CSV.open("gems_data.csv", "wb") do |csv| | |
csv << gems_data.first.keys | |
gems_data.each do |gem| | |
csv << gem.values | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment