Skip to content

Instantly share code, notes, and snippets.

@mberlanda
Created October 5, 2017 16:06
Show Gist options
  • Save mberlanda/54df3151292b9f0bd2a7b9bb00be8452 to your computer and use it in GitHub Desktop.
Save mberlanda/54df3151292b9f0bd2a7b9bb00be8452 to your computer and use it in GitHub Desktop.
Run it as `$ ruby gemfile_parser.rb > Gemfile ` to generate a new Gemfile from a Gemfile.lock
require 'bundler'
lockfile = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))
specs = lockfile.specs
gems_hash = Hash.new.tap do |h|
specs.each do |s|
h[s.name] = {
spec: s,
dependencies: s.dependencies.map(&:name)
}
end
end
dependencies = gems_hash.keys && gems_hash.values.map { |h| h[:dependencies] }.flatten.uniq.sort
dependencies.each { |dep| gems_hash.delete(dep) }
relevant_specs = gems_hash.values.map { |h| h[:spec] }
puts "source 'https://rubygems.org'"
puts
relevant_specs.each do |s|
if s.source.to_s =~ /https:\/\/rubygems.org/
puts "gem '#{s.name}', '#{s.version}'"
elsif s.source.is_a?(Bundler::Source::Git)
uri = s.source.uri
branch = s.source.branch
ref = s.source.ref
puts
puts "git '#{uri}', branch: '#{branch}', ref: :#{ref} do"
puts " gem '#{s.name}'"
puts "end"
puts
end
end
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment