2010-12-25 05:07:52 -05:00
|
|
|
require 'tmpdir'
|
|
|
|
|
2010-06-13 07:04:24 -04:00
|
|
|
module Devise
|
|
|
|
module Generators
|
|
|
|
class ViewsGenerator < Rails::Generators::Base
|
2010-07-04 11:08:00 -04:00
|
|
|
source_root File.expand_path("../../../../app/views", __FILE__)
|
2010-06-13 07:04:24 -04:00
|
|
|
desc "Copies all Devise views to your application."
|
|
|
|
|
|
|
|
argument :scope, :required => false, :default => nil,
|
|
|
|
:desc => "The scope to copy views to"
|
|
|
|
|
2010-08-05 21:13:26 -04:00
|
|
|
class_option :template_engine, :type => :string, :aliases => "-t",
|
2010-12-22 09:46:25 -05:00
|
|
|
:desc => "Template engine for the views. Available options are 'erb', 'haml' and 'slim'."
|
2010-06-13 07:04:24 -04:00
|
|
|
|
|
|
|
def copy_views
|
2010-07-04 07:13:00 -04:00
|
|
|
case options[:template_engine].to_s
|
2010-06-13 07:04:24 -04:00
|
|
|
when "haml"
|
|
|
|
verify_haml_existence
|
|
|
|
verify_haml_version
|
|
|
|
create_and_copy_haml_views
|
2010-12-22 09:46:25 -05:00
|
|
|
when "slim"
|
|
|
|
verify_haml_existence
|
|
|
|
verify_haml_version
|
|
|
|
verify_haml2slim_existence
|
|
|
|
verify_haml2slim_version
|
|
|
|
create_and_copy_slim_views
|
2010-06-13 07:04:24 -04:00
|
|
|
else
|
|
|
|
directory "devise", "app/views/#{scope || :devise}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def verify_haml_existence
|
|
|
|
begin
|
|
|
|
require 'haml'
|
|
|
|
rescue LoadError
|
2010-12-22 09:46:25 -05:00
|
|
|
say "Haml is not installed, or it is not specified in your Gemfile."
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_haml2slim_existence
|
|
|
|
begin
|
|
|
|
require 'haml2slim'
|
|
|
|
rescue LoadError
|
|
|
|
say "Haml2Slim is not installed, or it is not specified in your Gemfile."
|
2010-06-13 07:04:24 -04:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_haml_version
|
2010-12-22 09:46:25 -05:00
|
|
|
unless Haml.version[:major] == 2 && Haml.version[:minor] >= 3 || Haml.version[:major] >= 3
|
|
|
|
say "To generate Haml or Slim templates, you need to have Haml 2.3 or above installed."
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_haml2slim_version
|
|
|
|
unless Haml2Slim::VERSION.to_f >= '0.4.0'.to_f
|
|
|
|
say "To generate Slim templates, you need to have Haml2Slim 0.4.0 or above installed."
|
2010-06-13 07:04:24 -04:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_and_copy_haml_views
|
2010-12-22 09:46:25 -05:00
|
|
|
directory haml_tmp_root, "app/views/#{scope || :devise}"
|
2010-12-25 05:07:52 -05:00
|
|
|
FileUtils.rm_rf(haml_tmp_root)
|
2010-12-22 09:46:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_and_copy_slim_views
|
|
|
|
slim_tmp_root = Dir.mktmpdir("devise-slim.")
|
|
|
|
`haml2slim #{haml_tmp_root} #{slim_tmp_root}`
|
|
|
|
|
|
|
|
directory slim_tmp_root, "app/views/#{scope || :devise}"
|
|
|
|
|
2010-12-25 05:07:52 -05:00
|
|
|
FileUtils.rm_rf(haml_tmp_root)
|
2010-12-22 09:46:25 -05:00
|
|
|
FileUtils.rm_rf(slim_tmp_root)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_haml_views
|
2010-12-25 05:07:52 -05:00
|
|
|
@haml_tmp_root ||= begin
|
|
|
|
html_root = "#{self.class.source_root}/devise"
|
|
|
|
haml_tmp_root = Dir.mktmpdir("devise-haml.")
|
|
|
|
|
|
|
|
Dir["#{html_root}/**/*"].each do |path|
|
|
|
|
relative_path = path.sub(html_root, "")
|
2011-01-11 05:56:43 -05:00
|
|
|
source_path = (haml_tmp_root + relative_path).sub(/erb$/, "haml")
|
2010-12-25 05:07:52 -05:00
|
|
|
|
|
|
|
if File.directory?(path)
|
|
|
|
FileUtils.mkdir_p(source_path)
|
|
|
|
else
|
|
|
|
`html2haml -r #{path} #{source_path}`
|
|
|
|
end
|
2010-12-22 09:46:25 -05:00
|
|
|
end
|
|
|
|
|
2010-12-25 05:07:52 -05:00
|
|
|
haml_tmp_root
|
|
|
|
end
|
2010-06-13 07:04:24 -04:00
|
|
|
end
|
2010-12-22 09:46:25 -05:00
|
|
|
|
|
|
|
alias :haml_tmp_root :create_haml_views
|
2010-06-13 07:04:24 -04:00
|
|
|
end
|
|
|
|
end
|
2011-01-11 05:56:43 -05:00
|
|
|
end
|