Files
Greg Hurrell d924624677 Fix (spurious) test failure on big-endian architectures
Our example includes a int_64_t value (the PDU's length prefix), which
is endian-sensitive and so needs to be asserted using native endianness.

Originally reported as a bug on the upstream project where I initially
prototyped the Watchman protocol Ruby implementation. References:

  https://github.com/wincent/Command-T/issues/101
  https://bugzilla.redhat.com/show_bug.cgi?id=1130079
2014-08-22 11:01:56 -07:00
..
2014-05-23 11:29:49 -07:00
2014-05-16 13:15:07 -07:00
2014-05-16 13:15:07 -07:00
2014-05-16 13:15:07 -07:00
2014-05-16 13:15:07 -07:00
2014-05-16 13:15:07 -07:00
2014-05-16 13:15:07 -07:00

RubyWatchman

RubyWatchman is a gem that implements the Watchman binary protocol. It is implemented in C for speed, and is much faster than talking to Watchman using the JSON protocol.

Installation

Add this line to your application's Gemfile:

gem 'ruby-watchman'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ruby-watchman

Usage

This example shows:

  1. Asking a running Watchman process for its local socket
  2. Checking with Watchman whether a given path (in this case the current working directory) is being watched
  3. Adding the path to the watch list if necessary
  4. Asking Watchman for the names of all files in the watch (files which exist, or have existed since watching started), then printing them
require 'ruby-watchman'
require 'socket'
require 'pathname'

sockname = RubyWatchman.load(
  %x{watchman --output-encoding=bser get-sockname}
)['sockname']
raise unless $?.exitstatus.zero?

UNIXSocket.open(sockname) do |socket|
  root = Pathname.new('.').realpath.to_s
  roots = RubyWatchman.query(['watch-list'], socket)['roots']
  if !roots.include?(root)
    # this path isn't being watched yet; try to set up watch
    result = RubyWatchman.query(['watch', root], socket)

    # root_restrict_files setting may prevent Watchman from working
    raise if result.has_key?('error')
  end

  query = ['query', root, {
    'expression' => ['type', 'f'],
    'fields'     => ['name'],
  }]
  paths = RubyWatchman.query(query, socket)

  # could return error if watch is removed
  raise if paths.has_key?('error')

  p paths['files']
end