2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-12-12 16:51:33 -05:00
|
|
|
require "active_support/core_ext/module/delegation"
|
|
|
|
|
2010-09-29 14:05:34 -04:00
|
|
|
module Rails
|
|
|
|
class Application
|
2011-12-12 08:39:22 -05:00
|
|
|
class RoutesReloader
|
2020-12-03 11:50:18 -05:00
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
2019-12-05 09:06:14 -05:00
|
|
|
attr_reader :route_sets, :paths, :external_routes
|
2020-12-03 18:01:53 -05:00
|
|
|
attr_accessor :eager_load
|
|
|
|
attr_writer :run_after_load_paths # :nodoc:
|
2018-06-04 13:56:36 -04:00
|
|
|
delegate :execute_if_updated, :execute, :updated?, to: :updater
|
2011-12-12 16:51:33 -05:00
|
|
|
|
2019-12-06 01:57:08 -05:00
|
|
|
def initialize
|
2012-06-29 11:39:50 -04:00
|
|
|
@paths = []
|
|
|
|
@route_sets = []
|
2019-12-05 09:06:14 -05:00
|
|
|
@external_routes = []
|
2017-01-11 08:50:42 -05:00
|
|
|
@eager_load = false
|
2010-09-29 14:05:34 -04:00
|
|
|
end
|
2010-10-02 11:45:26 -04:00
|
|
|
|
2010-09-29 14:05:34 -04:00
|
|
|
def reload!
|
|
|
|
clear!
|
|
|
|
load_paths
|
|
|
|
finalize!
|
2018-06-04 13:56:36 -04:00
|
|
|
route_sets.each(&:eager_load!) if eager_load
|
2010-09-29 14:05:34 -04:00
|
|
|
ensure
|
|
|
|
revert
|
|
|
|
end
|
|
|
|
|
2011-12-13 05:23:21 -05:00
|
|
|
private
|
|
|
|
def updater
|
2019-12-05 09:06:14 -05:00
|
|
|
@updater ||= begin
|
|
|
|
dirs = @external_routes.each_with_object({}) do |dir, hash|
|
|
|
|
hash[dir.to_s] = %w(rb)
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport::FileUpdateChecker.new(paths, dirs) { reload! }
|
|
|
|
end
|
2011-12-13 05:23:21 -05:00
|
|
|
end
|
2010-10-02 11:45:26 -04:00
|
|
|
|
2010-09-29 14:05:34 -04:00
|
|
|
def clear!
|
2010-10-06 09:54:28 -04:00
|
|
|
route_sets.each do |routes|
|
2010-09-29 14:05:34 -04:00
|
|
|
routes.disable_clear_and_finalize = true
|
|
|
|
routes.clear!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_paths
|
2020-12-03 18:01:53 -05:00
|
|
|
paths.each { |path| load(path) }
|
|
|
|
run_after_load_paths.call
|
2020-12-03 11:50:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_after_load_paths
|
2020-12-03 18:14:28 -05:00
|
|
|
@run_after_load_paths ||= -> { }
|
2010-09-29 14:05:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def finalize!
|
2014-10-27 12:28:53 -04:00
|
|
|
route_sets.each(&:finalize!)
|
2010-09-29 14:05:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def revert
|
2010-10-06 09:54:28 -04:00
|
|
|
route_sets.each do |routes|
|
2010-09-29 14:05:34 -04:00
|
|
|
routes.disable_clear_and_finalize = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|