From bac12a49f4fa82cff02b7f943f3a2e4713d3851f Mon Sep 17 00:00:00 2001 From: Ingo Becker Date: Fri, 21 Mar 2014 18:24:04 +0100 Subject: [PATCH 1/2] added convertion support for mountable-engines and added user-definable convertion path --- README.md | 6 ++ lib/erb2haml/railties/erb2haml.rake | 98 ++++++++++++++--------------- 2 files changed, 55 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 30ebb8c..9ae23d5 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,12 @@ original ERB templates or not. | Yes | `rake haml:convert_erbs` | | No | `rake haml:replace_erbs` | +By default, erb2haml looks for ERB templates inside `app/views` and additional `test/dummy/app/views` in case of an engine. If you have ERB templates inside another non-standard direcotry(e.g. `spec/dummy/app/views`) just pass the path to the rake task as an environment variable called `ERB_PATH`: + +``` +rake haml:convert_erbs ERB_PATH=spec/dummy/app/views +``` + ## License Copyright (c) 2011-2013 David Leung and [contributors](https://github.com/dhl/erb2haml/contributors). See LICENSE for further details. diff --git a/lib/erb2haml/railties/erb2haml.rake b/lib/erb2haml/railties/erb2haml.rake index e716d12..b2b2ed4 100644 --- a/lib/erb2haml/railties/erb2haml.rake +++ b/lib/erb2haml/railties/erb2haml.rake @@ -9,72 +9,72 @@ def color(text, begin_text_style) begin_text_style + text + END_TEXT_STYLE end -namespace :haml do - desc "Perform bulk conversion of all html.erb files to Haml in views folder." - task :convert_erbs do - - if `which html2haml`.empty? - puts "#{color "ERROR: ", RED_FG} Could not find " + - "#{color "html2haml", GREEN_FG} in your PATH. Aborting." - exit(false) - end +def erb_paths + if ENV.has_key? 'ERB_PATH' + [ENV['ERB_PATH']] + else + ['app/views/', 'test/dummy/app/views'] + end +end - puts "Looking for #{color "ERB", GREEN_FG} files to convert to " + - "#{color("Haml", RED_FG)}..." +def check_html2haml + if `which html2haml`.empty? + puts "#{color "ERROR: ", RED_FG} Could not find " + + "#{color "html2haml", GREEN_FG} in your PATH. Aborting." + exit(false) + end +end - Find.find("app/views/") do |path| - if FileTest.file?(path) and path.downcase.match(/\.erb$/i) - haml_path = path.slice(0...-3)+"haml" +def erb2haml(path, options={remove: false}) + Find.find(path) do |path| + if FileTest.file?(path) and path.downcase.match(/\.erb$/i) + haml_path = path.slice(0...-3)+"haml" - unless FileTest.exists?(haml_path) - print "Converting: #{path}... " + unless FileTest.exists?(haml_path) + print "Converting: #{path}... " - if system("html2haml", path, haml_path) - puts color("Done!", GREEN_FG) - else - puts color("Failed!", RED_FG) + if system("html2haml", path, haml_path) + puts color("Done!", GREEN_FG) + if options[:remove] + print "Removing: #{path}... " + if system("rm", path) + puts color("Removed!", GREEN_FG) + else + puts color("Removal failed!", RED_FG) + end end - + else + puts color("Failed!", RED_FG) end - end end - end #End rake task + end +end - desc "Perform bulk conversion of all html.erb files to Haml in views folder, then remove the converted html.erb files." - task :replace_erbs do - if `which html2haml`.empty? - puts "#{color "ERROR: ", RED_FG} Could not find " + - "#{color "html2haml", GREEN_FG} in your PATH. Aborting." - exit(false) - end +namespace :haml do + desc "Perform bulk conversion of all html.erb files to Haml in views folder." + task :convert_erbs do + + check_html2haml puts "Looking for #{color "ERB", GREEN_FG} files to convert to " + "#{color("Haml", RED_FG)}..." - Find.find("app/views/") do |path| - if FileTest.file?(path) and path.downcase.match(/\.erb$/i) - haml_path = path.slice(0...-3)+"haml" - - unless FileTest.exists?(haml_path) - print "Converting: #{path}... " + erb_paths.each do |path| + erb2haml path + end + end #End rake task - if system("html2haml", path, haml_path) - puts color("Done!", GREEN_FG) - print "Removing: #{path}... " - if system("rm", path) - puts color("Removed!", GREEN_FG) - else - puts color("Failed!", RED_FG) - end - else - puts color("Failed!", RED_FG) - end + desc "Perform bulk conversion of all html.erb files to Haml in views folder, then remove the converted html.erb files." + task :replace_erbs do - end + check_html2haml - end + puts "Looking for #{color "ERB", GREEN_FG} files to convert to " + + "#{color("Haml", RED_FG)}..." + erb_paths.each do |path| + erb2haml path, remove: true end end #End rake task From 28b3780cd9b560ea7d1b47e92ff2b9eab7cb1fd4 Mon Sep 17 00:00:00 2001 From: Ingo Becker Date: Fri, 21 Mar 2014 18:47:12 +0100 Subject: [PATCH 2/2] warn the user if user-defined path doesn't exist --- lib/erb2haml/railties/erb2haml.rake | 40 +++++++++++++++++------------ 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/erb2haml/railties/erb2haml.rake b/lib/erb2haml/railties/erb2haml.rake index b2b2ed4..4e7589b 100644 --- a/lib/erb2haml/railties/erb2haml.rake +++ b/lib/erb2haml/railties/erb2haml.rake @@ -26,28 +26,34 @@ def check_html2haml end def erb2haml(path, options={remove: false}) - Find.find(path) do |path| - if FileTest.file?(path) and path.downcase.match(/\.erb$/i) - haml_path = path.slice(0...-3)+"haml" - - unless FileTest.exists?(haml_path) - print "Converting: #{path}... " - - if system("html2haml", path, haml_path) - puts color("Done!", GREEN_FG) - if options[:remove] - print "Removing: #{path}... " - if system("rm", path) - puts color("Removed!", GREEN_FG) - else - puts color("Removal failed!", RED_FG) + begin + Find.find(path) do |path| + if FileTest.file?(path) and path.downcase.match(/\.erb$/i) + haml_path = path.slice(0...-3)+"haml" + + unless FileTest.exists?(haml_path) + print "Converting: #{path}... " + + if system("html2haml", path, haml_path) + puts color("Done!", GREEN_FG) + if options[:remove] + print "Removing: #{path}... " + if system("rm", path) + puts color("Removed!", GREEN_FG) + else + puts color("Removal failed!", RED_FG) + end end + else + puts color("Failed!", RED_FG) end - else - puts color("Failed!", RED_FG) end end end + rescue + if ENV['ERB_PATH'] && ENV['ERB_PATH'] == path + puts color("Path #{path} not found!", RED_FG) + end end end