Skip to content

Instantly share code, notes, and snippets.

@jsooriah
Forked from Catharz/haml.rake
Created October 1, 2012 15:19
Show Gist options
  • Save jsooriah/3812430 to your computer and use it in GitHub Desktop.
Save jsooriah/3812430 to your computer and use it in GitHub Desktop.
Rake task to convert views from erb to haml
class ToHaml
def initialize(path)
@path = path
end
def convert_all!
Dir["#{@path}/**/*.erb"].each do |file|
haml_file = file.gsub(/\.erb$/, '.haml')
puts "Converting #{File.basename(file)} to #{File.basename(haml_file)}"
`html2haml -rx #{file} #{haml_file}`
end
end
# html2haml will create an empty file before parsing
# so can't just 'git mv' and then convert.
# need to convert a 'backup' of the file.
def git_convert_all!
Dir["#{@path}/**/*.erb"].each do |file|
haml_file = file.gsub(/\.erb$/, '.haml')
temp_file = file.gsub(/\.erb$/, '.tmp')
begin
puts "Copying #{file} to #{temp_file}"
File.copy_stream file, temp_file
puts "Renaming #{file} to #{haml_file}"
`git mv #{file} #{haml_file}`
puts "Converting #{File.basename(file)} to #{File.basename(haml_file)}"
`html2haml -rx #{temp_file} #{haml_file}`
puts "Removing #{temp_file}"
File.delete temp_file
rescue
File.rename(temp_file, file)
File.delete(temp_file)
end
end
end
end
namespace :haml do
namespace :convert do
desc "Convert all erb files to haml"
task :local do
path = File.join(Rails.root, 'app', 'views')
ToHaml.new(path).convert_all!
end
end
namespace :convert do
desc "Rename erb files to haml via git and convert"
task :git do
path = File.join(Rails.root, 'app', 'views')
ToHaml.new(path).git_convert_all!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment