forked from refinery/refinerycms
/
refinery-override
76 lines (67 loc) · 3.15 KB
/
refinery-override
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
refinery_root = (defined?(REFINERY_ROOT) && REFINERY_ROOT.is_a?(Pathname) ? REFINERY_ROOT : Pathname.new(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 = Pathname.new(Dir.getwd)
else
rails_root = Pathname.new(app_path)
end
if rails_root.exist?
# 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|refinery/
# we're inside the admin namespace
admin = override_parts.shift.gsub('refinery', 'admin')
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('/').gsub(/^\//, '')
# copy the controller
unless controller_with_admin =~ /\*(\*)?/ and !action.nil?
refinery_controllers = Dir[refinery_root.join("vendor", "plugins", "**", "app", "controllers", "#{controller_with_admin}_controller.rb")].compact
if refinery_controllers.any? # the controllers may not exist.
refinery_controllers.each do |refinery_controller|
# make the directories
FileUtils.mkdir_p(copy_to = rails_root.join("app", "controllers", admin).to_s)
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 = refinery_root.join("vendor", "plugins", "**", "app", "views", controller_with_admin.split("/").join(File::SEPARATOR), "#{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(rails_root.join("app", "views", action_file_dir.join(File::SEPARATOR)))
FileUtils.cp action_file, rails_root.join("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 /refinery/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