Skip to content

fix: improve error handling in erb2haml task #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/tasks/erb2haml.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace :haml do

erb_files = Dir.glob('app/views/**/*.erb').select { |f| File.file? f}
haml_files = Dir.glob('app/views/**/*.haml').select { |f| File.file? f}
failed_files = []

if erb_files.empty?
puts "No .erb files found. Task will now exit."
Expand Down Expand Up @@ -52,27 +53,43 @@ namespace :haml do
erb_files_to_convert.each do |file|
puts "Generating HAML for #{file}..."
`html2haml #{file} #{file.gsub(/\.erb\z/, '.haml')}`
if !$?.success?
puts "Error: html2haml failed to convert #{file} to HAML. Please check the file for errors."
failed_files << file
end
end

successful_files = erb_files_to_convert - failed_files

puts '-'*80

puts "HAML generated for the following files:"
erb_files_to_convert.each do |file|
successful_files.each do |file|
puts "\t#{file}"
end

if failed_files.any?
puts "The following files failed to convert:"
failed_files.each do |file|
puts "\t#{file}"
end
puts "Please check the above files for errors and that you have html2haml installed properly."
end

puts '-'*80
if ENV.has_key?("HAML_RAILS_DELETE_ERB") && (ENV["HAML_RAILS_DELETE_ERB"] == "true")
should_delete = 'y'
else
begin
puts 'Would you like to delete the original .erb files? (This is not recommended unless you are under version control.) (y/n)'
puts 'Would you like to delete the original .erb files that successfully converted? (This is not recommended unless you are under version control.) (y/n)'
should_delete = STDIN.gets.chomp.downcase[0]
end until ['y', 'n'].include?(should_delete)
end
if should_delete == 'y'
if should_delete == 'y' && successful_files.any?
puts "Deleting original .erb files."
File.delete(*erb_files)
File.delete(*successful_files)
elsif should_delete == 'y' && successful_files.empty?
puts "No successfully converted files to delete."
else
puts "Please remember to delete your .erb files once you have ensured they were translated correctly."
end
Expand Down