Skip to content

Instantly share code, notes, and snippets.

@dougdroper
Last active December 11, 2015 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougdroper/4566388 to your computer and use it in GitHub Desktop.
Save dougdroper/4566388 to your computer and use it in GitHub Desktop.
Update dependencies of engines

Update Dependencies Micro Gem

Updates a Gemfile of a dependent library from the gemfile.lock of an independent project.

Prints a new Gemfile with the gem versions matched to the independent gemfile.lock

  $ update_dependencies path_to_dependent_gemfile
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
require './update_dependencies'
UpdateDependencies.new(ARGV[0]).run_dependencies
Gem::Specification.new do |s|
s.name = "update_dependencies"
s.version = "0.1.0"
s.authors = "dougdroper"
s.email = ["dougdroper@gmail.com"]
s.homepage = "https://gist.github.com/4566388"
s.summary = "Updates dependencies of gemfile from gemfile.lock"
s.description = "Updates dependencies of gemfile from gemfile.lock"
s.executables = ["update_dependencies"]
s.files = ["update_dependencies.rb"]
s.require_path = "."
end
module Bundler
class Dsl
def gemspec
puts "gemspec \n\n"
end
end
end
class UpdateDependencies
attr_reader :dependency_gemfile
def initialize(path_to_gemfile)
raise "Needs a location of a dependent Gemfile" unless path_to_gemfile
puts "source 'http://rubygems.org' \n\n"
@dependency_gemfile = Bundler::Dsl.new.eval_gemfile(path_to_gemfile)
end
def run_dependencies
dependency_gemfile.group_by {|g| g.groups.first }.each do |group, gem_sources|
in_group(group.to_s) do
gem_sources.each do |gem_source|
print_dependent_sources(gem_source, group)
print_version_sources(gem_source, group)
end
end
end
end
private
def spaces(group)
group.to_s == "default" ? "" : " "
end
def ref(source)
source.ref == "master" ? "" : ", ref => '" + source.ref + "'"
end
def in_group(group)
puts "\ngroup :" + group + " do" unless group == "default"
yield
puts "end" unless group == "default"
end
def locked_gemfile
@locked ||= Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))
end
def print_dependent_sources(gem_source, group)
locked_gemfile.sources.select {|s| gem_source.name == s.name}.
map {|s| puts spaces(group) + "gem '" + s.name + "', :git => '" + s.uri + "'" + ref(s)}
end
def print_version_sources(gem_source, group)
locked_gemfile.specs.reject {|s| locked_gemfile.sources.map(&:name).include?(s.name)}.
select {|s| s.name == gem_source.name}.
map {|s| puts spaces(group) + "gem '" + s.name + "', '" + s.version.version + "'"}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment