Last active
July 21, 2018 15:52
-
-
Save oisin/fb01914f8c03eeb74b2bfc896bba68ca to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'gems' | |
require 'bundler/setup' | |
require 'json' | |
require 'rubygems' | |
# Find the current version of a gem you are using, then find the latest | |
# version of the gem and put in a reminder that theres been updates. | |
# | |
# gemnasium used to do this but then they got bought and that service is | |
# gone, I think it was bundled into github | |
# | |
# Given the name of a gem, release_versions will return all released | |
# version strings -- it exludes everything that isn't of the form | |
# of dot separated numbers, i.e. no betas or rc1s or that kind of thing | |
# | |
# Goes to the network -- Rubygems only -- so not useful for private | |
# gems, but ye should be in charge of those already. | |
# | |
def release_versions(name) | |
Gems.versions(name).map { |r| r['number'] }.select { |v| v =~ /^\d+.\d+(.\d+)*$/ } | |
end | |
# Scab returns a hash of the latest major versions of the gems in the hash | |
# which looks like { "gemname" : [array of gemspecs]} | |
def scab(gemhash) | |
# This needs a sorted array -- you get it from Gems.versions anyway | |
# so no need to do any sorts here directly, waste of time. | |
# Should probably be able to do filters too, but eh that's not important | |
results = {} | |
gemhash.keys.each do |name| | |
top_versions = release_versions(name).inject([]) do |acc, i| | |
if acc.empty? || i[0] != acc.last[0] | |
acc << i | |
else | |
acc | |
end | |
end | |
results[a] = { vs: top_versions, cv: gemhash[name].first.version } | |
end | |
results | |
end | |
if ARGV.count > 0 | |
# If list of gems on command line, discover versions of them, ignoring ones | |
# that are not installed locally | |
installed_gems = Gem::Specification.group_by(&:name) | |
arg_gems = ARGV.select { |g| installed_gems.key?(g) } | |
filtered_gems = installed_gems.select { |g| arg_gems.include?(g) } | |
STDOUT.puts scab(filtered_gems).to_json | |
else | |
# If no list of gems on command line, ask Bundler what's in use | |
# according to the local Gemfile.lock | |
STDOUT.puts scab(Bundler.load.specs.to_hash).to_json | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment