Sha256: 645569c48d43defe9da0337b91636865b5df67e948051be1046161e2a2b8560b

Contents?: true

Size: 1.93 KB

Versions: 18

Compression:

Stored size: 1.93 KB

Contents

module Nanoc::Int
  # @api private
  class ConfigLoader
    class NoConfigFileFoundError < ::Nanoc::Error
      def initialize
        super('No configuration file found')
      end
    end

    class NoParentConfigFileFoundError < ::Nanoc::Error
      def initialize(filename)
        super("There is no parent configuration file at #{filename}")
      end
    end

    class CyclicalConfigFileError < ::Nanoc::Error
      def initialize(filename)
        super("The parent configuration file at #{filename} includes one of its descendants")
      end
    end

    # @return [Boolean]
    def self.cwd_is_nanoc_site?
      !config_filename_for_cwd.nil?
    end

    # @return [String]
    def self.config_filename_for_cwd
      filenames = %w( nanoc.yaml config.yaml )
      candidate = filenames.find { |f| File.file?(f) }
      candidate && File.expand_path(candidate)
    end

    def new_from_cwd
      # Determine path
      filename = self.class.config_filename_for_cwd
      raise NoConfigFileFoundError if filename.nil?

      # Read
      apply_parent_config(
        Nanoc::Int::Configuration.new(YAML.load_file(filename)),
        [filename]).with_defaults
    end

    # @api private
    def apply_parent_config(config, processed_paths = [])
      parent_path = config[:parent_config_file]
      return config if parent_path.nil?

      # Get absolute path
      parent_path = File.absolute_path(parent_path, File.dirname(processed_paths.last))
      unless File.file?(parent_path)
        raise NoParentConfigFileFoundError.new(parent_path)
      end

      # Check recursion
      if processed_paths.include?(parent_path)
        raise CyclicalConfigFileError.new(parent_path)
      end

      # Load
      parent_config = Nanoc::Int::Configuration.new(YAML.load_file(parent_path))
      full_parent_config = apply_parent_config(parent_config, processed_paths + [parent_path])
      full_parent_config.merge(config.without(:parent_config_file))
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
nanoc-4.1.6 lib/nanoc/base/repos/config_loader.rb
nanoc-4.2.0b1 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.5 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.4 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.3 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.2 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.1 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.0 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.0rc2 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.0rc1 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.0b1 lib/nanoc/base/repos/config_loader.rb
nanoc-4.1.0a1 lib/nanoc/base/repos/config_loader.rb
nanoc-4.0.2 lib/nanoc/base/repos/config_loader.rb
nanoc-4.0.1 lib/nanoc/base/repos/config_loader.rb
nanoc-4.0.0 lib/nanoc/base/repos/config_loader.rb
nanoc-4.0.0rc3 lib/nanoc/base/repos/config_loader.rb
nanoc-4.0.0rc2 lib/nanoc/base/repos/config_loader.rb
nanoc-4.0.0rc1 lib/nanoc/base/repos/config_loader.rb