class Spud::Watch

Public Class Methods

new(driver:, task:, ordered:, named:, watches:) click to toggle source
# File lib/spud/watch.rb, line 31
def initialize(driver:, task:, ordered:, named:, watches:)
  @driver = driver
  @task = task
  @ordered = ordered
  @named = named
  @watches = watches

  @last_changed = Time.at(0)
end
run!(driver:, task:, ordered:, named:, watches:) click to toggle source
# File lib/spud/watch.rb, line 18
def self.run!(driver:, task:, ordered:, named:, watches:)
  new(driver: driver, task: task, ordered: ordered, named: named, watches: watches).run!
end

Public Instance Methods

latest_watch_change() click to toggle source
# File lib/spud/watch.rb, line 67
def latest_watch_change
  T.unsafe(Dir)[*@watches]
    .map(&File.method(:stat))
    .map(&:mtime)
    .max
end
run!() click to toggle source
# File lib/spud/watch.rb, line 42
def run!
  thread = T.let(nil, T.nilable(Thread))

  puts "running '#{@task}' on changes to #{@watches.map { |glob| "'#{glob}'" }.join(', ')}"
  loop do
    if watches_changed?
      thread&.kill
      Process.kill('SIGKILL', T.must(@driver.subprocess_pid)) if @driver.subprocess_pid

      @last_changed = latest_watch_change
      thread = Thread.new { @driver.invoke(@task, @ordered, @named) }
    end

    sleep(0.1)
  end
rescue Interrupt => error
  puts "handled interrupt #{error}" if @driver.debug?
end
watches_changed?() click to toggle source
# File lib/spud/watch.rb, line 62
def watches_changed?
  @last_changed < latest_watch_change
end