1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Added support for generating Slim view templates.

This commit is contained in:
Fred Wu 2010-12-22 22:46:25 +08:00 committed by José Valim
parent f56c588a6a
commit 4519364eaf

View file

@ -8,7 +8,7 @@ module Devise
:desc => "The scope to copy views to"
class_option :template_engine, :type => :string, :aliases => "-t",
:desc => "Template engine for the views. Available options are 'erb' and 'haml'."
:desc => "Template engine for the views. Available options are 'erb', 'haml' and 'slim'."
def copy_views
case options[:template_engine].to_s
@ -16,6 +16,12 @@ module Devise
verify_haml_existence
verify_haml_version
create_and_copy_haml_views
when "slim"
verify_haml_existence
verify_haml_version
verify_haml2slim_existence
verify_haml2slim_version
create_and_copy_slim_views
else
directory "devise", "app/views/#{scope || :devise}"
end
@ -27,37 +33,72 @@ module Devise
begin
require 'haml'
rescue LoadError
say "HAML is not installed, or it is not specified in your Gemfile."
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."
exit
end
end
def verify_haml_version
unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
say "To generate HAML templates, you need to install HAML 2.3 or above."
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."
exit
end
end
def create_and_copy_haml_views
require 'tmpdir'
html_root = "#{self.class.source_root}/devise"
directory haml_tmp_root, "app/views/#{scope || :devise}"
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
end
directory haml_root, "app/views/#{scope || :devise}"
end
FileUtils.rm_rf(haml_tmp_root)
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}"
FileUtils.rm_rf(haml_tmp_root)
FileUtils.rm_rf(slim_tmp_root)
end
private
def create_haml_views
require 'tmpdir'
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, "")
source_path = (haml_tmp_root + relative_path).sub(/erb$/, "haml")
if File.directory?(path)
FileUtils.mkdir_p(source_path)
else
`html2haml -r #{path} #{source_path}`
end
end
haml_tmp_root
end
alias :haml_tmp_root :create_haml_views
end
end
end