class DevboxLauncher::Watchman

Attributes

dir[R]

Public Class Methods

new(dir:) click to toggle source
# File lib/devbox_launcher/watchman.rb, line 6
def initialize(dir:)
  @dir = dir
end

Public Instance Methods

sockname() click to toggle source
# File lib/devbox_launcher/watchman.rb, line 37
def sockname
  sockname = RubyWatchman.load(
    %x{watchman --output-encoding=bser get-sockname}
  )['sockname']

  if !$?.exitstatus.zero?
    raise "Failed to connect to watchman. Is it running?"
  end

  sockname
end
trigger(command) click to toggle source
# File lib/devbox_launcher/watchman.rb, line 10
def trigger(command)
  UNIXSocket.open(sockname) do |socket|
    root = Pathname.new(dir).expand_path.to_s
    result = RubyWatchman.query(['watch-list'], socket)
    roots = result['roots']
    if !roots.include?(root)
      # this path isn't being watched yet; try to set up watch
      result = RubyWatchman.query(['watch-project', root], socket)

      # root_restrict_files setting may prevent Watchman from working
      raise "Unable to watch #{dir}" if result.has_key?('error')
    end

    query = ['trigger', root, {
      'name' => 'mutagen-sync',
      'expression' => ['match', '**/*', 'wholename'],
      'command' => command.split(" "),
    }]
    paths = RubyWatchman.query(query, socket)

    # could return error if watch is removed
    if paths.has_key?('error')
      raise "Unable to set trigger. Error: #{paths['error']}"
    end
  end
end