From 50719593a7882d4ae0540736a8592eaf122e7a75 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Mon, 14 Nov 2022 11:20:03 -0700 Subject: [PATCH] load project-specific configuration as well as user-specific so that you can configure a plugin per-project --- lib/yard/cli/yri.rb | 6 ++--- lib/yard/config.rb | 53 +++++++++++++++++++++++++++++++------------- lib/yard/registry.rb | 2 +- spec/config_spec.rb | 5 +++++ 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/lib/yard/cli/yri.rb b/lib/yard/cli/yri.rb index c6629cd97..81aa8397a 100644 --- a/lib/yard/cli/yri.rb +++ b/lib/yard/cli/yri.rb @@ -5,14 +5,14 @@ module YARD module CLI # A tool to view documentation in the console like `ri` class YRI < Command - # The location in {YARD::CONFIG_DIR} where the YRI cache file is loaded + # The location in {YARD::USER_CONFIG_DIR} where the YRI cache file is loaded # from. - CACHE_FILE = File.expand_path(File.join(YARD::Config::CONFIG_DIR, 'yri_cache')) + CACHE_FILE = File.expand_path(File.join(YARD::Config::USER_CONFIG_DIR, 'yri_cache')) # A file containing all paths, delimited by newlines, to search for # yardoc databases. # @since 0.5.1 - SEARCH_PATHS_FILE = File.expand_path(File.join(YARD::Config::CONFIG_DIR, 'yri_search_paths')) + SEARCH_PATHS_FILE = File.expand_path(File.join(YARD::Config::USER_CONFIG_DIR, 'yri_search_paths')) # Default search paths that should be loaded dynamically into YRI. These paths # take precedence over all other paths ({SEARCH_PATHS_FILE} and RubyGems diff --git a/lib/yard/config.rb b/lib/yard/config.rb index f1f7125c5..9dd013bf4 100644 --- a/lib/yard/config.rb +++ b/lib/yard/config.rb @@ -92,14 +92,17 @@ class << self end # The location where YARD stores user-specific settings - CONFIG_DIR = File.expand_path('~/.yard') + USER_CONFIG_DIR = File.expand_path('~/.yard') - # The main configuration YAML file. - CONFIG_FILE = File.join(CONFIG_DIR, 'config') + # @deprecated Use {USER_CONFIG_DIR} + CONFIG_DIR = USER_CONFIG_DIR + + # @deprecated Use {config_file}, passing the necessary directory + CONFIG_FILE = File.join(USER_CONFIG_DIR, 'config') # File listing all ignored plugins # @deprecated Set `ignored_plugins` in the {CONFIG_FILE} instead. - IGNORED_PLUGINS = File.join(CONFIG_DIR, 'ignored_plugins') + IGNORED_PLUGINS = File.join(USER_CONFIG_DIR, 'ignored_plugins') # Default configuration options DEFAULT_CONFIG_OPTIONS = { @@ -113,19 +116,36 @@ class << self # to allow it to be used as a plugin. YARD_PLUGIN_PREFIX = /^yard[-_]/ - # Loads settings from {CONFIG_FILE}. This method is called by YARD at - # load time and should not be called by the user. - # @return [void] def self.load self.options = SymbolHash.new(false) options.update(DEFAULT_CONFIG_OPTIONS) - options.update(read_config_file) + load_project_config_file + load_user_config_file load_commandline_safemode add_ignored_plugins_file translate_plugin_names load_plugins + end + + def self.config_file(dir) + File.join(dir, 'config') + end + + def self.load_project_config_file + load_config_file(config_file(".yard")) + end + + def self.load_user_config_file + load_config_file(config_file(USER_CONFIG_DIR)) + end + + # Loads settings from `config_file`. This method is called by YARD at + # load time and should not be called by the user. + # @return [void] + def self.load_config_file(config_file) + options.update(read_config_file(config_file)) rescue => e - log.error "Invalid configuration file, using default options." + log.error "Invalid configuration file #{config_file}, using default options." log.backtrace(e) options.update(DEFAULT_CONFIG_OPTIONS) end @@ -134,8 +154,8 @@ def self.load # @return [void] def self.save require 'yaml' - Dir.mkdir(CONFIG_DIR) unless File.directory?(CONFIG_DIR) - File.open(CONFIG_FILE, 'w') {|f| f.write(YAML.dump(options)) } + Dir.mkdir(USER_CONFIG_DIR) unless File.directory?(USER_CONFIG_DIR) + File.open(config_file(USER_CONFIG_DIR), 'w') {|f| f.write(YAML.dump(options)) } end # Loads gems that match the name 'yard-*' (recommended) or 'yard_*' except @@ -231,15 +251,16 @@ def self.translate_plugin_names end # Loads the YAML configuration file into memory + # @param [String] config_file the file to load # @return [Hash] the contents of the YAML file from disk - # @see CONFIG_FILE - def self.read_config_file - if File.file?(CONFIG_FILE) + # @see config_file + def self.read_config_file(config_file) + if File.file?(config_file) require 'yaml' if YAML.respond_to?(:safe_load_file) - YAML.safe_load_file(CONFIG_FILE, permitted_classes: [SymbolHash, Symbol]) + YAML.safe_load_file(config_file, permitted_classes: [SymbolHash, Symbol]) else - YAML.load_file(CONFIG_FILE) + YAML.load_file(config_file) end else {} diff --git a/lib/yard/registry.rb b/lib/yard/registry.rb index d70a442ea..3d624639d 100644 --- a/lib/yard/registry.rb +++ b/lib/yard/registry.rb @@ -31,7 +31,7 @@ module YARD # Registry.resolve(P('YARD::CodeObjects::Base'), '#docstring', true) module Registry DEFAULT_YARDOC_FILE = ".yardoc" - LOCAL_YARDOC_INDEX = File.expand_path(File.join(Config::CONFIG_DIR, 'gem_index')) + LOCAL_YARDOC_INDEX = File.expand_path(File.join(Config::USER_CONFIG_DIR, 'gem_index')) DEFAULT_PO_DIR = "po" extend Enumerable diff --git a/spec/config_spec.rb b/spec/config_spec.rb index b592ce4da..81752d936 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -2,6 +2,11 @@ require 'yaml' RSpec.describe YARD::Config do + before do + allow(File).to receive(:file?).with(".yard/config").and_return(false) + allow(File).to receive(:file?).with(".yardopts").and_return(false) + end + describe ".load" do before do expect(File).to receive(:file?).twice.with(CLI::Yardoc::DEFAULT_YARDOPTS_FILE).and_return(false)