forked from netzpirat/guard-coffeescript
/
coffeescript.rb
103 lines (88 loc) · 3.21 KB
/
coffeescript.rb
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
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
require 'guard'
require 'guard/guard'
require 'guard/watcher'
module Guard
# The CoffeeScript guard that gets notifications about the following
# Guard events: `start`, `stop`, `reload`, `run_all` and `run_on_change`.
#
class CoffeeScript < Guard
autoload :Formatter, 'guard/coffeescript/formatter'
autoload :Inspector, 'guard/coffeescript/inspector'
autoload :Runner, 'guard/coffeescript/runner'
DEFAULT_OPTIONS = {
:bare => false,
:shallow => false,
:hide_success => false,
:noop => false,
:all_on_start => false
}
# Initialize Guard::CoffeeScript.
#
# @param [Array<Guard::Watcher>] watchers the watchers in the Guard block
# @param [Hash] options the options for the Guard
# @option options [String] :input the input directory
# @option options [String] :output the output directory
# @option options [Boolean] :bare do not wrap the output in a top level function
# @option options [Boolean] :shallow do not create nested directories
# @option options [Boolean] :hide_success hide success message notification
# @option options [Boolean] :all_on_start generate all JavaScripts files on start
# @option options [Boolean] :noop do not generate an output file
#
def initialize(watchers = [], options = {})
watchers = [] if !watchers
defaults = DEFAULT_OPTIONS.clone
if options[:input]
defaults.merge!({ :output => options[:input] })
watchers << ::Guard::Watcher.new(%r{^#{ options.delete(:input) }/(.+\.coffee)$})
end
super(watchers, defaults.merge(options))
end
# Gets called once when Guard starts.
#
# @raise [:task_has_failed] when stop has failed
#
def start
run_all if options[:all_on_start]
end
# Gets called when all files should be regenerated.
#
# @raise [:task_has_failed] when stop has failed
#
def run_all
run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*.coffee'))))
end
# Gets called when watched paths and files have changes.
#
# @param [Array<String>] paths the changed paths and files
# @raise [:task_has_failed] when stop has failed
#
def run_on_change(paths)
changed_files, success = Runner.run(Inspector.clean(paths), watchers, options)
notify changed_files
throw :task_has_failed unless success
end
# Called on file(s) deletions that the Guard watches.
#
# @param [Array<String>] paths the deleted files or paths
# @raise [:task_has_failed] when run_on_change has failed
#
def run_on_deletion(paths)
Inspector.clean(paths).each do |file|
javascript = file.gsub(/(js\.coffee|coffee)$/, 'js')
File.remove(javascript) if File.exists?(javascript)
end
end
private
# Notify changed files back to Guard, so that other Guards can continue
# to work with the generated files.
#
# @param [Array<String>] changed_files the files that have been changed
#
def notify(changed_files)
::Guard.guards.each do |guard|
paths = Watcher.match_files(guard, changed_files)
guard.run_on_change paths unless paths.empty?
end
end
end
end