# frozen_string_literal: true require "yaml" require "pathname" module Herb class Configuration OPTIONS_PATH = File.expand_path("../../config/options.yml", __dir__ || __FILE__).freeze #: String OPTIONS = YAML.safe_load_file(OPTIONS_PATH).freeze #: Hash[String, untyped] VALID_FRAMEWORKS = OPTIONS["framework"]["values"].freeze #: Array[String] VALID_TEMPLATE_ENGINES = OPTIONS["template_engine"]["values"].freeze #: Array[String] CONFIG_FILENAMES = [".herb.yml"].freeze #: Array[String] MISNAMED_CONFIG_FILENAMES = [".herb.yaml", "herb.yml", "herb.yaml"].freeze #: Array[String] PROJECT_INDICATORS = [ ".git", ".herb", ".herb.yml", "Gemfile", "package.json", "Rakefile", "README.md", "*.gemspec", "config/application.rb" ].freeze #: Array[String] DEFAULTS_PATH = File.expand_path("defaults.yml", __dir__ || __FILE__).freeze DEFAULTS = YAML.safe_load_file(DEFAULTS_PATH).freeze GLOB_CHARACTERS = /[*?\[\]{}]/ #: Regexp attr_reader :config, :user_config, :config_path, :project_root, :misnamed_config_paths def initialize(project_path = nil) @start_path = project_path ? Pathname.new(project_path) : Pathname.pwd @config_path, @project_root = find_config_file @misnamed_config_paths = find_misnamed_config_paths @user_config = load_user_config @config = deep_merge(DEFAULTS, @user_config) warn_about_misnamed_config_files end def [](key) @config[key.to_s] end def dig(*keys) @config.dig(*keys.map(&:to_s)) end def version @config["version"] end #: () -> String def framework value = @config["framework"] || "ruby" unless VALID_FRAMEWORKS.include?(value) warn "[Herb] Unknown framework: #{value.inspect}. Valid values: #{VALID_FRAMEWORKS.join(", ")}. Defaulting to 'ruby'." return "ruby" end value end #: () -> String def template_engine value = @config["template_engine"] || "erubi" unless VALID_TEMPLATE_ENGINES.include?(value) warn "[Herb] Unknown template_engine: #{value.inspect}. Valid values: #{VALID_TEMPLATE_ENGINES.join(", ")}. Defaulting to 'erubi'." return "erubi" end value end def files @config["files"] || {} end def file_include_patterns files["include"] || DEFAULTS.dig("files", "include") || [] end def file_exclude_patterns files["exclude"] || DEFAULTS.dig("files", "exclude") || [] end def parser @config["parser"] || {} end #: () -> Array[String] def erb_openers parser["erb_openers"] || [] end #: () -> String? def ruby_version parser["ruby_version"] end #: () -> Hash[Symbol, untyped] def parser_options options = {} #: Hash[Symbol, untyped] openers = erb_openers version = ruby_version options[:erb_openers] = openers unless openers.empty? options[:ruby_version] = version if version options end def linter @config["linter"] || {} end def engine @config["engine"] || {} end #: (String, untyped) -> untyped def engine_option(key, default = nil) engine.fetch(key.to_s, default) end def enabled_validators(overrides = {}) config = dig("engine", "validators") || {} { security: config.fetch("security", true), nesting: config.fetch("nesting", true), accessibility: config.fetch("accessibility", true), generator_template: config.fetch("generator_template", true), }.merge( overrides.to_h { |key, value| [key.to_sym, !!value] } ) end def formatter @config["formatter"] || {} end def include_patterns_for(tool) tool_config = send(tool.to_s) file_include_patterns + (tool_config["include"] || []) end def exclude_patterns_for(tool) tool_config = send(tool.to_s) file_exclude_patterns + (tool_config["exclude"] || []) end def linter_include_patterns include_patterns_for(:linter) end def linter_exclude_patterns exclude_patterns_for(:linter) end def formatter_include_patterns include_patterns_for(:formatter) end def formatter_exclude_patterns exclude_patterns_for(:formatter) end def enabled_for_path?(path, tool) !path_excluded?(path, exclude_patterns_for(tool), include_patterns_for(tool)) end def linter_enabled_for_path?(path) enabled_for_path?(path, :linter) end def formatter_enabled_for_path?(path) enabled_for_path?(path, :formatter) end def path_excluded?(path, patterns, include_patterns = []) matching_excludes = patterns.select { |pattern| File.fnmatch?(pattern, path, File::FNM_PATHNAME) } return false if matching_excludes.empty? matching_includes = include_patterns.select { |pattern| File.fnmatch?(pattern, path, File::FNM_PATHNAME) } return true if matching_includes.empty? matching_excludes.any? do |exclude_pattern| matching_includes.none? { |include_pattern| include_overrides_exclude?(include_pattern, exclude_pattern) } end end def path_included?(path, patterns) patterns.any? { |pattern| File.fnmatch?(pattern, path, File::FNM_PATHNAME) } end def include_overrides_exclude?(include_pattern, exclude_pattern) exclude_prefix = literal_prefix_segments(exclude_pattern) return false if exclude_prefix.empty? include_prefix = literal_prefix_segments(include_pattern) include_prefix.length >= exclude_prefix.length && include_prefix.first(exclude_prefix.length) == exclude_prefix end def literal_prefix_segments(pattern) pattern.split("/").take_while { |segment| !segment.match?(GLOB_CHARACTERS) } end def find_files(search_path = nil) collect_files(search_path, file_include_patterns, file_exclude_patterns) end def find_files_for_tool(tool, search_path = nil) collect_files(search_path, include_patterns_for(tool), exclude_patterns_for(tool)) end def collect_files(search_path, include_patterns, exclude_patterns) search_path ||= @project_root || @start_path expanded_path = File.expand_path(search_path.to_s) all_files = include_patterns.flat_map { |pattern| Dir[File.join(expanded_path, pattern)] }.uniq all_files.reject do |file| path_excluded?(file.sub("#{expanded_path}/", ""), exclude_patterns, include_patterns) end.sort end def find_files_for_linter(search_path = nil) find_files_for_tool(:linter, search_path) end def find_files_for_formatter(search_path = nil) find_files_for_tool(:formatter, search_path) end class << self def load(project_path = nil) new(project_path) end def default @default ||= new end def default_file_patterns DEFAULTS.dig("files", "include") || [] end def default_exclude_patterns DEFAULTS.dig("files", "exclude") || [] end end private def find_config_file search_path = @start_path search_path = search_path.parent if search_path.file? while search_path.to_s != "/" CONFIG_FILENAMES.each do |filename| config_file = search_path / filename return [config_file, search_path] if config_file.exist? end return [nil, search_path] if project_root?(search_path) search_path = search_path.parent end [nil, @start_path] end def find_misnamed_config_paths search_path = @project_root || @start_path search_path = search_path.parent if search_path.file? MISNAMED_CONFIG_FILENAMES.map { |filename| search_path / filename }.select(&:exist?) end def warn_about_misnamed_config_files @misnamed_config_paths.each do |misnamed_path| warn "[Herb] Ignoring #{misnamed_path}: Herb only reads `#{CONFIG_FILENAMES.first}`. " \ "Rename it to `#{CONFIG_FILENAMES.first}` to apply it." end end def project_root?(path) PROJECT_INDICATORS.any? do |indicator| if indicator.include?("*") Dir.glob(path / indicator).any? else (path / indicator).exist? end end end def load_user_config return {} unless @config_path&.exist? begin YAML.safe_load_file(@config_path, permitted_classes: [Symbol], aliases: true) || {} rescue Psych::SyntaxError => e warn "Warning: Invalid YAML in #{@config_path}: #{e.message}" {} end end def deep_merge(base, override, additive_keys: ["include", "exclude"]) base.merge(override) do |key, old_value, new_value| if old_value.is_a?(Hash) && new_value.is_a?(Hash) deep_merge(old_value, new_value, additive_keys: additive_keys) elsif old_value.is_a?(Array) && new_value.is_a?(Array) && additive_keys.include?(key) old_value + new_value else new_value end end end end end