2010-02-17 06:25:20 -05:00
|
|
|
class DeviseViewsGenerator < Rails::Generators::Base
|
|
|
|
desc "Copies all Devise views to your application."
|
2010-03-13 09:17:12 -05:00
|
|
|
|
2010-03-10 10:53:09 -05:00
|
|
|
argument :scope, :required => false, :default => nil,
|
|
|
|
:desc => "The scope to copy views to"
|
2010-03-13 09:17:12 -05:00
|
|
|
|
2010-03-14 04:39:59 -04:00
|
|
|
class_option :template_engine, :type => :string, :aliases => "-t", :default => "erb",
|
|
|
|
:desc => "Template engine for the views. Available options are 'erb' and 'haml'."
|
2010-03-13 09:17:12 -05:00
|
|
|
|
2010-02-17 06:25:20 -05:00
|
|
|
def self.source_root
|
|
|
|
@_devise_source_root ||= File.expand_path("../../../../app/views", __FILE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_views
|
2010-03-14 04:39:59 -04:00
|
|
|
case options[:template_engine]
|
2010-03-13 09:17:12 -05:00
|
|
|
when "haml"
|
2010-03-13 10:21:38 -05:00
|
|
|
verify_haml_existence
|
2010-03-13 10:06:52 -05:00
|
|
|
verify_haml_version
|
2010-03-13 09:17:12 -05:00
|
|
|
create_and_copy_haml_views
|
2010-03-15 21:51:59 -04:00
|
|
|
else
|
|
|
|
directory "devise", "app/views/devise/#{scope}"
|
2010-03-13 09:17:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2010-03-13 10:21:38 -05:00
|
|
|
def verify_haml_existence
|
2010-03-13 21:34:41 -05:00
|
|
|
begin
|
|
|
|
require 'haml'
|
|
|
|
rescue LoadError
|
2010-03-13 10:21:38 -05:00
|
|
|
say "HAML is not installed, or it is not specified in your Gemfile."
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_haml_version
|
|
|
|
unless Haml.version[:major] >= 2 and Haml.version[:minor] >= 3
|
|
|
|
say "To generate HAML templates, you need to install HAML 2.3 or above."
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-13 09:17:12 -05:00
|
|
|
def create_and_copy_haml_views
|
2010-03-13 21:48:04 -05:00
|
|
|
require 'tmpdir'
|
2010-03-14 04:39:59 -04:00
|
|
|
html_root = "#{self.class.source_root}/devise"
|
2010-03-14 09:00:55 -04:00
|
|
|
|
|
|
|
Dir.mktmpdir("devise-haml.") do |haml_root|
|
|
|
|
Dir["#{html_root}/**/*"].each do |path|
|
|
|
|
relative_path = path.sub(html_root, "")
|
|
|
|
source_path = (haml_root + relative_path).sub(/erb$/, "haml")
|
|
|
|
|
|
|
|
if File.directory?(path)
|
|
|
|
FileUtils.mkdir_p(source_path)
|
|
|
|
else
|
|
|
|
`html2haml -r #{path} #{source_path}`
|
|
|
|
end
|
2010-03-13 09:17:12 -05:00
|
|
|
end
|
2010-03-14 09:00:55 -04:00
|
|
|
|
|
|
|
directory haml_root, "app/views/devise/#{scope}"
|
2010-03-13 09:17:12 -05:00
|
|
|
end
|
2010-02-20 01:54:05 -05:00
|
|
|
end
|
2010-02-17 06:25:20 -05:00
|
|
|
end
|