Class: Stealth::Reloader

Inherits:
Object
  • Object
show all
Defined in:
lib/stealth/reloader.rb

Instance Method Summary collapse

Constructor Details

#initializeReloader

Returns a new instance of Reloader.



9
10
11
12
13
14
# File 'lib/stealth/reloader.rb', line 9

def initialize
  @reloader = Class.new(ActiveSupport::Reloader)
  @loader = Zeitwerk::Loader.new
  # @loader.logger = method(:puts)
  @loader
end

Instance Method Details

#callObject



75
76
77
78
79
80
# File 'lib/stealth/reloader.rb', line 75

def call
  @reloader.wrap do
    reload
    yield
  end
end

#enable_eager_load!Object



42
43
44
45
46
47
48
# File 'lib/stealth/reloader.rb', line 42

def enable_eager_load!
  if Stealth.config.eager_load
    @loader.setup
    @loader.eager_load
    Stealth::Logger.l(topic: 'stealth', message: 'Eager loading enabled.')
  end
end

#enable_reloading!Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/stealth/reloader.rb', line 50

def enable_reloading!
  if Stealth.config.hot_reload
    @checker = ActiveSupport::EventedFileUpdateChecker.new([], files_to_watch) do
      reload!
    end

    @loader.enable_reloading
    Stealth::Logger.l(topic: 'stealth', message: 'Hot reloading enabled.')
  end
end

#load_autoload_paths!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stealth/reloader.rb', line 23

def load_autoload_paths!
  if Stealth.config.autoload_paths.present?
    Stealth.config.autoload_paths.each do |autoload_path|
      @loader.push_dir(autoload_path)
    end

    # Bot-specific ignores
    Stealth.config.autoload_ignore_paths.each do |autoload_ignore_path|
      @loader.ignore(autoload_ignore_path)
    end

    # Ignore setup files
    @loader.ignore(File.join(Stealth.root, 'config', 'initializers'))
    @loader.ignore(File.join(Stealth.root, 'config', 'boot.rb'))
    @loader.ignore(File.join(Stealth.root, 'config', 'environment.rb'))
    @loader.ignore(File.join(Stealth.root, 'config', 'puma.rb'))
  end
end

#load_bot!Object



16
17
18
19
20
21
# File 'lib/stealth/reloader.rb', line 16

def load_bot!
  load_autoload_paths!
  enable_reloading!
  enable_eager_load!
  @loader.setup
end

#reloadObject

Only reloads if a change has been detected in one of the autoload files`



62
63
64
65
66
# File 'lib/stealth/reloader.rb', line 62

def reload
  if Stealth.config.hot_reload
    @checker.execute_if_updated
  end
end

#reload!Object

Force reloads reglardless of filesystem changes



69
70
71
72
73
# File 'lib/stealth/reloader.rb', line 69

def reload!
  if Stealth.config.hot_reload
    @loader.reload
  end
end