Skip to content

Instantly share code, notes, and snippets.

@mckinnsb
Created January 5, 2012 22:24
Show Gist options
  • Save mckinnsb/1567659 to your computer and use it in GitHub Desktop.
Save mckinnsb/1567659 to your computer and use it in GitHub Desktop.
#Clear the existing refinery:override task.
Rake::Task["refinery:override"].clear
namespace :refinery do
desc "Override files for use in an application"
task :override => :environment do
require 'fileutils'
if (view = ENV["view"]).present?
pattern = "#{view.split("/").join(File::SEPARATOR)}*.{erb,builder}"
looking_for = Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "views", pattern).to_s}
# copy in the matches
matches = looking_for.collect{|d| Dir[d]}.flatten.compact.uniq
if matches.any?
matches.each do |match|
dir = match.split("/app/views/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together
unless (theme = ENV["theme"]).present?
destination_dir = Rails.root.join("app", "views", dir)
else
destination_dir = Rails.root.join("themes", theme, "views", dir)
end
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))
puts "Copied view template file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any view template files in any engines like #{view}"
end
elsif (controller = ENV["controller"]).present?
pattern = "#{controller.split("/").join(File::SEPARATOR)}*.rb"
looking_for = Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "controllers", pattern).to_s}
# copy in the matches
matches = looking_for.collect{|d| Dir[d]}.flatten.compact.uniq
if matches.any?
matches.each do |match|
dir = match.split("/app/controllers/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together
destination_dir = Rails.root.join("app", "controllers", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))
puts "Copied controller file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any controller files in any engines like #{controller}"
end
elsif (helper = ENV["helper"]).present?
pattern = "#{helper.split("/").join(File::SEPARATOR)}*.rb"
looking_for = []
Refinery::Plugins.registered.pathnames.each do |p|
looking_for << p.join("app", "helpers", pattern).to_s
#Also look for the "Refinery" helpers for the dashboard
looking_for << p.join("lib", "refinery", "helpers", pattern).to_s
end
# copy in the matches
matches = looking_for.collect{|d| Dir[d]}.flatten.compact.uniq
if matches.any?
matches.each do |match|
case match
when /\/app\/helpers/ then
#These are not namespaced - or shouldn't be.
dir = match.split("/app/helpers/").last.split('/')
when /\/lib\/refinery\/helpers/ then
#These will be namespaced.
dir = match.split("/lib/").last.split('/')
end
debugger
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together
destination_dir = Rails.root.join("app", "helpers", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))
puts "Copied helper file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any helper files in any engines like #{helper}"
end
elsif (model = ENV["model"]).present?
pattern = "#{model.split("/").join(File::SEPARATOR)}*.rb"
looking_for = Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "models", pattern).to_s}
# copy in the matches
matches = looking_for.collect{|d| Dir[d]}.flatten.compact.uniq
if matches.any?
matches.each do |match|
dir = match.split("/app/models/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together
destination_dir = Rails.root.join("app", "models", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))
puts "Copied model file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any model files in any engines like #{model}"
end
elsif (javascripts = ENV["javascript"]).present?
pattern = "#{javascripts.split("/").join(File::SEPARATOR)}*.js"
looking_for = Refinery::Plugins.registered.pathnames.map{|p| p.join("public", "javascripts", pattern).to_s}
# copy in the matches
matches = looking_for.collect{|d| Dir[d]}.flatten.compact.uniq
if matches.any?
matches.each do |match|
dir = match.split("/public/javascripts/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together
destination_dir = Rails.root.join("public", "javascripts", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))
puts "Copied javascript file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any javascript files in any engines like #{javascript}"
end
elsif (stylesheets = ENV["stylesheet"]).present?
pattern = "#{stylesheets.split("/").join(File::SEPARATOR)}*.css"
looking_for = Refinery::Plugins.registered.pathnames.map{|p| p.join("public", "stylesheets", pattern).to_s}
# copy in the matches
matches = looking_for.collect{|d| Dir[d]}.flatten.compact.uniq
if matches.any?
matches.each do |match|
dir = match.split("/public/stylesheets/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together
destination_dir = Rails.root.join("public", "stylesheets", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))
puts "Copied stylesheet file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any stylesheet files in any engines like #{stylesheets}"
end
else
puts "You didn't specify anything to override. Here's some examples:"
{
:view => ['pages/home', 'pages/home theme=demolicious', '**/*menu', 'shared/_menu_branch'],
:javascript => %w(jquery),
:stylesheet => %w(refinery/site_bar),
:controller => %w(pages),
:model => %w(page),
:helper => %w(script)
}.each do |type, examples|
examples.each do |example|
puts "rake refinery:override #{type}=#{example}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment