Skip to content

Instantly share code, notes, and snippets.

@chris-roerig
Created March 2, 2015 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-roerig/b6d57be82657d17c6c67 to your computer and use it in GitHub Desktop.
Save chris-roerig/b6d57be82657d17c6c67 to your computer and use it in GitHub Desktop.
A rake task for creating hamlbars templates
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