Class: LogicaCompiler::Watcher

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

Constant Summary collapse

DEFAULT_CONFIG_PATH =
"logica/config.yml"
PROGRAMS_DIR =
"logica/programs"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path: DEFAULT_CONFIG_PATH, config_loader: Config.method(:load!), compiler_factory: ->(config) { Compiler.new(config:) }, listener_factory: nil, logger: nil, sleeper: Deps::Sleeper.new, production_check: nil) ⇒ Watcher

Returns a new instance of Watcher.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/logica_compiler/watcher.rb', line 14

def initialize(
  config_path: DEFAULT_CONFIG_PATH,
  config_loader: Config.method(:load!),
  compiler_factory: ->(config) { Compiler.new(config:) },
  listener_factory: nil,
  logger: nil,
  sleeper: Deps::Sleeper.new,
  production_check: nil
)
  @config_path = config_path
  @config_loader = config_loader
  @compiler_factory = compiler_factory
  @listener_factory = listener_factory
  @logger = logger
  @sleeper = sleeper
  @production_check = production_check || method(:production?)
end

Class Method Details

.start!(config_path: DEFAULT_CONFIG_PATH, **kwargs) ⇒ Object



10
11
12
# File 'lib/logica_compiler/watcher.rb', line 10

def self.start!(config_path: DEFAULT_CONFIG_PATH, **kwargs)
  new(config_path:, **kwargs).start!
end

Instance Method Details

#start!Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logica_compiler/watcher.rb', line 32

def start!
  raise WatcherError, "logica watch is for development only" if @production_check.call

  config = @config_loader.call(@config_path)
  compiler = @compiler_factory.call(config)

  root = config.root.join(PROGRAMS_DIR).to_s
  listener = build_listener(root, compiler)

  info "[logica] watching #{root}..."
  listener.start
  @sleeper.sleep
end