File: erb2haml.rake

package info (click to toggle)
ruby-haml-rails 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 228 kB
  • sloc: ruby: 281; makefile: 4
file content (83 lines) | stat: -rw-r--r-- 2,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
namespace :haml do
  desc 'Convert html.erb to html.haml each file in app/views'
  task :erb2haml 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}

    if erb_files.empty?
      puts "No .erb files found. Task will now exit."
      exit
    end

    erb_files_to_convert = erb_files.dup

    haml_files_w_out_ext = haml_files.map { |f| f.gsub(/\.haml\z/, '') }

    # Get a list of all those erb files that already seem to have .haml equivalents

    already_existing = erb_files.select { |f| short = f.gsub(/\.erb\z/, ''); haml_files_w_out_ext.include?(short) }

    puts '-'*80

    if already_existing.any?
      puts "Some of your .html.erb files seem to already have .haml equivalents:"
      already_existing.map { |f| puts "\t#{f}" }

      if ENV.has_key?("HAML_RAILS_OVERWRITE_HAML") && (ENV["HAML_RAILS_OVERWRITE_HAML"] == "false")
        should_overwrite = 'n'
      else
        # Ask the user whether he/she would like to overwrite them.
        begin
          puts "Would you like to overwrite these .haml files? (y/n)"
          should_overwrite = STDIN.gets.chomp.downcase[0]
        end until ['y', 'n'].include?(should_overwrite)
      end
      puts '-'*80

      # If we are not overwriting, remove each already_existing from our erb_files list
      if should_overwrite == 'n'
        erb_files_to_convert = erb_files - already_existing

        if erb_files_to_convert.empty?
          # It is possible no .erb files remain, after we remove already_existing
          puts "No .erb files to convert"
        end
      else
        # Delete the current .haml
        already_existing.each { |f| File.delete(f.gsub(/\.erb\z/, '.haml')) }
      end
    end

    erb_files_to_convert.each do |file|
      puts "Generating HAML for #{file}..."
      `html2haml #{file} #{file.gsub(/\.erb\z/, '.haml')}`
    end

    puts '-'*80

    puts "HAML generated for the following files:"
    erb_files_to_convert.each do |file|
      puts "\t#{file}"
    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)'
        should_delete = STDIN.gets.chomp.downcase[0]
      end until ['y', 'n'].include?(should_delete)
    end
    if should_delete == 'y'
      puts "Deleting original .erb files."
      File.delete(*erb_files)
    else
      puts "Please remember to delete your .erb files once you have ensured they were translated correctly."
    end

    puts '-'*80
    puts "Task complete!"
  end
end