Guard executes shell scripts twice

I am setting up an example project with the following structure:

Gemfile
Guardfile

The contents of these files:

# Gemfile
source :rubygems
gem "guard"
gem "guard-shell"

and

# Guardfile
guard 'shell' do
  watch(/^test\.txt$/) {|m| `echo #{m.inspect} #{File.mtime(m[0])}` }
end

Then I continue to work guard. Whenever I repeat something in this file, the guard logs the change twice. In one shell:

$ echo blah >> test.txt

The protection works in the shell:

> [test.txt] 2012-06-26 00:40:22 +0200
> [test.txt] 2012-06-26 00:40:22 +0200

The same behavior takes into account vim / nano, etc. Interestingly, when I just start echo blah > test.txt, protection only works once.

Any idea how I can prevent this, or is this the expected behavior?

EDIT: open issue on github: https://github.com/guard/guard/issues/297#issuecomment-6586266

+5
source share
1 answer

/ guard / guard-shell. github. () , mtime :

# Guardfile
class GFilter
  def self.run(files, &block)
    @mtimes ||= Hash.new

    files.each { |f|
      mtime = File.mtime(f)
      next if @mtimes[f] == mtime
      @mtimes[f] = mtime

      yield f
    }
  end
end

guard 'shell' do
  watch(/^test\.txt$/) {|m| GFilter.run(m) { |f| puts "File: #{f}" } }
end
+5

All Articles