Class: YARD::Config
- Inherits:
-
Object
- Object
- YARD::Config
- Defined in:
- lib/yard/config.rb
Overview
This class maintains all system-wide configuration for YARD and handles the loading of plugins. To access options call Config.options, and to load a plugin use Config.load_plugin. All other public methods are used by YARD during load time.
User Configuration Files
Persistent user configuration files can be stored in the file ~/.yard/config, which is read when YARD first loads. The file should be formatted as YAML, and should contain a map of keys and values.
Although you can specify any key-value mapping in the configuration file, YARD defines special keys specified in DEFAULT_CONFIG_OPTIONS.
An example of a configuration file is listed below:
load_plugins: true # Auto-load plugins when YARD starts
ignored_plugins:
- yard-broken
- broken2 # yard- prefix not necessary
autoload_plugins:
- yard-rspec
Automatic Loading of Plugins
YARD 0.6.2 will no longer automatically load all plugins by default. This option can be reset by setting ‘load_plugins’ to true in the configuration file. In addition, you can specify a set of specific plugins to load on load through the ‘autoload_plugins’ list setting. This setting is independent of the ‘load_plugins’ value and will always be processed.
Ignored Plugins File
YARD 0.5 and below used a ~/.yard/ignored_plugins file to specify plugins to be ignored at load time. Ignored plugins in 0.6.2 and above should now be specified in the main configuration file, though YARD will support the ignored_plugins
file until 0.7.x.
Safe Mode
YARD supports running in safe-mode. By doing this, it will avoid executing any user code such as require files or queries. Plugins will still be loaded with safe mode on, because plugins are properly namespaced with a ‘yard-’ prefix, must be installed as a gem, and therefore cannot be touched by the user. To specify safe mode, use the safe_mode
key.
Plugin Specific Configuration
Additional settings can be defined within the configuration file specifically to provide configuration for a plugin. A plugin that utilizes the YARD configuration is strongly encouraged to utilize namespacing of their configuration content.
load_plugins: true # Auto-load plugins when YARD starts
ignored_plugins:
- yard-broken
- broken2 # yard- prefix not necessary
autoload_plugins:
- yard-rspec
# Plugin Specific Configuration
yard-sample-plugin:
show-results-inline: true
As the configuration is available system wide, it can be accessed within the plugin code.
if YARD::Config.['yard-sample-plugin'] and
YARD::Config.['yard-sample-plugin']['show-results-inline']
# ... perform the action that places the results inline ...
else
# ... do the default behavior of not showing the results inline ...
end
When accessing the configuration, be aware that this file is user managed so configuration keys and values may not be present. Make no assumptions and instead ensure that you check for the existence of keys before proceeding to retrieve values.
Constant Summary collapse
- CONFIG_DIR =
The location where YARD stores user-specific settings
File.('~/.yard')
- CONFIG_FILE =
The main configuration YAML file.
File.join(CONFIG_DIR, 'config')
- IGNORED_PLUGINS =
Deprecated.
Set ‘ignored_plugins` in the CONFIG_FILE instead.
File listing all ignored plugins
File.join(CONFIG_DIR, 'ignored_plugins')
- DEFAULT_CONFIG_OPTIONS =
Default configuration options
{ :load_plugins => false, # Whether to load plugins automatically with YARD :ignored_plugins => [], # A list of ignored plugins by name :autoload_plugins => [], # A list of plugins to be automatically loaded :safe_mode => false # Does not execute or eval any user-level code }
- YARD_PLUGIN_PREFIX =
The prefix used for YARD plugins. Name your gem with this prefix to allow it to be used as a plugin.
/^yard[-_]/
Class Attribute Summary collapse
-
.options ⇒ SymbolHash
The system-wide configuration options for YARD.
Class Method Summary collapse
-
.add_ignored_plugins_file ⇒ Object
Legacy support for IGNORED_PLUGINS.
-
.arguments ⇒ Array<String>
Arguments from commandline and yardopts file.
-
.load ⇒ void
Loads settings from CONFIG_FILE.
-
.load_autoload_plugins ⇒ Object
Load plugins set in :autoload_plugins.
-
.load_commandline_plugins ⇒ Object
Load plugins from Config.arguments.
-
.load_commandline_safemode ⇒ Object
Check for command-line safe_mode switch in Config.arguments.
-
.load_gem_plugins ⇒ Object
Load gem plugins if :load_plugins is true.
-
.load_plugin(name) ⇒ Boolean
Loads an individual plugin by name.
-
.load_plugin_failed(name, exception) ⇒ false
Print a warning if the plugin failed to load.
-
.load_plugins ⇒ Boolean
Loads gems that match the name ‘yard-*’ (recommended) or ‘yard_*’ except those listed in ~/.yard/ignored_plugins.
-
.read_config_file ⇒ Hash
Loads the YAML configuration file into memory.
-
.save ⇒ void
Saves settings to CONFIG_FILE.
-
.translate_plugin_name(name) ⇒ String
Sanitizes and normalizes a plugin name to include the ‘yard-’ prefix.
-
.translate_plugin_names ⇒ Object
Translates plugin names to add yard- prefix.
-
.with_yardopts ⇒ Object
Temporarily loads .yardopts file into @yardopts.
Class Attribute Details
.options ⇒ SymbolHash
The system-wide configuration options for YARD
91 92 93 |
# File 'lib/yard/config.rb', line 91 def @options end |
Class Method Details
.add_ignored_plugins_file ⇒ Object
Legacy support for IGNORED_PLUGINS
221 222 223 224 225 |
# File 'lib/yard/config.rb', line 221 def self.add_ignored_plugins_file if File.file?(IGNORED_PLUGINS) [:ignored_plugins] += File.read(IGNORED_PLUGINS).split(/\s+/) end end |
.arguments ⇒ Array<String>
Returns arguments from commandline and yardopts file.
264 265 266 |
# File 'lib/yard/config.rb', line 264 def self.arguments ARGV + @yardopts end |
.load ⇒ void
This method returns an undefined value.
Loads settings from CONFIG_FILE. This method is called by YARD at load time and should not be called by the user.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/yard/config.rb', line 119 def self.load self. = SymbolHash.new(false) .update(DEFAULT_CONFIG_OPTIONS) .update(read_config_file) load_commandline_safemode add_ignored_plugins_file translate_plugin_names load_plugins rescue => e log.error "Invalid configuration file, using default options." log.backtrace(e) .update(DEFAULT_CONFIG_OPTIONS) end |
.load_autoload_plugins ⇒ Object
Load plugins set in :autoload_plugins
189 190 191 |
# File 'lib/yard/config.rb', line 189 def self.load_autoload_plugins [:autoload_plugins].each {|name| load_plugin(name) } end |
.load_commandline_plugins ⇒ Object
Load plugins from arguments
194 195 196 197 198 199 200 201 |
# File 'lib/yard/config.rb', line 194 def self.load_commandline_plugins with_yardopts do arguments.each_with_index do |arg, i| next unless arg == '--plugin' load_plugin(arguments[i + 1]) end end end |
.load_commandline_safemode ⇒ Object
Check for command-line safe_mode switch in arguments
204 205 206 207 208 209 210 |
# File 'lib/yard/config.rb', line 204 def self.load_commandline_safemode with_yardopts do arguments.each_with_index do |arg, _i| [:safe_mode] = true if arg == '--safe' end end end |
.load_gem_plugins ⇒ Object
Load gem plugins if :load_plugins is true
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/yard/config.rb', line 169 def self.load_gem_plugins return true unless [:load_plugins] require 'rubygems' result = true YARD::GemIndex.each do |gem| begin next true unless gem.name =~ YARD_PLUGIN_PREFIX load_plugin(gem.name) rescue Gem::LoadError => e tmp = load_plugin_failed(gem.name, e) result = tmp unless tmp end end result rescue LoadError log.debug "RubyGems is not present, skipping plugin loading" false end |
.load_plugin(name) ⇒ Boolean
Loads an individual plugin by name. It is not necessary to include the yard-
plugin prefix here.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/yard/config.rb', line 157 def self.load_plugin(name) name = translate_plugin_name(name) return false if [:ignored_plugins].include?(name) return false if name =~ /^yard-doc-/ log.debug "Loading plugin '#{name}'..." require name true rescue LoadError => e load_plugin_failed(name, e) end |
.load_plugin_failed(name, exception) ⇒ false
Print a warning if the plugin failed to load
214 215 216 217 218 |
# File 'lib/yard/config.rb', line 214 def self.load_plugin_failed(name, exception) log.error "Error loading plugin '#{name}'" log.backtrace(exception) if $DEBUG false end |
.load_plugins ⇒ Boolean
Loads gems that match the name ‘yard-*’ (recommended) or ‘yard_*’ except those listed in ~/.yard/ignored_plugins. This is called immediately after YARD is loaded to allow plugin support.
146 147 148 149 150 |
# File 'lib/yard/config.rb', line 146 def self.load_plugins load_gem_plugins && load_autoload_plugins && load_commandline_plugins ? true : false end |
.read_config_file ⇒ Hash
Loads the YAML configuration file into memory
236 237 238 239 240 241 242 243 |
# File 'lib/yard/config.rb', line 236 def self.read_config_file if File.file?(CONFIG_FILE) require 'yaml' YAML.load_file(CONFIG_FILE) else {} end end |
.save ⇒ void
This method returns an undefined value.
Saves settings to CONFIG_FILE.
135 136 137 138 139 |
# File 'lib/yard/config.rb', line 135 def self.save require 'yaml' Dir.mkdir(CONFIG_DIR) unless File.directory?(CONFIG_DIR) File.open(CONFIG_FILE, 'w') {|f| f.write(YAML.dump()) } end |
.translate_plugin_name(name) ⇒ String
Sanitizes and normalizes a plugin name to include the ‘yard-’ prefix.
248 249 250 251 252 |
# File 'lib/yard/config.rb', line 248 def self.translate_plugin_name(name) name = name.delete('/') # Security sanitization name = "yard-" + name unless name =~ YARD_PLUGIN_PREFIX name end |
.translate_plugin_names ⇒ Object
Translates plugin names to add yard- prefix.
228 229 230 231 |
# File 'lib/yard/config.rb', line 228 def self.translate_plugin_names [:ignored_plugins].map! {|name| translate_plugin_name(name) } [:autoload_plugins].map! {|name| translate_plugin_name(name) } end |
.with_yardopts ⇒ Object
Temporarily loads .yardopts file into @yardopts
255 256 257 258 259 260 261 |
# File 'lib/yard/config.rb', line 255 def self.with_yardopts yfile = CLI::Yardoc::DEFAULT_YARDOPTS_FILE @yardopts = File.file?(yfile) ? File.read_binary(yfile).shell_split : [] result = yield @yardopts = nil result end |