class Djin::ConfigLoader

rubocop:disable Metrics/ClassLength

Constants

RESERVED_WORDS

Public Class Methods

load!(template_file_path, context_config: {}, base_directory: '.') click to toggle source
# File lib/djin/config_loader.rb, line 19
def self.load!(template_file_path, context_config: {}, base_directory: '.')
  new(template_file_path, context_config: context_config, base_directory: base_directory).load!
end
load_files!(*files, context_config: {}, base_directory: '.') click to toggle source
# File lib/djin/config_loader.rb, line 13
def self.load_files!(*files, context_config: {}, base_directory: '.')
  files.map do |file_path|
    ConfigLoader.load!(file_path, context_config: context_config, base_directory: base_directory)
  end&.reduce(:deep_merge)
end
new(template_file_path, context_config: {}, base_directory: '.') click to toggle source
# File lib/djin/config_loader.rb, line 23
def initialize(template_file_path, context_config: {}, base_directory: '.')
  @base_directory = Pathname.new(base_directory)
  @template_file = @base_directory.join(template_file_path)

  file_not_found!(@template_file) unless @template_file.exist?

  @template_file_content = Djin.cache.fetch(@template_file.realpath.to_s) { @template_file.read }
  @context_config = context_config
end

Public Instance Methods

load!() click to toggle source
# File lib/djin/config_loader.rb, line 33
def load!
  validate_version!
  validate_missing_config!

  file_config
end

Private Instance Methods

args() click to toggle source
# File lib/djin/config_loader.rb, line 125
def args
  index = ARGV.index('--')

  return [] unless index

  ARGV.slice((index + 1)..ARGV.size)
end
env() click to toggle source
# File lib/djin/config_loader.rb, line 133
def env
  @env ||= ENV.to_h.symbolize_keys
end
file_config() click to toggle source
# File lib/djin/config_loader.rb, line 42
def file_config
  MainConfig.new(
    djin_version: version,
    variables: variables,
    tasks: tasks,
    raw_tasks: raw_tasks,
    include_configs: @include_configs || []
  )
end
file_not_found!(filename, message = "File '%s' not found") click to toggle source
# File lib/djin/config_loader.rb, line 183
def file_not_found!(filename, message = "File '%s' not found")
  raise FileNotFoundError, message % filename
end
include_configs() click to toggle source
# File lib/djin/config_loader.rb, line 120
def include_configs
  @include_configs ||= Djin::IncludeConfigLoader.load!(raw_djin_config['include'],
                                                       base_directory: @template_file.dirname)
end
included_config() click to toggle source

TODO: Rename method

# File lib/djin/config_loader.rb, line 102
def included_config
  @included_config ||= begin
                         present_include_configs&.map do |present_include|
                           ConfigLoader.load!(present_include.file, base_directory: @template_file.dirname,
                                                                    # TODO: Rename to context_config
                                                                    context_config: present_include.context)
                         end&.reduce(:deep_merge)
                       end
end
included_raw_tasks() click to toggle source
# File lib/djin/config_loader.rb, line 95
def included_raw_tasks
  return {} unless included_config

  included_config.raw_tasks
end
included_tasks() click to toggle source
# File lib/djin/config_loader.rb, line 89
def included_tasks
  return {} unless included_config

  included_config.tasks
end
included_variables() click to toggle source
# File lib/djin/config_loader.rb, line 83
def included_variables
  return {} unless included_config

  included_config.variables
end
legacy_raw_tasks() click to toggle source
# File lib/djin/config_loader.rb, line 79
def legacy_raw_tasks
  raw_djin_config.except(*RESERVED_WORDS).reject { |task| task.start_with?('_') }
end
legacy_tasks() click to toggle source
# File lib/djin/config_loader.rb, line 69
def legacy_tasks
  Djin.warn_once(
    'Root tasks are deprecated and will be removed in Djin 1.0.0,' \
    ' put the tasks under \'tasks\' keyword',
    type: 'DEPRECATED'
  )

  rendered_djin_config.except(*RESERVED_WORDS).reject { |task| task.start_with?('_') }
end
missing_include_configs() click to toggle source
# File lib/djin/config_loader.rb, line 116
def missing_include_configs
  include_configs&.select(&:missing?)
end
present_include_configs() click to toggle source
# File lib/djin/config_loader.rb, line 112
def present_include_configs
  include_configs&.select(&:present?)
end
raw_djin_config() click to toggle source
# File lib/djin/config_loader.rb, line 137
def raw_djin_config
  @raw_djin_config ||= yaml_load(@template_file_content).deep_merge(@context_config)
rescue Psych::SyntaxError => e
  raise InvalidConfigFileError, "File: #{@template_file.realpath}\n  #{e.message}"
end
raw_tasks() click to toggle source
# File lib/djin/config_loader.rb, line 65
def raw_tasks
  included_raw_tasks.merge(raw_djin_config['tasks'] || legacy_raw_tasks)
end
rendered_djin_config() click to toggle source
# File lib/djin/config_loader.rb, line 143
def rendered_djin_config
  @rendered_djin_config ||= begin
                              locals = env.merge(variables)

                              rendered_yaml = Mustache.render(@template_file_content,
                                                              args: args.join(' '),
                                                              args?: args.any?,
                                                              **locals)
                              yaml_load(rendered_yaml).merge(@context_config)
                            end
end
tasks() click to toggle source
# File lib/djin/config_loader.rb, line 61
def tasks
  included_tasks.merge(rendered_djin_config['tasks'] || legacy_tasks)
end
validate_missing_config!() click to toggle source
# File lib/djin/config_loader.rb, line 167
def validate_missing_config!
  missing_include_configs.each do |ic|
    file_not_found!(ic.full_path) if ic.type == :local

    missing_file_remote_error = "#{ic.git} exists but is missing %s," \
      'if the file exists in upstream run djin remote-config fetch to fix'

    file_not_found!(ic.full_path, missing_file_remote_error) if ic.type == :remote && ic.repository_fetched?

    if ic.type == :remote
      Djin.warn_once "Missing #{ic.git} with version '#{ic.version}', " \
        'run `djin remote-config fetch` to fetch the config'
    end
  end
end
validate_version!() click to toggle source
# File lib/djin/config_loader.rb, line 159
def validate_version!
  raise MissingVersionError, 'Missing djin_version' unless version

  return if file_config.version_supported?

  raise VersionNotSupportedError, "Version #{version} is not supported, use #{Djin::VERSION} or higher"
end
variables() click to toggle source
# File lib/djin/config_loader.rb, line 57
def variables
  @variables ||= included_variables.merge(raw_djin_config['variables']&.symbolize_keys || {})
end
version() click to toggle source
# File lib/djin/config_loader.rb, line 52
def version
  # TODO: Deprecates djin_version and use version instead
  @version || raw_djin_config['djin_version']
end
yaml_load(text) click to toggle source
# File lib/djin/config_loader.rb, line 155
def yaml_load(text)
  YAML.safe_load(text, [], [], true)
end