#!/usr/bin/env ruby require 'fileutils' REFINERY_ROOT = File.expand_path(File.dirname(__FILE__) << "/..") unless (override_path = ARGV.shift).nil? or (override_path.length == 0) # if "" or "." or "./" is specified then get the current directory otherwise accept the specified app_path. if ((app_path = ARGV.shift).nil? or app_path.length <= 2) and ((is_current_dir = app_path =~ /(\.(\/)?)/).nil? or !is_current_dir or is_current_dir < 2) RAILS_ROOT = Dir.getwd else RAILS_ROOT = app_path end if File.exists? RAILS_ROOT # figure out what to override override_parts = override_path.downcase.split('/').compact.collect {|part| part if part.length > 0 }.compact admin = "" if override_parts.first == "admin" # we're inside the admin namespace admin = override_parts.shift end if override_parts.length > 1 # controller plus action is specified action = override_parts.pop end # controller is what's left. controller = override_parts.first controller_with_admin = [admin,override_parts].flatten.compact.join('/') # copy the controller unless controller_with_admin =~ /\*(\*)?/ and !action.nil? refinery_controllers = Dir[File.join(REFINERY_ROOT, %w(vendor plugins ** app controllers), "#{controller_with_admin}_controller.rb")] unless refinery_controllers.compact.empty? # the controllers may not exist. refinery_controllers.each do |refinery_controller| # make the directories FileUtils.mkdir_p(copy_to = File.join(RAILS_ROOT, %w(app controllers), admin)) FileUtils.cp(refinery_controller, copy_to) end else puts "Note: Couldn't find a matching controller to override." end end # copy the action, if it exists unless action.nil? or action.length == 0 # get all the matching files looking_for = File.join(REFINERY_ROOT, %w(vendor plugins ** app views), controller_with_admin.split('/'), "#{action}*.erb") action_files = Dir[looking_for] # copy in the action template action_files.each do |action_file| action_file_path = action_file.split("/app/views/").last action_file_dir = action_file_path.split('/') action_file_dir.pop # get rid of the file. FileUtils.mkdir_p(File.join(RAILS_ROOT, %w(app views), action_file_dir)) FileUtils.cp action_file, File.join(RAILS_ROOT, %w(app views), action_file_path) end else puts "Note: No action was specified." end else puts "Couldn't understand your project's directory?" end else puts "You didn't specify anything to override. Here's some examples:" puts "refinery-override /pages/* /path/to/my/project" puts "refinery-override /pages/show /path/to/my/project" puts "refinery-override /admin/pages/index" puts "refinery-override /shared/_menu /path/to/my/project" puts "refinery-override **/*menu /path/to/my/project" puts "refinery-override /shared/_menu_branch" end