lib/schema_dev/gemfiles.rb in schema_dev-1.2.5 vs lib/schema_dev/gemfiles.rb in schema_dev-1.3.0

- old
+ new

@@ -6,41 +6,50 @@ extend self TEMPLATES_ROOT = Pathname.new(__FILE__).dirname.parent.parent + "templates" def build(config) - @src_root = TEMPLATES_ROOT - @dst_root = Pathname.new('.') + Dir.mktmpdir do |tmpdir| + @src_root = TEMPLATES_ROOT + @dst_root = Pathname.new(tmpdir).realpath - path = Pathname.new("gemfiles") + relpath = Pathname.new("gemfiles") + abspath = @dst_root + relpath + target_abspath = Pathname.new(".").realpath + relpath - _blow_away(path) + _copy(relpath, 'Gemfile.base') - _copy(path, 'Gemfile.base') + config.rails.each do |rails| - config.rails.each do |rails| + rails_path = relpath + "rails-#{rails}" + _copy(rails_path, 'Gemfile.base') - rails_path = path + "rails-#{rails}" - _copy(rails_path, 'Gemfile.base') + config.db.each do |db| + _copy(rails_path, "Gemfile.#{db}") + end + end - config.db.each do |db| - _copy(rails_path, "Gemfile.#{db}") + if `diff -rq #{abspath} gemfiles 2>&1 | grep -v lock`.length == 0 + return false end + + _blow_away(target_abspath) + abspath.rename(target_abspath) + return true end - return true end - def _copy(path, filename) - srcfile = @src_root + path + filename - dstfile = @dst_root + path + filename + def _copy(relpath, filename) + srcfile = @src_root + relpath + filename + dstfile = @dst_root + relpath + filename return unless srcfile.exist? dstfile.dirname.mkpath FileUtils.copy_file(srcfile, dstfile) end - def _blow_away(path) - (@dst_root + path).rmtree + def _blow_away(relpath) + (@dst_root + relpath).rmtree rescue Errno::ENOENT end end end