Skip to content

Instantly share code, notes, and snippets.

@calorie
Created February 9, 2014 20:31
Show Gist options
  • Save calorie/8905537 to your computer and use it in GitHub Desktop.
Save calorie/8905537 to your computer and use it in GitHub Desktop.
require 'fileutils'
GEMFILE_TMP = 'Gemfile_tmp'
FileUtils.cp('Gemfile', GEMFILE_TMP)
regex = /^\s*gem ['"]([-\w]+)['"](,.*)?/
gems = File.read(GEMFILE_TMP).scan(regex).map do |g|
g.first if g.last.nil?
end
gems.compact!
h = {}
`bundle exec gem list`.scan(/(\w+(\-\w+)*)\s+\((\d+\.\d+\.\d+(\.\d+)*)\)/m).each do |g|
g.compact!
h[g.first] = g.last if gems.include?(g.first)
end
File.open('Gemfile', 'w') {}
File.open(GEMFILE_TMP) do |tmp|
tmp.each do |l|
File.open('Gemfile', 'a') do |gemfile|
name = l.scan(regex).flatten.first
if !name.nil? && !h[name].nil?
l.chomp! << ", '#{h[name]}'\n"
end
gemfile.write(l)
end
end
end
FileUtils.rm_f(GEMFILE_TMP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment