forked from alloy/kicker
/
kicker.rb
124 lines (101 loc) · 2.55 KB
/
kicker.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
$:.unshift File.expand_path('../../vendor', __FILE__)
require 'rucola/fsevents'
require 'kicker/callback_chain'
require 'kicker/core_ext'
require 'kicker/growl'
require 'kicker/log_status_helper'
require 'kicker/options'
require 'kicker/utils'
class Kicker #:nodoc:
def self.run(argv = ARGV)
Kicker::Options.parse(argv)
new.start
end
attr_reader :last_event_processed_at
def initialize
finished_processing!
end
def paths
@paths ||= Kicker.paths.map { |path| File.expand_path(path) }
end
def start
validate_options!
log "Watching for changes on: #{paths.join(', ')}"
log ''
run_watch_dog!
Kicker::Growl.start! if Kicker::Growl.use?
run_startup_chain
OSX.CFRunLoopRun
end
private
def validate_options!
validate_paths_and_command!
validate_paths_exist!
end
def validate_paths_and_command!
if startup_chain.empty?
puts Kicker::Options.parser.help
exit
end
end
def validate_paths_exist!
paths.each do |path|
unless File.exist?(path)
puts "The given path `#{path}' does not exist"
exit 1
end
end
end
def run_watch_dog!
dirs = @paths.map { |path| File.directory?(path) ? path : File.dirname(path) }
watch_dog = Rucola::FSEvents.start_watching(dirs, :latency => self.class.latency) do |events|
process events
end
trap('INT') do
log "Exiting…"
watch_dog.stop
exit
end
end
def run_startup_chain
startup_chain.call([], false)
end
def finished_processing!
@last_event_processed_at = Time.now
end
def process(events)
unless (files = changed_files(events)).empty?
full_chain.call(files)
finished_processing!
end
end
def changed_files(events)
make_paths_relative(events.map do |event|
files_in_directory(event.path).select { |file| file_changed_since_last_event? file }
end.flatten.uniq.sort)
end
def files_in_directory(dir)
Dir.entries(dir)[2..-1].map { |f| File.join(dir, f) }
rescue Errno::ENOENT
[]
end
def file_changed_since_last_event?(file)
File.mtime(file) > @last_event_processed_at
rescue Errno::ENOENT
false
end
def make_paths_relative(files)
return files if files.empty?
wd = Dir.pwd
files.map do |file|
if file[0..wd.length-1] == wd
file[wd.length+1..-1]
else
file
end
end
end
end
# Load this as last, because it actually loads all recipes, so everything has
# to be defined before that.
require 'kicker/recipes'