Class: Gloss::Watcher
- Inherits:
-
Object
- Object
- Gloss::Watcher
- Defined in:
- lib/gloss/watcher.rb
Instance Method Summary collapse
-
#initialize(paths) ⇒ Watcher
constructor
A new instance of Watcher.
- #kill ⇒ Object
- #watch ⇒ Object
Constructor Details
#initialize(paths) ⇒ Watcher
Returns a new instance of Watcher.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gloss/watcher.rb', line 11 def initialize(paths) @paths = paths (if @paths.empty? @paths = [File.join(Dir.pwd, Config.src_dir)] @only = /(?:(\.gl|(?:(?<=\/)[^\.\/]+))\z|\A[^\.\/]+\z)/ else file_names = Array.new paths = Array.new @paths.each() { |pa| pn = Pathname.new(pa) paths.<<(pn.parent .to_s) file_names.<<((if pn.file? pn.basename .to_s else pa end)) } @paths = paths.uniq @only = /#{Regexp.union(file_names)}/ end) end |
Instance Method Details
#kill ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/gloss/watcher.rb', line 75 def kill() Gloss.logger .info("Interrupt signal received, shutting down") (if @listener @listener.stop end) end |
#watch ⇒ Object
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 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gloss/watcher.rb', line 34 def watch() Gloss.logger .info("Now listening for changes in #{@paths.join(", ")}") @listener ||= Listen.to(*@paths, latency: 2, only: @only) { |modified, added, removed| modified.+(added) .each() { |f| Gloss.logger .info("Rewriting #{f}") content = File.read(f) err = catch(:"error") { || Writer.new(Visitor.new(Parser.new(content) .run) .run, f) .run nil } (if err Gloss.logger .error(err) else Gloss.logger .info("Done") end) } removed.each() { |f| out_path = Utils.src_path_to_output_path(f) Gloss.logger .info("Removing #{out_path}") (if File.exist?(out_path) File.delete(out_path) end) Gloss.logger .info("Done") } } begin @listener.start sleep rescue Interrupt kill end end |