Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
support autoreload
  • Loading branch information
tka committed Feb 17, 2013
1 parent 7a32410 commit 6fcae7d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
39 changes: 35 additions & 4 deletions src/app_watcher.rb
@@ -1,7 +1,7 @@

if Compass::VERSION =~ /^0.12/
$LOAD_PATH.unshift File.join(LIB_PATH,'ruby','compass_0.12','backport_from_0.13','lib')
require 'compass/watcher'
$LOAD_PATH.unshift File.join(LIB_PATH,'ruby','compass_0.12','backport_from_0.13','lib')
require 'compass/watcher'
end

module Compass
Expand All @@ -10,6 +10,7 @@ class AppWatcher < ProjectWatcher
def initialize(project_path, watches=[], options={}, poll=false)
super
@sass_watchers += coffeescript_watchers
@sass_watchers += livereload_watchers
setup_listener
end

Expand All @@ -30,9 +31,39 @@ def coffee_callback(base, file, action)
log_action(:info, "#{file} was #{action}", options)
puts( "#{file} was #{action}", options)
CoffeeCompiler.compile_folder( Compass.configuration.fireapp_coffeescripts_dir,
Compass.configuration.javascripts_dir,
Compass.configuration.fireapp_coffeescript_options );
Compass.configuration.javascripts_dir,
Compass.configuration.fireapp_coffeescript_options );
end

def livereload_watchers
::App::CONFIG["services_livereload_extensions"].split(/,/).map do |ext|
filter = "**.#{ext}"
Watcher::Watch.new(filter, &method(:livereload_callback))
end
end

def livereload_callback(base, file, action)
puts ">>> #{action} detected to: #{file}"
SimpleLivereload.instance.send_livereload_msg( base, file ) if SimpleLivereload.instance.alive?

if App::CONFIG["notifications"].include?(:overwrite) && action == :modified
App.notifications << "Changed: #{file}"
end

tray = Tray.instance
tray.shell.display.wake if tray.shell
end

def setup_listener
@listener = Listen.to(@project_path, :relative_paths => true)
if poll
@listener = listener.force_polling(true)
end
@listener = listener.polling_fallback_message(POLLING_MESSAGE)
#@listener = listener.ignore(/\.css$/) # we dont ignore .css, because we need livereload
@listener = listener.change(&method(:listen_callback))
end

end
end
end
40 changes: 3 additions & 37 deletions src/livereload.rb
Expand Up @@ -43,8 +43,6 @@ def initialize
end

def watch(dir, options)
unwatch
start_watch_project(dir)
start_websocket_server(options)
end

Expand Down Expand Up @@ -96,9 +94,11 @@ def unwatch
if @livereload_thread && @livereload_thread.alive?
EventMachine::WebSocket.stop
end
@watch_project_thread.kill if @watch_project_thread && @watch_project_thread.alive?
end

def alive?
@livereload_thread && @livereload_thread.alive?
end

def send_livereload_msg( base, relative )
data = JSON.dump( ['refresh', { :path => URI.escape(File.join(base, relative)),
Expand All @@ -112,40 +112,6 @@ def send_livereload_msg( base, relative )
end
end

def start_watch_project(dir)
tray = Tray.instance
@watch_project_thread = Thread.new do
FSSM.monitor do |monitor|
monitor.path dir do |path|

if defined?(::App)
extensions = ::App::CONFIG["services_livereload_extensions"]
else
extensions = "css,png,jpg,gif,html,erb,haml"
end

path.glob "**/*.{#{extensions}}"

path.update do |base, relative|
puts ">>> Change detected to: #{relative}"
SimpleLivereload.instance.send_livereload_msg( base, relative )
if defined?(App) && App::CONFIG["notifications"].include?(:overwrite)
App.notifications << "Changed: #{relative}"
tray.shell.display.wake if tray.shell
end
end
path.create do |base, relative|
puts ">>> New file detected: #{relative}"
SimpleLivereload.instance.send_livereload_msg( base, relative )
end
path.delete do |base, relative|
puts ">>> File Removed: #{relative}"
SimpleLivereload.instance.send_livereload_msg( base, relative )
end
end
end
end
end

end

0 comments on commit 6fcae7d

Please sign in to comment.