Class: DevboxLauncher::Watchman

Inherits:
Object
  • Object
show all
Defined in:
lib/devbox_launcher/watchman.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir:) ⇒ Watchman

Returns a new instance of Watchman.



6
7
8
# File 'lib/devbox_launcher/watchman.rb', line 6

def initialize(dir:)
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



4
5
6
# File 'lib/devbox_launcher/watchman.rb', line 4

def dir
  @dir
end

Instance Method Details

#socknameObject



37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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