Skip to content

Commit

Permalink
update app_watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
hungtatai committed Aug 25, 2014
1 parent d83aa77 commit ca26e23
Showing 1 changed file with 44 additions and 76 deletions.
120 changes: 44 additions & 76 deletions src/app_watcher.rb
Original file line number Diff line number Diff line change
@@ -1,90 +1,58 @@
module Compass
module Watcher

class LivereloadWatch < Watch
def match?(changed_path)
@glob.split(/,/).each do |ext|
changed_path =~ Regexp.new("#{ext}\\Z")
end
end
end

class AppWatcher < ProjectWatcher
def initialize(project_path, watches=[], options={}, poll=false)
super
@watchers << livereload_watchers
setup_listener
end

def listen_callback(modified_files, added_files, removed_files)
#log_action(:info, ">>> Listen Callback fired added: #{added_files}, mod: #{modified_files}, rem: #{removed_files}", {})
files = {:modified => modified_files,
:added => added_files,
:removed => removed_files}
require 'livereload.rb'

run_once, run_each = watchers.partition {|w| w.run_once_per_changeset?}
class AppWatcher < Compass::Commands::WatchProject
def initialize(project_path, options={})
super

run_once.each do |watcher|
if file = files.values.flatten.detect{|f| watcher.match?(f) }
action = files.keys.detect{|k| files[k].include?(file) }
log_action(:warning, Dir.pwd, {})
log_action(:warning, project_path.inspect,{})
watcher.run_callback(project_path, relative_to(file, project_path), action)
end
end
CompassHooker::WatchHooker.watches += livereload_watchers

end

run_each.each do |watcher|
files.each do |action, list|
list.each do |file|
watcher.run_callback(project_path, relative_to(file, project_path), action) if watcher.match?(file)
end
end
end
java.lang.System.gc()
end
def watch!
perform
sass_compiler.compile!
end

def stop
listener = sass_compiler.compiler.listener

log_action(:info, "AppWatcher stop!",{})
begin
listener.stop if listener and listener.adapter
rescue Exception => e
log_action(:warning, "#{e.message}\n#{e.backtrace}", {})
end

def watch!
compile
super
end
end

def stop
log_action(:info, "AppWatcher stop!",{})
begin
listener.stop
rescue Exception => e
log_action(:warning, "#{e.message}\n#{e.backtrace}", {})
end
end

def custom_watcher(dir, extensions, callback)
filter = File.join(dir, extensions)
childe_filter = File.join(dir, "**", extensions)

def livereload_watchers
Watcher::LivereloadWatch.new(::App::CONFIG["services_livereload_extensions"], &method(:livereload_callback))
end
[Compass::Configuration::Watch.new(filter, &callback),
Compass::Configuration::Watch.new(childe_filter, &callback)]
end

def livereload_watchers
watches = []
App::CONFIG["services_livereload_extensions"].split(/\s*,\s*/).each do |ext|
watches += custom_watcher("", "*.#{ext}", method(:livereload_callback))
end
watches
end

def livereload_callback(base, file, action)
puts ">>> #{action} detected to: #{file}"
SimpleLivereload.instance.send_livereload_msg( base, file ) if SimpleLivereload.instance.alive?
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
if App::CONFIG["notifications"].include?(:overwrite) && action == :modified
App.notifications << "Changed: #{file}"
end

tray = Tray.instance
tray.shell.display.wake if tray.shell
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

0 comments on commit ca26e23

Please sign in to comment.