让 FSSM gem(文件观察器)在启动时对所有文件执行此操作

发布于 2024-10-16 07:37:33 字数 583 浏览 4 评论 0原文

我有一个简单的 FSSM(文件系统状态监视器) 脚本设置来监视一些文件并用它们执行一些操作当它们被改变时。工作起来棒极了。

但是,我希望它在启动脚本时处理具有更新处理程序的任何文件。就像现在一样,如果我在启动脚本之前对文件进行编辑,那么在我再次保存文件之前,我的编辑不会被拾取。在一个需要监视数十个文件的项目中,这并不理想。

那么,如何让 FSSM 处理配置为在我的脚本启动时监视的每个文件呢?

我现在拥有的一个片段:

monitor = FSSM::Monitor.new
monitor.path '.' do
  glob '**/*.coffee'

  update do |base, relative|
    coffee base, relative
  end

  delete do |base, relative|
    remove base, relative
  end
end
monitor.run

我希望它在启动时运行更新子句,以便在未观看时编辑的任何文件都会立即得到处理。

I have a simple FSSM (File System State Monitor) script setup to watch some files and do some things with them when they are changed. It's woking great.

However, I want it to process any files it has an update handler for when I boot my script. As it is now, if I make edits to files before I start the script then my edits arent picked up until I save the file again. And in a project with dozens of files being watched, that is less than ideal.

So how make FSSM process every file it is configured to watch when my script launches?

A snippet of what I have now:

monitor = FSSM::Monitor.new
monitor.path '.' do
  glob '**/*.coffee'

  update do |base, relative|
    coffee base, relative
  end

  delete do |base, relative|
    remove base, relative
  end
end
monitor.run

I'd like it to run the update clause on launch so that any files edited while they were not being watched would get immediately processed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-10-23 07:37:33

最简单的方法可能是在每次调用更新子句时更新零字节文件的时间戳,然后在启动时处理比该标记文件更新的任何文件。由于 update 子句需要在两个地方使用,因此将其存储为 lambda。

# marker file
ts = ".timestamp"
File.open(ts,"w"){} unless File.exists? ts


# store the update block, so we're DRY
update_clause = lambda do |base, relative| 
       coffee base, relative
       atime = mtime = Time.now
       File.utime(atime, mtime, ts)
end

# run the update against any files newer than the timestamp file
Dir["**/*.coffee"].each do |f|
    if File.mtime(f) > File.mtime(ts)
       update_clause.call(File.dirname(f), File.basename(f))
    end
end

monitor = FSSM::Monitor.new
monitor.path '.' do
  glob '**/*.coffee'

  update &update_clause

  delete do |base, relative|
    remove base, relative
  end
end
monitor.run

我对 Ruby 相当陌生,因此可能有更清晰或更惯用的语法,但基本思想应该会照顾到您。

The simplest method would probably be to update the timestamp of a zero-byte file on each call of the update clause, then on launch process any files that are newer than that marker file. Since the update clause will need to be used in two places, store it as lambda.

# marker file
ts = ".timestamp"
File.open(ts,"w"){} unless File.exists? ts


# store the update block, so we're DRY
update_clause = lambda do |base, relative| 
       coffee base, relative
       atime = mtime = Time.now
       File.utime(atime, mtime, ts)
end

# run the update against any files newer than the timestamp file
Dir["**/*.coffee"].each do |f|
    if File.mtime(f) > File.mtime(ts)
       update_clause.call(File.dirname(f), File.basename(f))
    end
end

monitor = FSSM::Monitor.new
monitor.path '.' do
  glob '**/*.coffee'

  update &update_clause

  delete do |base, relative|
    remove base, relative
  end
end
monitor.run

I'm fairly new to Ruby, so there may be a cleaner or more idiomatic syntax for this, but the basic idea should take care of you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文