From e752dbe1c17cb9ee810834ca97bf30344672c020 Mon Sep 17 00:00:00 2001 From: Thibaud Guillaume-Gentil Date: Mon, 29 Aug 2011 21:25:58 +0200 Subject: [PATCH] Maybe first working version (but still a work in progress) --- lib/guard.rb | 35 +++++++++++++++++---------- lib/guard/interactor.rb | 52 ++++++++++++++++------------------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index cf218fe..dcfb1e4 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -16,6 +16,8 @@ module Guard @options = options @listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd) @guards = [] + @locked = false + @files = [] @options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off @@ -35,13 +37,22 @@ module Guard listener.on_change do |files| Dsl.reevaluate_guardfile if Watcher.match_guardfile?(files) - run { run_on_change_for_all_guards(files) } if Watcher.match_files?(guards, files) + @files += files if Watcher.match_files?(guards, files) end UI.info "Guard is now watching at '#{listener.directory}'" guards.each { |guard| supervised_task(guard, :start) } Interactor.listen + Thread.new do + loop do + if @files != [] && !@listener_locked + files = @files.dup + @files.clear + run { run_on_change_for_all_guards(files) } + end + end + end listener.start end @@ -55,10 +66,10 @@ module Guard end # Reparse the whole directory to catch new files modified during the guards run - new_modified_files = listener.modified_files([listener.directory], :all => true) - if !new_modified_files.empty? && Watcher.match_files?(guards, new_modified_files) - run { run_on_change_for_all_guards(new_modified_files) } - end + # new_modified_files = listener.modified_files([listener.directory], :all => true) + # if !new_modified_files.empty? && Watcher.match_files?(guards, new_modified_files) + # run { run_on_change_for_all_guards(new_modified_files) } + # end end # Let a guard execute its task but @@ -74,15 +85,15 @@ module Guard end def run - listener.stop - Interactor.cancel + @listener_locked = true + Interactor.lock UI.clear if options[:clear] - begin + # begin yield - rescue Interrupt - end - Interactor.listen - listener.start + # rescue Interrupt + # end + Interactor.unlock + @listener_locked = false end def add_guard(name, watchers = [], options = {}) diff --git a/lib/guard/interactor.rb b/lib/guard/interactor.rb index 8da4633..eb0a46f 100644 --- a/lib/guard/interactor.rb +++ b/lib/guard/interactor.rb @@ -2,51 +2,39 @@ module Guard module Interactor extend self - @@stopped = false + @@locked = false def listen return if ENV["GUARD_ENV"] == 'test' - # if @@thread - # puts "listen kill" - # @@thread.stop - # end Thread.new do - # puts "listen begin" - @@stopped = false - if entry = $stdin.gets - return if @@stopped - # puts "listen gets #{entry}" - entry.gsub! /\n/, '' - case entry - when 'quit', 'exit', 'q', 'e' - UI.info "Bye bye...", :reset => true - ::Guard.listener.stop - ::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :stop) } - abort - when 'reload', 'r', 'z' - # puts "listen reload" - Thread.new do - ::Guard.run do - ::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :reload) } - end - end - else # run_all - # puts "listen run_all" - Thread.new do + loop do + if (entry = $stdin.gets) && !@@locked + entry.gsub! /\n/, '' + case entry + when 'quit', 'exit', 'q', 'e' + UI.info "Bye bye...", :reset => true + ::Guard.listener.stop + ::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :stop) } + abort + when 'reload', 'r', 'z' + ::Guard.run do + ::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :reload) } + end + else # run_all ::Guard.run do ::Guard.guards.each { |guard| ::Guard.supervised_task(guard, :run_all) } end end end - # puts "listen end" end - # puts "listen end" end end - def cancel - # puts "listen stop" - @@stopped = true + def lock + @@locked = true + end + def unlock + @@locked = false end # def run_all