Class: Toys::Loader
- Inherits:
-
Object
- Object
- Toys::Loader
- Defined in:
- lib/toys/loader.rb
Overview
The Loader service loads tools from configuration files, and finds the appropriate tool given a set of command line arguments.
Instance Method Summary collapse
-
#add_block(high_priority: false, source_name: nil, context_directory: nil, &block) ⇒ self
Add a configuration block to the loader.
-
#add_git(git_remote, git_path, git_commit, high_priority: false, update: false, context_directory: nil) ⇒ self
Add a configuration git source to the loader.
-
#add_path(path, high_priority: false, source_name: nil, context_directory: :parent) ⇒ self
Add a configuration file/directory to the loader.
-
#add_path_set(root_path, relative_paths, high_priority: false, context_directory: :path) ⇒ self
Add a set of configuration files/directories from a common directory to the loader.
-
#has_subtools?(words) ⇒ Boolean
Returns true if the given path has at least one subtool, even if they are hidden or non-runnable.
-
#initialize(index_file_name: nil, preload_dir_name: nil, preload_file_name: nil, data_dir_name: nil, lib_dir_name: nil, middleware_stack: [], extra_delimiters: "", mixin_lookup: nil, middleware_lookup: nil, template_lookup: nil, git_cache: nil) ⇒ Loader
constructor
Create a Loader.
-
#list_subtools(words, recursive: false, include_hidden: false, include_namespaces: false, include_non_runnable: false) ⇒ Array<Toys::ToolDefinition>
Returns a list of subtools for the given path, loading from the configuration if necessary.
-
#lookup(args) ⇒ Array(Toys::ToolDefinition,Array<String>)
Given a list of command line arguments, find the appropriate tool to handle the command, loading it from the configuration if necessary.
-
#lookup_specific(words) ⇒ Toys::ToolDefinition?
Given a tool name, looks up the specific tool, loading it from the configuration if necessary.
-
#split_path(str) ⇒ Array<String>
Splits the given path using the delimiters configured in this Loader.
Constructor Details
#initialize(index_file_name: nil, preload_dir_name: nil, preload_file_name: nil, data_dir_name: nil, lib_dir_name: nil, middleware_stack: [], extra_delimiters: "", mixin_lookup: nil, middleware_lookup: nil, template_lookup: nil, git_cache: nil) ⇒ Loader
Create a Loader
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/toys/loader.rb', line 42 def initialize(index_file_name: nil, preload_dir_name: nil, preload_file_name: nil, data_dir_name: nil, lib_dir_name: nil, middleware_stack: [], extra_delimiters: "", mixin_lookup: nil, middleware_lookup: nil, template_lookup: nil, git_cache: nil) if index_file_name && ::File.extname(index_file_name) != ".rb" raise ::ArgumentError, "Illegal index file name #{index_file_name.inspect}" end require "monitor" @mutex = ::Monitor.new @mixin_lookup = mixin_lookup || ModuleLookup.new @template_lookup = template_lookup || ModuleLookup.new @middleware_lookup = middleware_lookup || ModuleLookup.new @index_file_name = index_file_name @preload_file_name = preload_file_name @preload_dir_name = preload_dir_name @data_dir_name = data_dir_name @lib_dir_name = lib_dir_name @loading_started = false @worklist = [] @tool_data = {} @roots_by_priority = {} @max_priority = @min_priority = 0 @stop_priority = -999_999 @min_loaded_priority = 999_999 @middleware_stack = Middleware.stack(middleware_stack) @delimiter_handler = DelimiterHandler.new(extra_delimiters) @git_cache = git_cache get_tool([], -999_999) end |
Instance Method Details
#add_block(high_priority: false, source_name: nil, context_directory: nil, &block) ⇒ self
Add a configuration block to the loader.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/toys/loader.rb', line 167 def add_block(high_priority: false, source_name: nil, context_directory: nil, &block) @mutex.synchronize do raise "Cannot add a block after tool loading has started" if @loading_started priority = high_priority ? (@max_priority += 1) : (@min_priority -= 1) source = SourceInfo.create_proc_root(block, priority, context_directory: context_directory, source_name: source_name, data_dir_name: @data_dir_name, lib_dir_name: @lib_dir_name) @roots_by_priority[priority] = source @worklist << [source, [], priority] end self end |
#add_git(git_remote, git_path, git_commit, high_priority: false, update: false, context_directory: nil) ⇒ self
Add a configuration git source to the loader.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/toys/loader.rb', line 203 def add_git(git_remote, git_path, git_commit, high_priority: false, update: false, context_directory: nil) git_cache = @git_cache || Loader.default_git_cache path = git_cache.get(git_remote, path: git_path, commit: git_commit, update: update) @mutex.synchronize do raise "Cannot add a git source after tool loading has started" if @loading_started priority = high_priority ? (@max_priority += 1) : (@min_priority -= 1) source = SourceInfo.create_git_root(git_remote, git_path, git_commit, path, priority, context_directory: context_directory, data_dir_name: @data_dir_name, lib_dir_name: @lib_dir_name) @roots_by_priority[priority] = source @worklist << [source, [], priority] end self end |
#add_path(path, high_priority: false, source_name: nil, context_directory: :parent) ⇒ self
Add a configuration file/directory to the loader.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/toys/loader.rb', line 94 def add_path(path, high_priority: false, source_name: nil, context_directory: :parent) @mutex.synchronize do raise "Cannot add a path after tool loading has started" if @loading_started priority = high_priority ? (@max_priority += 1) : (@min_priority -= 1) source = SourceInfo.create_path_root(path, priority, context_directory: context_directory, data_dir_name: @data_dir_name, lib_dir_name: @lib_dir_name, source_name: source_name) @roots_by_priority[priority] = source @worklist << [source, [], priority] end self end |
#add_path_set(root_path, relative_paths, high_priority: false, context_directory: :path) ⇒ self
Add a set of configuration files/directories from a common directory to the loader. The set of paths will be added at the same priority level and will share a root.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/toys/loader.rb', line 131 def add_path_set(root_path, relative_paths, high_priority: false, context_directory: :path) relative_paths = Array(relative_paths) @mutex.synchronize do raise "Cannot add a path after tool loading has started" if @loading_started priority = high_priority ? (@max_priority += 1) : (@min_priority -= 1) root_source = SourceInfo.create_path_root(root_path, priority, context_directory: context_directory, data_dir_name: @data_dir_name, lib_dir_name: @lib_dir_name) @roots_by_priority[priority] = root_source relative_paths.each do |path, individual_name| source = root_source.relative_child(path, source_name: individual_name) @worklist << [source, [], priority] end end self end |
#has_subtools?(words) ⇒ Boolean
Returns true if the given path has at least one subtool, even if they are hidden or non-runnable. Loads from the configuration if necessary.
306 307 308 309 310 311 312 313 |
# File 'lib/toys/loader.rb', line 306 def has_subtools?(words) # rubocop:disable Naming/PredicateName load_for_prefix(words) len = words.length all_cur_definitions.any? do |tool| name = tool.full_name name.length > len && name.slice(0, len) == words end end |
#list_subtools(words, recursive: false, include_hidden: false, include_namespaces: false, include_non_runnable: false) ⇒ Array<Toys::ToolDefinition>
Returns a list of subtools for the given path, loading from the configuration if necessary. The list will be sorted by name.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/toys/loader.rb', line 281 def list_subtools(words, recursive: false, include_hidden: false, include_namespaces: false, include_non_runnable: false) load_for_prefix(words) len = words.length found_tools = all_cur_definitions.find_all do |tool| name = tool.full_name name.length > len && name.slice(0, len) == words && (include_hidden || name[len..-1].none? { |word| word.start_with?("_") }) end found_tools.sort_by!(&:full_name) found_tools = filter_non_runnable_tools(found_tools, include_namespaces, include_non_runnable) found_tools.select! { |tool| tool.full_name.length == len + 1 } unless recursive found_tools end |
#lookup(args) ⇒ Array(Toys::ToolDefinition,Array<String>)
Given a list of command line arguments, find the appropriate tool to handle the command, loading it from the configuration if necessary. This always returns a tool. If the specific tool path is not defined and cannot be found in any configuration, it finds the nearest namespace that would contain that tool, up to the root tool.
Returns a tuple of the found tool, and the array of remaining arguments that are not part of the tool name and should be passed as tool args.
235 236 237 238 239 240 241 242 243 |
# File 'lib/toys/loader.rb', line 235 def lookup(args) orig_prefix, args = @delimiter_handler.find_orig_prefix(args) prefix = orig_prefix loop do tool = lookup_specific(prefix) return [tool, args.slice(prefix.length..-1)] if tool prefix = prefix.slice(0..-2) end end |
#lookup_specific(words) ⇒ Toys::ToolDefinition?
Given a tool name, looks up the specific tool, loading it from the configuration if necessary.
If there is an active tool, returns it; otherwise, returns the highest
priority tool that has been defined. If no tool has been defined with
the given name, returns nil
.
257 258 259 260 261 262 263 |
# File 'lib/toys/loader.rb', line 257 def lookup_specific(words) words = @delimiter_handler.split_path(words.first) if words.size == 1 load_for_prefix(words) tool = @mutex.synchronize { get_tool_data(words, false)&.cur_definition } finish_definitions_in_tree(words) if tool tool end |
#split_path(str) ⇒ Array<String>
Splits the given path using the delimiters configured in this Loader. You may pass in either an array of strings, or a single string possibly delimited by path separators. Always returns an array of strings.
323 324 325 326 |
# File 'lib/toys/loader.rb', line 323 def split_path(str) return str.map(&:to_s) if str.is_a?(::Array) @delimiter_handler.split_path(str.to_s) end |