#!/usr/bin/env ruby require 'softwear/lib' if ARGV.length > 0 && ARGV[0] == 'update' old_gemfile = File.open('Gemfile').read.gsub(/\r\n?/, "\n") gemfile = [] handler = nil append_line = nil injected_gems = false ignore_line = lambda do |line| if line.include? Softwear::Lib::GEMFILE_CLOSER gemfile << line handler = append_line end end append_line = lambda do |line| gemfile << line if line.include? Softwear::Lib::GEMFILE_OPENER puts "Updating common gems" gemfile << Softwear::Lib::COMMON_GEMS injected_gems = true handler = ignore_line end end handler = append_line old_gemfile.each_line do |line| handler.call(line) end unless injected_gems puts "Adding common gems - check for duplicates!" gemfile << "\n" + Softwear::Lib::GEMFILE_OPENER + "\n" gemfile << Softwear::Lib::COMMON_GEMS gemfile << Softwear::Lib::GEMFILE_CLOSER + "\n" end File.open('Gemfile', 'w') do |file| gemfile.each do |line| file.write(line) end end puts "Done!" else puts "Run `softwear update` to update your gemfile's common dependencies" end