Class: CommandT::Scanner::FileScanner::WatchmanFileScanner

Inherits:
FindFileScanner show all
Defined in:
lib/command-t/scanner/file_scanner/watchman_file_scanner.rb

Overview

A FileScanner which delegates the heavy lifting to Watchman (github.com/facebook/watchman); useful for very large hierarchies.

Inherits from FindFileScanner so that it can fall back to it in the event that Watchman isn’t available or able to fulfil the request.

Constant Summary collapse

WatchmanError =

Exception raised when Watchman is unavailable or unable to process the requested path.

Class.new(::RuntimeError)

Constants inherited from CommandT::Scanner::FileScanner

FileLimitExceeded

Instance Attribute Summary

Attributes inherited from CommandT::Scanner::FileScanner

#path

Instance Method Summary collapse

Methods inherited from CommandT::Scanner::FileScanner

#flush, #initialize, #paths

Methods inherited from CommandT::Scanner

#paths

Constructor Details

This class inherits a constructor from CommandT::Scanner::FileScanner

Instance Method Details

#paths!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/command-t/scanner/file_scanner/watchman_file_scanner.rb', line 20

def paths!
  sockname = extract_value(
    Watchman::Utils.load(get_raw_sockname),
    'sockname'
  )

  UNIXSocket.open(sockname) do |socket|
    root = Pathname.new(@path).realpath.to_s
    # Use `watch-project` for efficiency if available.
    if use_watch_project?
        result = Watchman::Utils.query(['watch-project', root], socket)
        root = extract_value(result, 'watch')
        relative_root = extract_value(result, 'relative_path') if result.has_key?('relative_path')
    else
      roots = Watchman::Utils.query(['watch-list'], socket)['roots']
      if !roots.include?(root)
        # This path isn't being watched yet; try to set up watch.
        result = Watchman::Utils.query(['watch', root], socket)

        # `root_restrict_files` setting may prevent Watchman from
        # working or enforce_root_files/root_files (>= version 3.1).
        extract_value(result)
      end
    end

    query_params = {
      'expression' => ['type', 'f'],
      'fields'     => ['name'],
    }
    query_params['relative_root'] = relative_root if relative_root
    query = ['query', root, query_params]
    paths = Watchman::Utils.query(query, socket)

    # could return error if watch is removed
    extracted = extract_value(paths, 'files')
    if @wildignore
      extracted.select { |path| path !~ @wildignore }
    else
      extracted
    end
  end
rescue Errno::ENOENT, WatchmanError
  # watchman executable not present, or unable to fulfil request
  super
end