Class: Jsus::Util::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/jsus/util/watcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_dirs, ignored_dirs = [], &callback) ⇒ Watcher

Instantiates a FSSM monitor and starts watching. Consider using class method Jsus::Util::Watcher.watch instead.

See Also:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jsus/util/watcher.rb', line 21

def initialize(input_dirs, ignored_dirs = [], &callback)
  require 'fssm'
  @callback = callback
  input_dirs = Array(input_dirs).compact
  @semaphore = Mutex.new
  watcher = self
  @ignored_dirs = Array(ignored_dirs).map {|dir| File.expand_path(dir) }
  FSSM.monitor do
    input_dirs.each do |dir|
      dir = File.expand_path(dir)
      path(dir) do
        glob ["**/*.js", "**/package.yml", "**/package.json"]
        create &watcher.method(:watch_callback)
        update &watcher.method(:watch_callback)
        delete &watcher.method(:watch_callback)
      end
    end
  end

rescue LoadError => e
  Jsus.logger.error "You need to install fssm gem for --watch option."
  Jsus.logger.error "You may also want to install rb-fsevent for OS X" if RUBY_PLATFORM =~ /darwin/
  raise e
end

Class Method Details

.watch(input_dirs, ignored_dirs = []) {|filename| ... } ⇒ FSSM::Monitor

Watches input directories and their subdirectories for changes in js source files and package metadata files.

Parameters:

  • input_dirs (String, Array)

    directory or directories to watch

  • ignored_dirs (String, Array) (defaults to: [])

    directory or directories to ignore

Yields:

  • (filename)

    Callback to trigger on creation / update / removal of any file in given directories

Yield Parameters:

  • filename (String)

    Updated filename full path

Returns:

  • (FSSM::Monitor)

    fssm monitor instance



12
13
14
# File 'lib/jsus/util/watcher.rb', line 12

def watch(input_dirs, ignored_dirs = [], &callback)
  new(input_dirs, ignored_dirs, &callback)
end

Instance Method Details

#runObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jsus/util/watcher.rb', line 62

def run
  if @semaphore.try_lock
    begin
      yield
    rescue Exception => e
      Jsus.logger.error "Exception happened during watching: #{e}, #{e.inspect}"
      Jsus.logger.error "\t#{e.backtrace.join("\n\t")}" if Jsus.verbose?
      Jsus.logger.error "Compilation FAILED."
    ensure
      @semaphore.unlock
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/jsus/util/watcher.rb', line 78

def running?
  @semaphore.locked?
end

#watch_callback(base, match) ⇒ Object

Note:

Defers the processing to a separate thread and ignores all the incoming events received during the processing.

Default callback for the FSSM watcher.

Parameters:

  • base (String)

    base part of filename

  • match (String)

    matched part of filename



52
53
54
55
56
57
58
59
# File 'lib/jsus/util/watcher.rb', line 52

def watch_callback(base, match)
  Thread.new do
    full_path = File.join(base, match)
    unless @ignored_dirs.any? {|dir| full_path.index(dir) == 0 }
      run { @callback.call(full_path) }
    end
  end
end