mirror of
https://github.com/ubicloud/ubicloud.git
synced 2023-08-22 09:38:31 +03:00
As it turns out, Clover was started almost on the same day Unreloader
got a release that implemented autoload functionality. Had it been
around a bit longer, maybe I would have done things like this to begin
with.
As it turns out, we had some issues with Zeitwerk being a bit too
clever -- or prescriptive -- about how it handled constants, and as a
result, code reloading didn't quite work right. Now, this does:
Unreloader.reload!
Also somewhat less idiomatic to Unreloader vs. Zeitwerk is setting up
autoload, and subsequently forcing them to load in production cases.
Doing things this way frees Clover code from having to require
dependencies within the project manually: otherwise, when
Unreloader.autoload is switched to a "require" when `autoload: false`
is passed, missing constant dependencies between files can crash. I
considered whether we should abide the old Ruby ways and maintain a
correct -- and, more difficult still, minimal -- require order
embedded into each file, but decided the Zeitwerk style seemed more
practical.
Alternatively, I considered removing Unreloader in favor of Zeitwerk,
but that seemed unappealing: the in-place reloading funcionality it
was designed for with hash_branch and Roda is too good to give up.
See https://github.com/jeremyevans/roda-sequel-stack/issues/21.
And Unreloader has some more advanced features that improve the
quality of reloading if one is willing to write bespoke code to help
Unreloader, which we are.
One piece of errata: Zeitwerk is good about figuring out if a
namespace should be a class or a module given files like this:
a/dir/foo.rb
a/dir.rb
In this case, Zeitwerk will notice that A::Dir should be a class, not
a module. The code I wrote here is not so smart: if the list of files
is presented with a nested constant first, it'll create modules to
contain it, even if later a class is found that should define the
namespace.
But right now, we don't have this ambiguity, so I figure we can solve
it later as necessary.
37 lines
881 B
Ruby
37 lines
881 B
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "loader"
|
|
|
|
run(Config.development? ? Unreloader : Clover.freeze.app)
|
|
|
|
freeze_core = false
|
|
# freeze_core = !dev # Uncomment to enable refrigerator
|
|
if freeze_core
|
|
begin
|
|
require "refrigerator"
|
|
rescue LoadError
|
|
else
|
|
require "tilt/sass" unless File.exist?(File.expand_path("../compiled_assets.json", __FILE__))
|
|
|
|
# When enabling refrigerator, you may need to load additional
|
|
# libraries before freezing the core to work correctly. You'll
|
|
# want to uncomment the appropriate lines below if you run into
|
|
# problems after enabling refrigerator.
|
|
|
|
# rackup -s webrick
|
|
# require 'forwardable'
|
|
# require 'webrick'
|
|
|
|
# rackup -s Puma
|
|
# require 'yaml'
|
|
# Gem.ruby
|
|
|
|
# Puma (needed for state file)
|
|
# require 'yaml'
|
|
|
|
# Unicorn (no changes needed)
|
|
|
|
Refrigerator.freeze_core
|
|
end
|
|
end
|