2024-04-16 00:47:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
require "bundler/setup"
|
|
|
|
Bundler.setup
|
|
|
|
|
|
|
|
require "active_support"
|
|
|
|
require "active_support/core_ext"
|
|
|
|
require "zeitwerk"
|
|
|
|
|
|
|
|
require_relative "converters"
|
2024-04-16 00:47:40 +08:00
|
|
|
|
|
|
|
module Migrations
|
|
|
|
def self.root_path
|
|
|
|
@root_path ||= File.expand_path("..", __dir__)
|
|
|
|
end
|
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
def self.load_rails_environment(quiet: false)
|
|
|
|
message = "Loading Rails environment ..."
|
|
|
|
print message unless quiet
|
2024-04-16 00:47:40 +08:00
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
rails_root = File.expand_path("../..", __dir__)
|
|
|
|
# rubocop:disable Discourse/NoChdir
|
|
|
|
Dir.chdir(rails_root) do
|
2024-04-16 00:47:40 +08:00
|
|
|
begin
|
2024-09-09 23:14:39 +08:00
|
|
|
require File.join(rails_root, "config/environment")
|
|
|
|
rescue LoadError => e
|
|
|
|
$stderr.puts e.message
|
|
|
|
raise
|
2024-04-16 00:47:40 +08:00
|
|
|
end
|
|
|
|
end
|
2024-09-09 23:14:39 +08:00
|
|
|
# rubocop:enable Discourse/NoChdir
|
|
|
|
|
|
|
|
print "\r"
|
|
|
|
print " " * message.length
|
|
|
|
print "\r"
|
2024-04-16 00:47:40 +08:00
|
|
|
end
|
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
def self.configure_zeitwerk
|
|
|
|
loader = Zeitwerk::Loader.new
|
|
|
|
loader.log! if ENV["DEBUG"]
|
2024-04-16 00:47:40 +08:00
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
loader.inflector.inflect(
|
|
|
|
{ "cli" => "CLI", "intermediate_db" => "IntermediateDB", "uploads_db" => "UploadsDB" },
|
|
|
|
)
|
2024-04-16 00:47:40 +08:00
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
loader.push_dir(File.join(::Migrations.root_path, "lib"), namespace: ::Migrations)
|
|
|
|
loader.push_dir(File.join(::Migrations.root_path, "lib", "common"), namespace: ::Migrations)
|
2024-04-16 00:47:40 +08:00
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
# All sub-directories of a converter should have the same namespace.
|
|
|
|
# Unfortunately `loader.collapse` doesn't work recursively.
|
|
|
|
Converters.all.each do |name, converter_path|
|
|
|
|
module_name = name.camelize.to_sym
|
|
|
|
namespace = ::Migrations::Converters.const_set(module_name, Module.new)
|
2024-04-16 00:47:40 +08:00
|
|
|
|
2024-09-09 23:14:39 +08:00
|
|
|
Dir[File.join(converter_path, "**", "*")].each do |subdirectory|
|
|
|
|
next unless File.directory?(subdirectory)
|
|
|
|
loader.push_dir(subdirectory, namespace: namespace)
|
|
|
|
end
|
2024-04-16 00:47:40 +08:00
|
|
|
end
|
2024-09-09 23:14:39 +08:00
|
|
|
|
2024-04-16 00:47:40 +08:00
|
|
|
loader.setup
|
|
|
|
end
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
def self.enable_i18n
|
|
|
|
require "i18n"
|
|
|
|
|
|
|
|
locale_glob = File.join(::Migrations.root_path, "config", "locales", "**", "migrations.*.yml")
|
|
|
|
I18n.load_path += Dir[locale_glob]
|
|
|
|
I18n.backend.load_translations
|
|
|
|
|
|
|
|
# always use English for now
|
|
|
|
I18n.default_locale = :en
|
|
|
|
I18n.locale = :en
|
|
|
|
end
|
2024-04-16 00:47:40 +08:00
|
|
|
end
|