5

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

Gemfile
Guardfile

这些文件的内容是:

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

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

然后我继续跑guard。每当我将某些内容回显到该文件中时,guard 都会注册两次更改。在一个外壳中:

$ echo blah >> test.txt

在外壳运行后卫中:

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

vim/nano 等也是同样的行为。有趣的是,当我只是 run 时echo blah > test.txt,guard 只会触发一次。

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

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

4

1 回答 1

5

似乎是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
于 2012-06-26T02:01:59.813 回答