Class: Cmd::Add::Autoload

Inherits:
Dry::CLI::Command
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/cmd/add/autoload.rb

Defined Under Namespace

Classes: DepsType

Constant Summary collapse

ZEITWERK_TEMPLATE =
<<~CONTENT
  # frozen_string_literal: true

  require 'zeitwerk'
  require 'pathname'

  root = Pathname('.')

  loader = Zeitwerk::Loader.new

  loader.push_dir(root.join('lib').realpath)
  loader.push_dir(root.join('config').realpath)

  loader.setup
CONTENT
DRY_SYSTEM_TEMPLATE =
<<~CONTENT
  # frozen_string_literal: true

  require 'dry/system'

  # Application configure all the paths to be autoloaded and auto injectable.
  class Application < Dry::System::Container
    configure do |config|
      config.root = Pathname('.')

      config.component_dirs.loader = Dry::System::Loader::Autoloading

      config.component_dirs.add 'lib'
      config.component_dirs.add 'config'
    end
  end

  loader = Zeitwerk::Loader.new
  loader.push_dir(Application.config.root.join('lib').realpath)
  loader.push_dir(Application.config.root.join('config').realpath)
  loader.setup
CONTENT

Instance Method Summary collapse

Instance Method Details

#call(dry_system:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cmd/add/autoload.rb', line 63

def call(dry_system:)
  return puts 'Could not locate Gemfile' unless Utils::Gem.gemfile_exist?

  Utils::FileSystem.create_directory_if_not_exist('config')

  if dry_system
    File.write('config/application.rb', DRY_SYSTEM_TEMPLATE)

    install_deps(DepsType::DRY_SYSTEM)
  end

  return if dry_system

  File.write('config/application.rb', ZEITWERK_TEMPLATE)

  install_deps(DepsType::ZEITWERK)
end