204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/livereload.rb', line 204
def self.run(directories, explicit_config)
EM.kqueue = true if EM.kqueue?
web_sockets = []
global_config = Config.merge(DEFAULT_CONFIG, USER_CONFIG, explicit_config)
directories = directories.collect { |directory| File.expand_path(directory) }
projects = directories.collect { |directory| Project.new(directory, explicit_config) }
puts
puts "Version: #{GEM_VERSION} (compatible with browser extension versions #{API_VERSION}.x)"
puts "Port: #{global_config.port}"
projects.each { |project| project.print_config }
EventMachine.run do
projects.each do |project|
project.when_changes_detected do |event, modified_file|
case event
when :config_changed
puts
puts ">> Configuration change: " + modified_file
puts
EventMachine.next_tick do
projects.each { |project| project.read_config; project.print_config }
puts
projects.each { |project| project.restart_watching }
end
when :excluded
puts "Excluded: #{File.basename(modified_file)}"
when :modified
puts "Modified: #{File.basename(modified_file)}"
data = ['refresh', { :path => modified_file,
:apply_js_live => project.config.apply_js_live,
:apply_css_live => project.config.apply_css_live,
:apply_images_live => project.config.apply_images_live }].to_json
puts data if global_config.debug
web_sockets.each do |ws|
ws.send data
end
end
end
end
projects.each { |project| project.restart_watching }
puts
puts "LiveReload is waiting for browser to connect."
EventMachine::WebSocket.start(:host => global_config.host, :port => global_config.port, :debug => global_config.debug) do |ws|
ws.onopen do
begin
puts "Browser connected."; ws.send "!!ver:#{API_VERSION}"; web_sockets << ws
rescue
puts $!
puts $!.backtrace
end
end
ws.onmessage do |msg|
puts "Browser URL: #{msg}"
end
ws.onclose do
web_sockets.delete ws
puts "Browser disconnected."
end
end
end
end
|