1 | require "guard"
|
2 | require "guard/guard"
|
3 | require "json"
|
4 | require "erb"
|
5 |
|
6 | module ::Guard
|
7 | class Thrift < ::Guard::Guard
|
8 | def initialize(watchers=[], options={})
|
9 | super(watchers, options)
|
10 | end
|
11 |
|
12 | def start
|
13 | run_all if options[:all_on_start]
|
14 | end
|
15 |
|
16 | def run_all
|
17 | files = Watcher.match_files(self, Dir.glob('**{,/*/**}/*.thrift'))
|
18 | run_on_changes(Watcher.match_files(self, files))
|
19 | end
|
20 |
|
21 | def run_on_changes(paths)
|
22 | clean_targets!
|
23 |
|
24 | statuses = ["thrift"].product(language_options, paths.uniq).map{|c| c.join(" ")}.map do |command|
|
25 | puts "running #{command}"
|
26 | puts `#{command}`
|
27 | $?
|
28 | end
|
29 |
|
30 | if statuses.any?{ |status| !status.success? }
|
31 | puts "******errored******"
|
32 | throw :task_has_failed
|
33 | end
|
34 | end
|
35 |
|
36 | def run_on_removals(paths)
|
37 | run_on_changes(paths)
|
38 | end
|
39 |
|
40 | protected
|
41 |
|
42 | def clean_targets!
|
43 | if @options[:clean_target] && @options[:targets].is_a?(Hash)
|
44 | @options[:targets].keys.each do |path|
|
45 | command = "rm -rf #{path}/*"
|
46 | puts "running #{command}"
|
47 | puts `#{command}`
|
48 | end
|
49 | end
|
50 | end
|
51 |
|
52 | def language_options
|
53 | if @options[:targets].is_a? Hash
|
54 | target_option = @options[:gen_folder] ? "-o" : "-out"
|
55 | @options[:targets].map do |path, language|
|
56 | "#{target_option} #{path} --gen #{language}"
|
57 | end
|
58 | else
|
59 | @options[:targets].map do |language|
|
60 | "--gen #{language}"
|
61 | end
|
62 | end
|
63 | end
|
64 | end
|
65 | end
|
66 |
|
67 | guard :shell, all_on_start: true do
|
68 | watch("thrift/hearts.thrift.erb") do
|
69 | ava_version = JSON.parse(File.read("package.json"))["version"]
|
70 | puts "AVA VERSION #{ava_version}"
|
71 | file = "thrift/hearts.thrift.erb"
|
72 | output = "thrift/gen/hearts.thrift"
|
73 | contents = ::ERB.new(File.read(file)).result(binding)
|
74 | File.open(output, 'w'){ |f| f.write(contents) }
|
75 | end
|
76 |
|
77 | watch("thrift/tic_tac_toe.thrift.erb") do
|
78 | ava_version = JSON.parse(File.read("package.json"))["version"]
|
79 | puts "AVA VERSION #{ava_version}"
|
80 | file = "thrift/tic_tac_toe.thrift.erb"
|
81 | output = "thrift/gen/tic_tac_toe.thrift"
|
82 | contents = ::ERB.new(File.read(file)).result(binding)
|
83 | File.open(output, 'w'){ |f| f.write(contents) }
|
84 | end
|
85 | end
|
86 |
|
87 | guard :thrift, all_on_start: true,
|
88 | clean_target: true,
|
89 | targets: { "service/hearts/types" => "js:node",
|
90 | "web/public/types/hearts" => "js:jquery",
|
91 | "dist/hearts/nodejs/lib" => "js:node",
|
92 | "dist/hearts/java/lib" => "java",
|
93 | "dist/hearts/haskell/lib" => "hs",
|
94 | "dist/hearts/go/lib" => "go",
|
95 | "dist/hearts/ruby/lib" => "rb" } do
|
96 | watch('thrift/gen/hearts.thrift')
|
97 | end
|
98 |
|
99 | guard :thrift, all_on_start: true,
|
100 | clean_target: true,
|
101 | targets: { "service/tic_tac_toe/types" => "js:node",
|
102 | "web/public/types/tic_tac_toe" => "js:jquery",
|
103 | "dist/tic_tac_toe/nodejs/lib" => "js:node",
|
104 | "dist/tic_tac_toe/ruby/lib" => "rb" } do
|
105 | watch('thrift/gen/tic_tac_toe.thrift')
|
106 | end
|
107 |
|
108 | guard :shell do
|
109 | watch(%r{vendor/thrift/lib/nodejs/(.*)}) {|m| puts "#{m[0]} changed, packaging thrift"; puts `make package-thrift` }
|
110 | ignore! []
|
111 | end
|