lib/svelte_on_rails/installer/haml.rb



module SvelteOnRails
  module Installer
    module Haml
      def self.install_haml_and_convert

        # install haml-rails

        puts '-' * 80

        gu = SvelteOnRails::GemUtils
        gu.install_gem('haml-rails')

        # check existing files

        existing_haml_files = Dir.glob('app/views/**/*.haml').select { |f| File.file? f }

        overwrite_files = []
        files = Dir.glob('app/views/**/*.html.erb').each_with_object([]) do |f, ary|
          new_file = f.gsub(/\.erb\z/, '.haml')
          if File.file? new_file
            overwrite_files += [new_file]
            existing_haml_files.delete(new_file)
          end
          if File.file? f
            ary << [
              f,
              new_file
            ]
          end
        end

        if files.empty?
          puts "No .erb files found, no conversion necessary."
          return
        end

        # ask if haml already exist

        if existing_haml_files.any?
          puts '-' * 80
          begin
            puts "Theare are already .haml files:"
            puts existing_haml_files
            puts "Would you like to continue? (y/n)"
            continue = STDIN.gets.chomp.downcase[0]
          end until ['y', 'n'].include?(continue)
          if continue == 'n'
            puts 'skipping convert to haml'
            return
          end
        end

        # ask if overwrite

        if overwrite_files.any?
          puts '-' * 80
          begin
            puts "The following files already exist and would be overwritten:"
            puts overwrite_files
            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

        # check for html2haml

        installed_html2haml = false
        if gu.check_gem_version('html2haml')
          puts "html2haml already installed, now converting..."
        else
          gu.install_gem('html2haml', group: 'development')
          installed_html2haml = true
        end

        # convert

        backup_dir = "svelte_on_rails_backup_views_#{Time.now.strftime('%Y%m%d_%H%M%S')}"
        FileUtils.mkdir_p(backup_dir)

        Dir.chdir(Rails.root) do
          files.each do |f|
            stdout, stderr, status = Open3.capture3("html2haml #{f.join(' ')}")
            if stderr.present?
              raise stderr
            else
              view_dir = File.dirname(f.first).to_s.match(/(?<=app\/views\/)(.*)/).to_s
              backup_view_dir = backup_dir + '/' + view_dir
              FileUtils.mkdir_p(backup_view_dir)
              FileUtils.mv(f.first, backup_view_dir)
              puts "Converted #{f.join(' => ')}"
            end
          end
        end

        if installed_html2haml
          system("bundle remove html2haml")
          if $?.success?
            puts "cleanup: removed html2haml from Gemfile"
          else
            puts "ERROR: failed to remove html2haml (please remove manually as it was only installed for this task)"
          end
        end

      end
    end
  end
end