Class: Gloss::Watcher

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

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ Watcher

Returns a new instance of Watcher.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gloss/watcher.rb', line 10

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

#killObject



74
75
76
77
78
79
80
# File 'lib/gloss/watcher.rb', line 74

def kill()
  Gloss.logger
.info("Interrupt signal received, shutting down")
  (if @listener
    @listener.stop
  end)
end

#watchObject



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
65
66
67
68
69
70
71
72
73
# File 'lib/gloss/watcher.rb', line 33

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