Sha256: 535a541db6acff1255066b886a788cd5ffacabdd8abc3a2f36f98a6530ae5e3b

Contents?: true

Size: 1.74 KB

Versions: 16

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  # Raised when a configuration file is not found.
  class ConfigNotFoundError < StandardError
  end

  class ConfigLoader
    class << self
      def load_config
        config_path = ConfigFinder.find_project_dotfile

        if config_path
          load_yaml_configuration(config_path)
        else
          {}
        end
      end

      # Method from Rubocop::ConfigLoader
      def load_yaml_configuration(absolute_path)
        file_contents = read_file(absolute_path)

        hash = yaml_safe_load(file_contents, absolute_path) || {}

        # TODO: Print config if debug flag/option is set

        raise(TypeError, "Malformed configuration in #{absolute_path}") unless hash.is_a?(Hash)

        hash
      end

      # Read the specified file, or exit with a friendly, concise message on
      # stderr. Care is taken to use the standard OS exit code for a "file not
      # found" error.
      #
      # Method from Rubocop::ConfigLoader
      def read_file(absolute_path)
        File.read(absolute_path, encoding: Encoding::UTF_8)
      rescue Errno::ENOENT
        raise ConfigNotFoundError, "Configuration file not found: #{absolute_path}"
      end

      # Method from Rubocop::ConfigLoader
      def yaml_safe_load(yaml_code, filename)
        yaml_safe_load!(yaml_code, filename)
      rescue
        if defined?(::SafeYAML)
          raise "SafeYAML is unmaintained, no longer needed and should be removed"
        end

        raise
      end

      # Method from Rubocop::ConfigLoader
      def yaml_safe_load!(yaml_code, filename)
        YAML.safe_load(
          yaml_code, permitted_classes: [Regexp, Symbol], aliases: true, filename: filename, symbolize_names: true
        )
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
annotaterb-4.14.0 lib/annotate_rb/config_loader.rb
annotaterb-4.13.0 lib/annotate_rb/config_loader.rb
annotaterb-4.12.0 lib/annotate_rb/config_loader.rb
annotaterb-4.11.0 lib/annotate_rb/config_loader.rb
annotaterb-4.10.2 lib/annotate_rb/config_loader.rb
annotaterb-4.10.1 lib/annotate_rb/config_loader.rb
annotaterb-4.10.0 lib/annotate_rb/config_loader.rb
annotaterb-4.9.0 lib/annotate_rb/config_loader.rb
annotaterb-4.7.0 lib/annotate_rb/config_loader.rb
annotaterb-4.6.0 lib/annotate_rb/config_loader.rb
annotaterb-4.5.0 lib/annotate_rb/config_loader.rb
annotaterb-4.4.1 lib/annotate_rb/config_loader.rb
annotaterb-4.4.0 lib/annotate_rb/config_loader.rb
annotaterb-4.3.1 lib/annotate_rb/config_loader.rb
annotaterb-4.3.0 lib/annotate_rb/config_loader.rb
annotaterb-4.2.0 lib/annotate_rb/config_loader.rb