Skip to content

Commit

Permalink
Refactor CLI code.
Browse files Browse the repository at this point in the history
* Hash keys to symbols
* Avoided using OVERRIDES[kind] over and over using a variable.
  • Loading branch information
parndt committed Jul 3, 2012
1 parent 9b96ee6 commit e59938f
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions core/lib/refinery/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@ module Refinery
class CLI < Thor
include Thor::Actions

KINDS = {
'view' => {
'glob' => '*.{erb,builder}',
'dir' => 'views',
'desc' => 'view template',
OVERRIDES = {
:view => {
:glob => '*.{erb,builder}',
:dir => 'views',
:desc => 'view template',
},
'controller' => {
'glob' => '*.rb',
'dir' => 'controllers',
'desc' => 'controller',
:controller => {
:glob => '*.rb',
:dir => 'controllers',
:desc => 'controller',
},
'model' => {
'glob' => '*.rb',
'dir' => 'models',
'desc' => 'model',
:model => {
:glob => '*.rb',
:dir => 'models',
:desc => 'model',
},
'presenter' => {
'glob' => '*.rb',
'dir' => 'presenters',
'desc' => 'presenter',
:presenter => {
:glob => '*.rb',
:dir => 'presenters',
:desc => 'presenter',
},
'javascript' => {
'glob' => '*.js{,.*}',
'dir' => 'assets/javascripts',
'desc' => 'javascript',
:javascript => {
:glob => '*.js{,.*}',
:dir => 'assets/javascripts',
:desc => 'javascript',
},
'stylesheet' => {
'glob' => '*.css.scss',
'dir' => 'assets/stylesheets',
'desc' => 'stylesheet',
:stylesheet => {
:glob => '*.css.scss',
:dir => 'assets/stylesheets',
:desc => 'stylesheet',
},
}

desc "override", "copies files from any Refinery extension that you are using into your application"
def override(env)
for kind in KINDS.keys
if (which = env[kind]).present?
OVERRIDES.keys.each do |kind|
if (which = env[kind.to_s]).present?
return _override(kind, which)
end
end

puts "You didn't specify anything to override. Here are some examples:"
puts "You didn't specify anything valid to override. Here are some examples:"
{
:view => ['pages/home', 'refinery/pages/home', '**/*menu', '_menu_branch'],
:javascript => %w(admin refinery/site_bar),
Expand Down Expand Up @@ -96,28 +96,28 @@ def uncrudify(controller, action)
end
end


private

def _override(kind, which)
pattern = "{refinery#{File::SEPARATOR},}#{which.split("/").join(File::SEPARATOR)}#{KINDS[kind]['glob']}"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", KINDS[kind]['dir'], pattern).to_s}
override_kind = OVERRIDES[kind]
pattern = "{refinery#{File::SEPARATOR},}#{which.split("/").join(File::SEPARATOR)}#{override_kind[:glob]}"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", override_kind[:dir], pattern).to_s}

# copy in the matches
if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
matches.each do |match|
dir = match.split("/app/#{KINDS[kind]['dir']}/").last.split('/')
dir = match.split("/app/#{override_kind[:dir]}/").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", KINDS[kind]['dir'], dir)
destination_dir = Rails.root.join('app', override_kind[:dir], dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))

puts "Copied #{KINDS[kind]['desc']} file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
puts "Copied #{override_kind[:desc]} file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any #{KINDS[kind]['desc']} files in any extensions like #{which}"
puts "Couldn't match any #{override_kind[:desc]} files in any extensions like #{which}"
end
end
end
Expand Down

0 comments on commit e59938f

Please sign in to comment.