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.



9
10
11
12
13
14
# File 'lib/gloss/watcher.rb', line 9

def initialize(paths)
  @paths = paths
  (if @paths.empty?
    @paths = [File.join(Dir.pwd, Config.src_dir)]
  end)
end

Instance Method Details

#watchObject



15
16
17
18
19
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
# File 'lib/gloss/watcher.rb', line 15

def watch()
  puts("=====> Now listening for changes in #{@paths.join(", ")}")
  listener = Listen.to(*@paths, latency: 2) { |modified, added, removed|
    modified.+(added)
.each() { |f|
      unless           f.end_with?(".gl")
        next
      end
      puts("====> Rewriting #{f}")
      content = File.read(f)
      Writer.new(Builder.new(Parser.new(content)
.run)
.run, f)
.run
      puts("====> Done")
    }
    removed.each() { |f|
      unless           f.end_with?(".gl")
        next
      end
      out_path = Utils.src_path_to_output_path(f)
      puts("====> Removing #{out_path}")
      (if File.exist?(out_path)
        File.delete(out_path)
      end)
      puts("====> Done")
    }
  }
  listener.start
  begin
    loop() { ||
      sleep(10)
    }
  rescue Interrupt
    puts("=====> Interrupt signal received, shutting down")
    exit(0)
  end
end