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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/svelte_on_rails/installer/haml.rb', line 6
def self.install_haml_and_convert(force: false)
puts '-' * 80
gu = SvelteOnRails::GemUtils
gu.install_gem('haml-rails')
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
unless force
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
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
end
puts '-' * 80
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
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
|