Created
March 2, 2015 17:31
-
-
Save chris-roerig/b6d57be82657d17c6c67 to your computer and use it in GitHub Desktop.
A rake task for creating hamlbars templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
namespace :hamlbars do | |
desc "Creates a hamlbars template in app/assets/javascripts/templates" | |
task :template do | |
template = ARGV.last | |
dir = template.rpartition("/").first | |
file = template.rpartition("/").last | |
begin | |
created = create_new_template_file(dir, file) | |
puts "created #{created}" | |
rescue => e | |
puts e.message | |
end | |
task template.to_s do ; end | |
end | |
def create_new_template_file(dir, file) | |
file_dir = "app/assets/javascripts/templates/#{dir}" | |
full_path = "#{file_dir}/#{file}.hamlbars" | |
if File.exists?(full_path) | |
puts "File exists: #{full_path}" | |
print "Overwrite? [y/n]: " | |
overwrite = STDIN.gets.chomp.downcase | |
raise 'Skipping overwrite' unless overwrite == 'y' | |
FileUtils.rm(full_path) | |
end | |
FileUtils.mkdir_p(file_dir) | |
FileUtils.touch(full_path) | |
return full_path | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment