Skip to content

Commit fd88e47

Browse files
committed
Add local gem laoder helper using file watcher in rails and evn driven relaoding in the gem
1 parent c65ab08 commit fd88e47

23 files changed

Lines changed: 180 additions & 29 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
.envrc

my_namespace-my_gem/lib/my_namespace/my_gem.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

my_namespace-my_gem/lib/my_namespace/my_gem/version.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

my_rails_app/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ source "https://rubygems.org"
22

33
ruby "3.3.0"
44

5-
gem "my_namespace-my_gem", path: "../my_namespace-my_gem"
5+
gem "my_root_namespace-my_namespace-my_gem", path: "../my_root_namespace-my_namespace-my_gem"
66

77
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
88
gem "rails", "~> 7.1.3"

my_rails_app/Gemfile.lock

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
PATH
2-
remote: ../my_namespace-my_gem
2+
remote: ../my_root_namespace-my_namespace-my_gem
33
specs:
4-
my_namespace-my_gem (0.1.0)
4+
my_root_namespace-my_namespace-my_gem (0.1.0)
55
http
6+
zeitwerk
67

78
GEM
89
remote: https://rubygems.org/
@@ -269,7 +270,7 @@ DEPENDENCIES
269270
bootsnap
270271
debug
271272
importmap-rails
272-
my_namespace-my_gem!
273+
my_root_namespace-my_namespace-my_gem!
273274
propshaft
274275
puma (>= 5.0)
275276
rails (~> 7.1.3)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class DashboardController < ApplicationController
22
def show
3-
render inline: MyNamespace::MyGem.do_something
3+
render inline: MyRootNamespace::MyNamespace::MyGem.do_something
44
end
55
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class LocalGemReloading
2+
def self.setup_reloading_for_local_zeitwerk_gem(gem_name, local_gems_folder)
3+
gem_path = Pathname.new(File.join(local_gems_folder, gem_name, "lib"))
4+
puts gem_path
5+
parts = gem_name.split("-")
6+
gem_root = parts.delete(parts.last)
7+
gem_namespaces = parts.map(&:camelize)
8+
9+
puts gem_namespaces, gem_root
10+
loader_tag = "#{[gem_namespaces.join("::"), gem_root.underscore].join('-')}"
11+
puts loader_tag
12+
gem_loader = Zeitwerk::Registry.loaders.find { |loader| loader.tag == loader_tag }
13+
14+
file_watcher = ActiveSupport::FileUpdateChecker.new(gem_path.glob('**/*')) do
15+
gem_loader.reload
16+
end
17+
18+
# Plug it to Rails to be executed on each request
19+
Rails.application.reloaders << Class.new do
20+
def initialize(file_watcher)
21+
@file_watcher = file_watcher
22+
end
23+
24+
def updated?
25+
@file_watcher.execute_if_updated
26+
end
27+
end.new(file_watcher)
28+
true
29+
end
30+
end

my_rails_app/config/environments/development.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
11
require "active_support/core_ext/integer/time"
2+
require_relative "../../app/utils/local_gem_reloading"
3+
4+
def setup_reloading_for_local_zeitwerk_gem(gem_path)
5+
# gem_lib_path = gem_path.join("lib")
6+
# gem_loader = Zeitwerk::Loader.new
7+
# gem_loader.push_dir(gem_lib_path)
8+
puts Zeitwerk::Registry.loaders.inspect
9+
gem_loader = Zeitwerk::Registry.loaders.find{ |loader| loader.tag == "MyNamespace-my_gem" }
10+
11+
puts gem_loader.inspect
12+
13+
# gem_loader = Zeitwerk::Registry.loader_for(gem_lib_path)
14+
# gem_loader.enable_reloading
15+
# gem_loader.setup
16+
17+
file_watcher = ActiveSupport::FileUpdateChecker.new(gem_path.glob('**/*')) do
18+
gem_loader.reload
19+
end
20+
21+
# Plug it to Rails to be executed on each request
22+
Rails.application.reloaders << Class.new do
23+
def initialize(file_watcher)
24+
@file_watcher = file_watcher
25+
end
26+
27+
def updated?
28+
@file_watcher.execute_if_updated
29+
end
30+
end.new(file_watcher)
31+
end
32+
33+
# MyNamespace-my_gem
34+
35+
236

337
Rails.application.configure do
438
# Settings specified here will take precedence over those in config/application.rb.
@@ -71,4 +105,7 @@
71105

72106
# Raise error when a before_action's only/except options reference missing actions
73107
config.action_controller.raise_on_missing_callback_actions = true
108+
109+
LocalGemReloading.setup_reloading_for_local_zeitwerk_gem("my_root_namespace-my_namespace-my_gem", Rails.root.join("../"))
110+
# setup_reloading_for_local_zeitwerk_gem(Rails.root.join('../', "my_root_namspace-my_namespace-my_gem"))
74111
end
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)