Skip to content

Commit 853ef60

Browse files
improve hot reloading
1 parent 4fd4b1c commit 853ef60

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/plutonium/packaging/app.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ def scoped_to_entity?
2121
scoped_entity_class.present?
2222
end
2323

24+
def initialize_register!
25+
# this exists solely to support hot reloads
26+
# if the user has modified the register especially if they removed a registration, we have no way of telling
27+
# so instead we start over
28+
@resource_register = []
29+
end
30+
2431
def register_resource(resource)
25-
@resource_register ||= []
26-
@resource_register << resource
32+
@resource_register.append resource
2733
end
2834

2935
def resource_register

lib/plutonium/reactor/engine.rb

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,31 @@ module Engine
1313
next unless Plutonium::Config.reload_files
1414

1515
@listener ||= begin
16-
require 'listen'
16+
require "listen"
1717

18-
listener = Listen.to(Plutonium.lib_root.to_s, only: /\.rb$/) do |modified, added, removed|
19-
(modified + added).each { |f| load f}
18+
plutonium_lib_dir = Plutonium.lib_root.to_s
19+
packages_dir = Rails.root.join("packages/").to_s
20+
listener = Listen.to(plutonium_lib_dir, packages_dir, only: /\.rb$/) do |modified, added, removed|
21+
(modified + added).each do |file|
22+
if file.starts_with?(packages_dir)
23+
# if package file was added, ignore it
24+
# otherwise rails gets mad at us since engines cannot be loaded after initial boot
25+
# TODO: check if guard has apis to control reloading dynamically
26+
next if added.include? file
27+
28+
case File.basename(file)
29+
when "engine.rb"
30+
# reload engines. due to how we load packages, rails does not support
31+
load file
32+
# reload routes to pick up any registration changes
33+
Rails.application.reload_routes!
34+
else
35+
# non engine package files are reloaded by rails automatically
36+
end
37+
else
38+
load file
39+
end
40+
end
2041
end
2142
listener.start
2243
listener

0 commit comments

Comments
 (0)