-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThorfile
68 lines (56 loc) · 1.84 KB
/
Thorfile
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
# encoding: utf-8
require 'rubygems'
require 'erb'
class Test < Thor
ROOT = File.dirname(__FILE__).freeze
REL = /^#{Regexp.escape(ROOT + File::SEPARATOR)}/.freeze
desc 'erb[recipes]', 'Tests erb templates of specified or all recipes for its validity.'
method_options :force => :array
def erb(recipes = [])
valid = true
Dir.glob(File.expand_path(File.join("**/*.erb"), ROOT), File::FNM_DOTMATCH) do |file_path|
next if dots?(file_path)
rel_name = file_path.sub(REL, '')
next unless from_recipes?(rel_name, recipes)
unless system("sh -c 'erubis -x #{file_path} | ruby -c'")
$stderr.puts("#{rel_name}: Cannot be compiled")
valid = false
end
end
show_valid('erb templates', valid)
end
desc 'rb[recipes]', 'Tests rb files of specified or all recipes for its validity.'
method_options :force => :array
def rb(recipes = [])
valid = true
Dir.glob(File.expand_path(File.join("**/*.rb"), ROOT), File::FNM_DOTMATCH) do |file_path|
next if dots?(file_path)
rel_name = file_path.sub(REL, '')
next unless from_recipes?(rel_name, recipes)
unless system("ruby -c #{file_path}")
$stderr.puts("#{rel_name}: Cannot be compiled")
valid = false
end
end
show_valid('rb code', valid)
end
desc 'all[recipes]', 'Tests all files of specified or all recipes for its validity.'
method_options :force => :array
def all(recipes = [])
rb(recipes)
erb(recipes)
end
private
def show_valid(msg, valid)
puts "#{msg}: #{valid ? 'OK' : 'FAIL'}"
end
def dots?(file_path)
file_path =~ /#{Regexp.escape(File::SEPARATOR)}\.\.?$/
end
def from_recipes?(rel_name, recipes)
return true if recipes.empty?
!!recipes.detect { |recipe| rel_name =~ /^#{Regexp.escape(recipe + File::SEPARATOR)}/ }
end
end
__END__
vim: set ft=ruby