Class: Filewatcher

Inherits:
Object
  • Object
show all
Includes:
Cycles
Defined in:
lib/filewatcher.rb,
lib/filewatcher/env.rb,
lib/filewatcher/cycles.rb,
lib/filewatcher/runner.rb,
lib/filewatcher/version.rb

Overview

Simple file watcher. Detect changes in files and directories.

Issues: Currently doesn’t monitor changes in directorynames

Defined Under Namespace

Modules: Cycles Classes: Env, Runner

Constant Summary collapse

VERSION =
'1.1.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unexpanded_filenames, options = {}) ⇒ Filewatcher

Returns a new instance of Filewatcher.



20
21
22
23
24
25
26
27
28
29
# File 'lib/filewatcher.rb', line 20

def initialize(unexpanded_filenames, options = {})
  @unexpanded_filenames = unexpanded_filenames
  @unexpanded_excluded_filenames = options[:exclude]
  @keep_watching = false
  @pausing = false
  @immediate = options[:immediate]
  @show_spinner = options[:spinner]
  @interval = options.fetch(:interval, 0.5)
  @every = options[:every]
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



11
12
13
# File 'lib/filewatcher.rb', line 11

def interval
  @interval
end

#keep_watchingObject (readonly)

Returns the value of attribute keep_watching.



12
13
14
# File 'lib/filewatcher.rb', line 12

def keep_watching
  @keep_watching
end

Instance Method Details

#finalize(&on_update) ⇒ Object

Calls the update block repeatedly until all changes in the current snapshot are dealt with



75
76
77
78
79
80
81
82
# File 'lib/filewatcher.rb', line 75

def finalize(&on_update)
  on_update = @on_update unless block_given?
  while filesystem_updated?(@end_snapshot || mtime_snapshot)
    update_spinner('Finalizing')
    trigger_changes(on_update)
  end
  @end_snapshot = nil
end

#last_found_filenamesObject



84
85
86
# File 'lib/filewatcher.rb', line 84

def last_found_filenames
  last_snapshot.keys
end

#pauseObject



48
49
50
51
52
53
# File 'lib/filewatcher.rb', line 48

def pause
  @pausing = true
  update_spinner('Initiating pause')
  # Ensure we wait long enough to enter pause loop in #watch
  sleep @interval
end

#resumeObject



55
56
57
58
59
60
61
62
63
# File 'lib/filewatcher.rb', line 55

def resume
  if !@keep_watching || !@pausing
    raise "Can't resume unless #watch and #pause were first called"
  end
  @last_snapshot = mtime_snapshot # resume with fresh snapshot
  @pausing = false
  update_spinner('Resuming')
  sleep @interval # Wait long enough to exit pause loop in #watch
end

#stopObject

Ends the watch, allowing any remaining changes to be finalized. Used mainly in multi-threaded situations.



67
68
69
70
71
# File 'lib/filewatcher.rb', line 67

def stop
  @keep_watching = false
  update_spinner('Stopping')
  nil
end

#update_spinner(label) ⇒ Object



14
15
16
17
18
# File 'lib/filewatcher.rb', line 14

def update_spinner(label)
  return unless @show_spinner
  @spinner ||= %w[\\ | / -]
  print "#{' ' * 30}\r#{label}  #{@spinner.rotate!.first}\r"
end

#watch {|'', ''| ... } ⇒ Object

Yields:

  • ('', '')


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/filewatcher.rb', line 31

def watch(&on_update)
  ## The set of available signals depends on the OS
  ## Windows doesn't support `HUP` signal, for example
  (%w[HUP INT TERM] & Signal.list.keys).each do |signal|
    trap(signal) { exit }
  end

  @on_update = on_update
  @keep_watching = true
  yield('', '') if @immediate

  main_cycle

  @end_snapshot = mtime_snapshot
  finalize(&on_update)
end