class Packwerk::Generators::ConfigurationFile

Constants

CONFIGURATION_TEMPLATE_FILE_PATH

Public Class Methods

generate(load_paths:, root:, out:) click to toggle source
# File lib/packwerk/generators/configuration_file.rb, line 14
def generate(load_paths:, root:, out:)
  new(load_paths: load_paths, root: root, out: out).generate
end
new(load_paths:, root:, out: $stdout) click to toggle source
# File lib/packwerk/generators/configuration_file.rb, line 20
def initialize(load_paths:, root:, out: $stdout)
  @load_paths = load_paths
  @root = root
  @out = out

  set_template_variables
end

Public Instance Methods

generate() click to toggle source
# File lib/packwerk/generators/configuration_file.rb, line 29
def generate
  @out.puts("📦 Generating Packwerk configuration file...")
  default_config_path = File.join(@root, ::Packwerk::Configuration::DEFAULT_CONFIG_PATH)

  if File.exist?(default_config_path)
    @out.puts("⚠️  Packwerk configuration file already exists.")
    return true
  end

  File.write(default_config_path, render)

  @out.puts("✅ Packwerk configuration file generated in #{default_config_path}")
  true
end

Private Instance Methods

render() click to toggle source
# File lib/packwerk/generators/configuration_file.rb, line 58
def render
  ERB.new(template, trim_mode: "-").result(binding)
end
set_template_variables() click to toggle source
# File lib/packwerk/generators/configuration_file.rb, line 46
def set_template_variables
  @load_paths_formatted = if @load_paths.empty?
    "# load_paths:\n# - 'app/models'\n"
  else
    @load_paths.map { |path| "- #{path}\n" }.join
  end

  @load_paths_comment = unless @load_paths.empty?
    "# These load paths were auto generated by Packwerk.\nload_paths:\n"
  end
end
template() click to toggle source
# File lib/packwerk/generators/configuration_file.rb, line 62
def template
  template_file_path = File.join(__dir__, CONFIGURATION_TEMPLATE_FILE_PATH)
  File.read(template_file_path)
end