-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhamlr.thor
63 lines (55 loc) · 1.58 KB
/
hamlr.thor
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
require 'find'
require 'rubygems'
begin
require 'haml'
require 'haml/exec'
rescue LoadError => e
dep = e.message[/^no such file to load -- (.*)/, 1]
puts <<MESSAGE
Required dependency #{dep} not found!
Run "gem install #{dep}" to get it.
MESSAGE
exit 1
end
class Hamlr < Thor::Group
include Thor::Actions
class_option :delete, :default => true, :aliases => "-d", :type => :boolean, :desc => "Delete the original erb files?"
class_option :html, :default => true, :aliases => "-h", :type => :boolean, :desc => "Convert HTML as well?"
desc "convert all html/erb views in your project to haml"
def convert
branch_project
exts = [".erb", ".html"]
exts.delete(".html") unless options[:html]
Find.find(destination_root) do |f|
ext = File.extname(f)
if exts.include? ext
begin
haml = Haml::Exec::HTML2Haml.new(["-r", "#{f}", "#{f.gsub(/#{ext}$/, '.haml')}"])
haml.parse
remove_file(f) if options[:delete]
rescue Exception => e
puts "Oh snap! Error: #{e.message}"
end
end
end
puts "All files converted. Please confirm changes then merge `hamlr` branch."
end
private
def using_git?
File.directory?('.git')
end
def branch_project
if using_git?
begin
`git branch -D hamlr` if File.file?("#{destination_root}/.git/refs/heads/hamlr")
`git checkout -b hamlr`
rescue Exception => e
puts "Oh snap! Error: #{e.message}"
exit 1
end
else
puts "Use git please. I feel bad doing this work without it."
exit 1
end
end
end