Skip to content

Writting rake task

May 7, 2012

Lib/tasks/mytask.rake

require "haml/html"

namespace :html2haml do
desc “convert your erb to haml”
task :convert => :environment do
Dir[“#{Rails.root}/app/views/**/*.erb”].each do |file_name|

haml_file_name = file_name.gsub(/erb$/,”haml”)
puts “Converting: #{file_name} to #{haml_file_name}”
if !File.exist?(haml_file_name)
erb_string = File.open(file_name).read
haml_string = Haml::HTML.new(erb_string, :erb => true).render
f = File.open(haml_file_name, “w”)
f.write haml_string
end
end
end

desc “disable erb support for rails”
task :disable_erb => :environment do
Dir[“#{Rails.root}/app/views/**/*.erb”].each do |file_name|
backup_file = file_name.gsub(/erb$/,”erb_backup”)
puts “backing up the #{file_name} to #{backup_file}”
system(“mv #{file_name} #{backup_file} “)
end
end

desc “clean”
task :clean => :environment do
Dir[“#{Rails.root}/app/views/**/*.erb”].each do |file_name|

puts “destroying : #{file_name}”
File.delete(file_name)
end
end

desc “rollback erb support”
task :enable_erb => :environment do
Dir[“#{Rails.root}/app/views/**/*.erb_backup”].each do |file_name|
erb_file = file_name.gsub(/erb_backup$/,”erb”)
puts “backing up the #{file_name} to #{erb_file}”
system(“mv #{file_name} #{erb_file} “)
end
end
end

 

Advertisements

From → Rubyonrails

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: