Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'gems'
  3. require 'bundler/setup'
  4. require 'json'
  5. require 'rubygems'
  6.  
  7. # Find the current version of a gem you are using, then find the latest version of the
  8. # gem and put in a reminder that theres been updates.
  9. #
  10. # gemnasium used to do this but then they got bought and that service is gone, I think
  11. # it was bundled into github
  12. #
  13.  
  14. # Scab returns a hash of the latest major versions of the gems in the hash
  15. # which looks like { "gemname" : [array of gemspecs]}
  16. def scab(gemhash)
  17. # This needs a sorted array -- you get it from Gems.versions anyway
  18. # so no need to do any sorts here directly, waste of time.
  19.  
  20. # Should probably be able to do filters too, but eh that's not important
  21. results = {}
  22. gemhash.keys.each {|a|
  23. all_rails = Gems.versions(a).map{|r| r['number'] }
  24. release_versions = all_rails.select{|v| v =~ /^\d+.\d+(.\d+)*$/ }
  25. latest_versions = release_versions.inject([]) { |acc, i|
  26. if (acc.empty? || i[0] != acc.last[0])
  27. acc << i
  28. else
  29. acc
  30. end
  31. }
  32. results[a] = { vs: latest_versions, cv: gemhash[a].first.version}
  33. }
  34. results
  35. end
  36.  
  37.  
  38. if (ARGV.count > 0)
  39. # If list of gems on command line, discover versions of them, ignoring ones
  40. # that are not installed locally.
  41. # Yes, you could collapse all this into one line but let's be able to read it, ok?
  42. installed_gems = Gem::Specification.group_by{|g| g.name}
  43. arg_gems = ARGV.select{|g| installed_gems.keys.include?(g)}
  44. filtered_gems = installed_gems.select{|g| arg_gems.include?(g)}
  45. STDOUT.puts scab(filtered_gems).to_json
  46. else
  47. # If no list of gems on command line, ask Bundler what's in use
  48. # according to the local Gemfile.lock
  49. STDOUT.puts scab(Bundler.load.specs.to_hash).to_json
  50. end
Add Comment
Please, Sign In to add comment