简体   繁体   中英

Making FSSM gem (file watcher) do it's thing to all files when it boots

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM