Skip to content

Instantly share code, notes, and snippets.

@thbar
Created November 27, 2011 11:03
Show Gist options
  • Save thbar/1397408 to your computer and use it in GitHub Desktop.
Save thbar/1397408 to your computer and use it in GitHub Desktop.
A quick and crappy script to extract gems versions from Gemfile.lock, back into Gemfile
# utterly crappy proof-of-concept that works. for me. today.
task :freeze do
require 'bundler_auto_update'
include Bundler::AutoUpdate
locked_gems = IO.read('Gemfile.lock').match(/^GEM.*^PLATFORMS$/m).to_s.split("\n").grep(/^\s{4}\w/).map(&:strip)
locked_gems = locked_gems.inject({}) { |result, gem| result.merge!(Hash[[gem.gsub(/\(|\)/, '').split]]); result}
gemfile = Gemfile.new
gemfile.gems.each do |g|
updater = GemUpdater.new(g, gemfile, nil)
if (!updater.send(:updatable?)) && (!locked_gems[g.name].blank?)
puts "Freezing #{g.name} at #{locked_gems[g.name]}"
new_content = ""
IO.read('Gemfile').each_line do |line|
if line =~ /^\s*gem\s*['"]#{g.name}['"]\s*$/
line.gsub!("\n", ", \"#{locked_gems[g.name]}\"\n")
end
new_content += line
end
File.open('Gemfile', 'w') { |f| f << new_content }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment