#!/usr/bin/env ruby require 'fileutils' REFINERY_ROOT = File.expand_path(File.dirname(__FILE__) << "/..") RAILS_ROOT = ARGV.first unless defined? RAILS_ROOT unless RAILS_ROOT.nil? or RAILS_ROOT.length == 0 # ensure asset directories exist. dirs = [%w(public stylesheets), %w(public javascripts), %w(db migrate), %w(lib tasks)] FileUtils::makedirs dirs.map {|dir| File.join(RAILS_ROOT, dir) } # copy in the new assets. assets = [%w(public stylesheets refinery), %w(public javascripts refinery)] assets.each do |asset| FileUtils::rm_rf File.join(RAILS_ROOT, asset), :secure => true # ensure the destination is clear. FileUtils::cp_r File.join(REFINERY_ROOT, asset), File.join(RAILS_ROOT, asset) # copy the new assets into the project. end # copy in any new migrations. FileUtils::cp Dir[File.join(REFINERY_ROOT, %w(db migrate *.rb))], File.join(RAILS_ROOT, %w(db migrate)) # copy any rake tasks from plugins to the main lib directory so they can be run. FileUtils::cp Dir[File.join(REFINERY_ROOT, %w(** tasks *.rake))], File.join(RAILS_ROOT, %w(lib tasks)) # read in the config files app_config = File.open(File.join(RAILS_ROOT, %w(config environment.rb)), "r").read environment_updated = false # backup app's environment.rb unless (app_refinery_gems_section = app_config.scan(/(#===REFINERY REQUIRED GEMS===)([^#]*)(#===REFINERY END OF REQUIRED GEMS===)/).join("")).length == 0 # read in the config file in the gem refinery_config = File.open(File.join(REFINERY_ROOT, %w(config environment.rb)), "r").read FileUtils.cp File.join(RAILS_ROOT, %w(config environment.rb)), File.join(RAILS_ROOT, %w(config environment.autobackupbyrefinery.rb)) # write the new content into the file. File.open(File.join(RAILS_ROOT, %w(config environment.rb)), "w").puts(app_config.gsub!( app_refinery_gems_section, refinery_config.scan(/(#===REFINERY REQUIRED GEMS===)([^#]*)(#===REFINERY END OF REQUIRED GEMS===)/).join("") )) environment_updated = true end unless ARGV.include?("--from-refinery-installer") puts "---------" puts "Copied new Refinery core assets." if environment_updated puts "I've made a backup of your current config/environment.rb file as it has been updated with the latest Refinery RubyGem requirements." puts "The backup is located at config/environment.autobackupbyrefinery.rb incase you need it." end puts "" puts "=== ACTION REQUIRED ===" puts "Please run rake db:migrate to ensure your database is at the correct version." puts "Please also run rake gems:install to ensure you have the currently specified gems." if environment_updated puts "" end else unless ARGV.include?("--from-refinery-installer") puts "Please specify the path of the refinery project that you want to update, i.e. refinery-update-core /path/to/project" end end