#!/usr/bin/env ruby require 'pathname' require 'fileutils' require 'yaml' refinery_root = if defined?(REFINERY_ROOT) and REFINERY_ROOT.is_a?(Pathname) REFINERY_ROOT else Pathname.new(File.expand_path(File.dirname(__FILE__) << "/..")) end unless (app_path = ARGV.shift).nil? or app_path.length == 0 # if "" or "." or "./" is specified then get the current directory otherwise accept the specified app_path. if (app_path.length <= 2 and ((is_current_dir = app_path =~ /(\.(\/)?)/).nil? or is_current_dir < 2)) rails_root = Dir.getwd else rails_root = app_path =~ /^\// ? app_path : File.expand_path(File.join(Dir.getwd, app_path.gsub(/^\.\//, ''))) end rails_root = Pathname.new(rails_root.to_s) if rails_root.join('Gemfile').exist? puts "Your application appears to already be running Refinery CMS >= 0.9.7" exit end puts "--------Updating--------\n\n" FileUtils::cp refinery_root.join('Gemfile').to_s, rails_root.join('Gemfile').to_s, :verbose => true FileUtils::cp refinery_root.join('config', 'preinitializer.rb').to_s, rails_root.join('config', 'preinitializer.rb').to_s, :verbose => true # try to figure out the database adapter.. db_adapter = 'sqlite3' if rails_root.join('config', 'database.yml').exist? db_config = YAML::load(rails_root.join('config', 'database.yml').open('r').read) if db_config.keys.include?("development") && db_config["development"].keys.include?("adapter") db_adapter = db_config["development"]["adapter"] end end db_adapter = 'sqlite3' unless %w(sqlite3 mysql postgresql).include?(db_adapter) # read in the Gemfile and write it back out with the refinerycms gem enabled. (lines = refinery_root.join('Gemfile').open('r').read.split("\n")).each do |line| line.gsub!(/\#*\s?gem 'refinerycms'/, "gem 'refinerycms'") # Ensure that the correct database gem libraries are included for the database adapter # that the user has specified in the refinery install command. if line =~ /\#db\_adapter\=/ if line =~ %r{#db_adapter=#{db_adapter}} line.gsub!(/^(\#*\s?gem)/, 'gem') else line.gsub!(/^(\s?gem)/, '# gem') end end end # write the new content into the file. app_gemfile = rails_root.join('Gemfile').open('w') app_gemfile.puts(lines.join("\n")) app_gemfile.close # backup the config file. app_config_file = 'application.rb' FileUtils.cp rails_root.join('config', app_config_file).cleanpath.to_s, rails_root.join('config', "#{app_config_file.gsub('.rb', '')}.autobackupbyrefineryupgrade.rb").cleanpath.to_s, :verbose => true # copy the new config file. FileUtils.cp refinery_root.join('config', app_config_file).cleanpath.to_s, rails_root.join('config', app_config_file).cleanpath.to_s, :verbose => true unless (rails_config_settings = rails_root.join('config', 'settings.rb')).exist? FileUtils.cp refinery_root.join('config', 'settings.rb').cleanpath.to_s, rails_config_settings.to_s, :verbose => true end unless (aai_config_file = rails_root.join('config', 'acts_as_indexed_config.rb')).exist? FileUtils::cp refinery_root.join('config', 'acts_as_indexed_config.rb').to_s, aai_config_file.to_s, :verbose => true end # update routes files puts "\nNow I'm verifying and updating your routing in your files and your plugins...\n" Dir[rails_root.join('**', 'config', 'routes.rb').to_s].map{|f| Pathname.new(f)}.each do |routes_file| open_routes_file = routes_file.open('r') lines = open_routes_file.read.split("\n") original_lines = lines.dup open_routes_file.close lines.each do |line| if routes_file.to_s == rails_root.join('config', 'routes.rb').to_s line.gsub!("ActionController::Routing::Routes.draw do |map|", "ActionController::Routing::Routes.draw do |map|\n map.filter(:locale)") line.gsub!("map.connect 'admin/*path', :controller => 'admin/base', :action => 'error_404'", "map.redirect 'admin/*path', :controller => 'admin/base'") line.gsub!("map.connect '*path', :controller => 'application', :action => 'error_404'", "map.connect 'refinery/*path', :controller => 'admin/base', :action => 'error_404'\n\n # Marketable URLs\n map.connect '*path', :controller => 'pages', :action => 'show'") end line.gsub!("map.namespace(:admin) do |admin|", "map.namespace(:admin, :path_prefix => 'refinery') do |admin|") end lines = lines.join("\n").split("\n") open_routes_file = routes_file.open("w") open_routes_file.puts(lines.join("\n")) open_routes_file.close puts "Made modifications to routes in #{routes_file} please ensure they are still valid." if original_lines != lines end puts "\nCopied files required to support the new RefineryCMS version 0.9.7" puts "I backed up your config/application.rb file to #{rails_root.join('config', "#{app_config_file.gsub('.rb', '')}.autobackupbyrefineryupgrade.rb").cleanpath.to_s}" puts "\nI think your database adapter is #{db_adapter} so that is what will be installed by bundler.\n\n" # automate.. puts "Installing gem requirements using bundler..\n" puts (cmd="bundle install --without test") puts `cd '#{rails_root}' && #{cmd}` if rails_root.join('config', 'database.yml').exist? puts "\n\nUpdating some core refinery files..\n" puts (cmd="rake -f '#{rails_root.join('Rakefile')}' refinery:update from_installer=true") puts `cd '#{rails_root}' && #{cmd}` puts "\n\nMigrating your database..\n" puts (cmd="rake -f '#{rails_root.join('Rakefile')}' db:migrate") puts `cd '#{rails_root}' && #{cmd}` else puts "\nYour config/database.yml file is missing so I can't run a task that is required." puts "Once you have your database file in place or are confident it will work without it please run:" puts "\nrake refinery:update" end # end automation.. puts "\n--------Update complete--------\n\n" else puts "\nPlease specify the path for the RefineryCMS application you want to upgrade. i.e. refinery-upgrade-096-to-097 /path/to/project" end