Guard执行两次shell脚本

jab*_*sad 5 ruby guard

我设置了一个具有以下结构的示例项目:

Gemfile
Guardfile
Run Code Online (Sandbox Code Playgroud)

这些文件的内容是:

# Gemfile
source :rubygems
gem "guard"
gem "guard-shell"
Run Code Online (Sandbox Code Playgroud)

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

然后我继续跑guard.每当我在该文件中回显某些内容时,警卫会记录两次更改.在一个shell中:

$ echo blah >> test.txt
Run Code Online (Sandbox Code Playgroud)

在shell运行中:

> [test.txt] 2012-06-26 00:40:22 +0200
> [test.txt] 2012-06-26 00:40:22 +0200
Run Code Online (Sandbox Code Playgroud)

同样的行为可以解释vim/nano等.有趣的是,当我跑步时echo blah > test.txt,守卫只会发射一次.

知道如何防止这种情况发生或者这是否是预期的行为?

编辑:在github上打开一个问题:https://github.com/guard/guard/issues/297#issuecomment-6586266

Cas*_*per 5

看起来像guard和/或的bug /功能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
Run Code Online (Sandbox Code Playgroud)