Created
September 11, 2014 21:08
-
-
Save Dagnan/9bee80c203db6e2f3e4d to your computer and use it in GitHub Desktop.
parse Gemfile
This file contains hidden or 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
require 'active_support/core_ext/object/try.rb' | |
trap("INT") { puts "Exiting..."; exit } | |
FILENAME = ARGV[0] | |
if FILENAME.nil? || FILENAME.empty? | |
puts 'Usage:' | |
puts 'ruby parse_gemfile.rb path-to-your-Gemfile' | |
exit | |
end | |
file = | |
begin | |
File.open(FILENAME) | |
rescue Errno::ENOENT => e | |
puts "Could not open file #{FILENAME}" ; exit | |
end | |
file.each_line do |l| | |
gem = l.match(/^(\s*)gem\s?['"]([\-a-zA-z]+)['"](,\s*['"](.*[-\.0-9a-zA-Z]+)['"])?(.*)/) | |
# Puts the line if it does not declare a gem | |
(puts l ; next) unless gem | |
# Skips gems with already a version specified | |
actual_version = gem[4] | |
(puts l ; next) if actual_version | |
spaces, gem_name, end_of_line = gem[1], gem[2], gem[5] | |
output = `bundle show #{gem_name}` | |
version = output.match(/.*-([0-9\.]+)/).try(:[], 1) or nil | |
if version | |
puts %|#{spaces}gem '#{gem_name}', '~> #{version}'#{end_of_line}| | |
else | |
puts l | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment