module Rake::Tilde

Constants

VERSION

Public Instance Methods

listen(**args, &blk) click to toggle source
# File lib/rake/tilde.rb, line 14
def listen(**args, &blk)
  name = args.delete(:to)
  args[:blk] = blk
  paths[name] = args
end
listeners() click to toggle source
# File lib/rake/tilde.rb, line 24
def listeners
  @listeners ||= []
end
listening?() click to toggle source
# File lib/rake/tilde.rb, line 20
def listening?
  !listeners.empty?
end
paths() click to toggle source
# File lib/rake/tilde.rb, line 10
def paths
  @paths ||= {}
end
run(task_name, *task_args) click to toggle source
# File lib/rake/tilde.rb, line 28
def run(task_name, *task_args)
  task        = Rake.application[task_name]
  listen_path = paths.fetch(task_name.intern) {{}}
  paths       = listen_path.fetch(:paths, listen_path.fetch(:path) { Dir.pwd })
  opts        = listen_path.fetch(:opts) {{}}
  blk         = listen_path[:blk]

  begin
    task.invoke(*task_args)
  rescue StandardError => exception
    $stderr.puts "** Task failed to run successfully with tilde"
    Rake.application.display_error_message(exception)
    Rake.application.exit_because_of_exception(exception)
  end

  listener = Listen.to(paths, opts) do |modified, added, removed|
    $stdout.puts "** File system changed"
    begin
      pid = spawn("rake #{task.name}")
      Process.wait pid
      blk.call(modified, added, removed) if blk
    rescue StandardError => exception
      $stderr.puts "** Task failed to run successfully with tilde"
      Rake.application.display_error_message(exception)
    end
  end

  listeners.push listener
  listener.start
end
sleep_forever() click to toggle source
# File lib/rake/tilde.rb, line 59
def sleep_forever
  begin
    sleep
  rescue Interrupt
    puts
  end
end