加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Guard执行两次shell脚本

发布时间:2020-12-17 03:14:34 所属栏目:百科 来源:网络整理
导读:我设置了一个具有以下结构的示例项目: GemfileGuardfile 这些文件的内容是: # Gemfilesource :rubygemsgem "guard"gem "guard-shell" 和 # Guardfileguard 'shell' do watch(/^test.txt$/) {|m| `echo #{m.inspect} #{File.mtime(m[0])}` }end 然后我继续
我设置了一个具有以下结构的示例项目:

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

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

$echo blah >> test.txt

在shell运行中:

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

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

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

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

解决方法

看起来像是防护和/或防护外壳的bug /功能.我会在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读