Class: Aspera::Cli::Command::Registry
- Inherits:
-
Object
- Object
- Aspera::Cli::Command::Registry
- Defined in:
- lib/aspera/cli/command/registry.rb
Overview
Stores Command objects indexed by their full path (Array
Public API:
register(spec) - store a Command; raises on duplicate full_path
register_option(spec) - store an Option by name
option_specs - Hash{Symbol => Option} of all registered options
[](path) - retrieve a Command by full path (follows mounts)
children_of(path) - Hash{Symbol => Command} of direct children (follows mounts)
resolve(path) - [registry, path] owning the spec at path (follows mounts)
local?(path) - true if path is owned by this registry (not reached through a mount)
mount_of(path) - Command::Mount of the local node at path, if any
mount_at(path) - Command::Mount of the node at path, if any (follows mounts)
own_children_of(path) - Hash{Symbol => Command} of direct children declared on the node, without mounted ones
arguments_at(path) - Array of Argument read by the node at path (mount arguments first)
leaf_paths - Array of all leaf paths (follows mounts, or stops at mount nodes)
command_path(words) - command path designated by command line words (aliases, prefixes, arguments)
command_position(words) - command path and number of its arguments, as read by the dispatcher
all_paths - Array of all locally registered full paths
any? - true if at least one spec has been registered
validate! - cross-spec consistency checks; raises on violation
Paths are always expressed in this registry's namespace: a path going through a mounted node (see Command::Mount) is translated to the target registry transparently. Specs returned for mounted paths are the target's specs (their full_path is in the target namespace).
Instance Method Summary collapse
- #[](path) ⇒ Command?
-
#all_paths ⇒ Array<Array<Symbol>>
All locally registered full paths.
-
#any? ⇒ Boolean
True if at least one spec is registered.
-
#arguments_at(path) ⇒ Array<Argument>
Arguments read by the node at path, in order.
-
#children_of(path) ⇒ Hash{Symbol => Command}
Returns a Hash mapping each child id to its Command for all direct children of
path. -
#command_path(words) ⇒ Array(Array<Symbol>, Symbol)
Command path designated by words of a command line: sub-commands (or their aliases, or a unique prefix), each possibly followed by its positional arguments, e.g.
-
#command_position(words) ⇒ Array(Array<Symbol>, Integer)?
Position reached by words of a command line, as read by the dispatcher: an intermediate command reads its positional arguments before its sub-command, a leaf command reads the following words as arguments.
-
#leaf_paths(path = [], chain = [], expand_mounts: true) ⇒ Array<Array<Symbol>>
All leaf paths under path, in tree order, following mounts.
-
#local?(path) ⇒ Boolean
True if the node at path is declared in this registry (not mounted).
-
#mount_at(path) ⇒ Command::Mount?
The mount declared on the node at path, following mounts.
-
#mount_of(path) ⇒ Command::Mount?
The mount declared on that node.
-
#none? ⇒ Boolean
True if no specs have been registered.
-
#option_registered?(name) ⇒ Boolean
True if an option of this name is registered (no ordering).
-
#option_specs ⇒ Hash{Symbol => Option}
Options that cannot be placed (the anchor of their
before:is not registered, or the anchors form a cycle) do not raise, so that a wrong anchor in one module does not break every command: they go at the end, in order of registration, and a warning names each of them and its anchor (logged once per registry, as the list is built several times). -
#own_children_of(path) ⇒ Hash{Symbol => Command}
Children declared on the node itself, without the ones exposed by its mount.
-
#register(spec, before: nil) ⇒ Command
Register a Command.
-
#register_option(spec, before: nil) ⇒ Option
Register an Option.
-
#resolve(path) ⇒ Array(Command::Registry, Array<Symbol>)
Find the registry owning
path, following mounts. -
#validate!(plugin_class: nil) ⇒ self
Cross-spec consistency checks.
Instance Method Details
#[](path) ⇒ Command?
41 42 43 44 |
# File 'lib/aspera/cli/command/registry.rb', line 41 def [](path) registry, local_path = resolve(path) registry.equal?(self) ? @specs[local_path] : registry[local_path] end |
#all_paths ⇒ Array<Array<Symbol>>
Returns all locally registered full paths.
210 211 212 |
# File 'lib/aspera/cli/command/registry.rb', line 210 def all_paths @specs.keys end |
#any? ⇒ Boolean
Returns true if at least one spec is registered.
264 265 266 |
# File 'lib/aspera/cli/command/registry.rb', line 264 def any? !@specs.empty? end |
#arguments_at(path) ⇒ Array<Argument>
Arguments read by the node at path, in order. For a child exposed by a mount, the mount's arguments come first (read by the host).
93 94 95 96 |
# File 'lib/aspera/cli/command/registry.rb', line 93 def arguments_at(path) path = Array(path) (path.empty? ? [] : mount_arguments(path)) + (self[path]&.arguments || []) end |
#children_of(path) ⇒ Hash{Symbol => Command}
Returns a Hash mapping each child id to its Command for all direct
children of path. Empty hash if no children are registered.
For a mount node: mounted children (filtered) followed by local children,
local ones overriding mounted ones with the same id.
129 130 131 132 133 134 135 136 |
# File 'lib/aspera/cli/command/registry.rb', line 129 def children_of(path) registry, local_path = resolve(path) return registry.children_of(local_path) unless registry.equal?(self) own = @children_index[local_path] || {} mount = @specs[local_path]&.mount return own if mount.nil? mount.registry.children_of(mount.at).select { |id, _| mount.accepts?(id) }.merge(own) end |
#command_path(words) ⇒ Array(Array<Symbol>, Symbol)
Command path designated by words of a command line: sub-commands (or their aliases, or a unique prefix),
each possibly followed by its positional arguments, e.g. packages receive ALL designates packages receive.
Words after a leaf command are its arguments.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/aspera/cli/command/registry.rb', line 164 def command_path(words) path = [] # Number of positional arguments still accepted by the node at path args_left = 0 words.each do |word| children = children_of(path) break if children.empty? id = child_id(children, word) if id path += [id] arguments = arguments_at(path) args_left = arguments.any?(&:multiple) ? Float::INFINITY : arguments.length elsif args_left.positive? args_left -= 1 else return [path, word] end end [path, nil] end |
#command_position(words) ⇒ Array(Array<Symbol>, Integer)?
Position reached by words of a command line, as read by the dispatcher: an intermediate command reads
its positional arguments before its sub-command, a leaf command reads the following words as arguments.
Unlike command_path, a word expected as argument is never taken as a sub-command.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/aspera/cli/command/registry.rb', line 191 def command_position(words) path = [] given = 0 words.each do |word| children = children_of(path) arguments = arguments_at(path) if given < arguments.length || (children.empty? && arguments.last&.multiple) given += 1 else id = child_id(children, word) return nil if id.nil? path += [id] given = 0 end end [path, given] end |
#leaf_paths(path = [], chain = [], expand_mounts: true) ⇒ Array<Array<Symbol>>
All leaf paths under path, in tree order, following mounts. A mount cycle (a sub-tree mounting one of its ancestors) is not expanded twice.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/aspera/cli/command/registry.rb', line 143 def leaf_paths(path = [], chain = [], expand_mounts: true) children_of(path).keys.flat_map do |id| child = path + [id] key = subtree_key(child) next [] if chain.include?(key) if ! && mount_at(child) next [child] + own_children_of(child).keys.flat_map do |own_id| own_child = child + [own_id] children_of(own_child).empty? ? [own_child] : leaf_paths(own_child, chain + [key], expand_mounts: false) end end children_of(child).empty? ? [child] : leaf_paths(child, chain + [key], expand_mounts: ) end end |
#local?(path) ⇒ Boolean
Returns true if the node at path is declared in this registry (not mounted).
63 64 65 |
# File 'lib/aspera/cli/command/registry.rb', line 63 def local?(path) resolve(path).first.equal?(self) end |
#mount_at(path) ⇒ Command::Mount?
Returns the mount declared on the node at path, following mounts.
75 76 77 78 |
# File 'lib/aspera/cli/command/registry.rb', line 75 def mount_at(path) registry, local_path = resolve(path) registry.mount_of(local_path) end |
#mount_of(path) ⇒ Command::Mount?
Returns the mount declared on that node.
69 70 71 |
# File 'lib/aspera/cli/command/registry.rb', line 69 def mount_of(path) @specs[Array(path)]&.mount end |
#none? ⇒ Boolean
Returns true if no specs have been registered.
269 270 271 |
# File 'lib/aspera/cli/command/registry.rb', line 269 def none? @specs.empty? end |
#option_registered?(name) ⇒ Boolean
Returns true if an option of this name is registered (no ordering).
229 230 231 |
# File 'lib/aspera/cli/command/registry.rb', line 229 def option_registered?(name) @option_specs.key?(name) end |
#option_specs ⇒ Hash{Symbol => Option}
Options that cannot be placed (the anchor of their before: is not registered, or the anchors form a cycle) do not raise,
so that a wrong anchor in one module does not break every command: they go at the end, in order of registration,
and a warning names each of them and its anchor (logged once per registry, as the list is built several times).
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/aspera/cli/command/registry.rb', line 237 def option_specs return @option_specs.dup if @option_before.empty? ordered = @option_specs.except(*@option_before.keys) pending = @option_before.dup until pending.empty? # an option whose anchor is itself a pending option waits for the anchor to be placed (chains, whatever the declaration order) ready = pending.select { |_name, anchor| ordered.key?(anchor) } unless ready.empty? ready.each do |name, anchor| spec = @option_specs[name] ordered = ordered.each_with_object({}) do |(id, other), placed| placed[name] = spec if id.eql?(anchor) placed[id] = other end pending.delete(name) end next end # the remaining ones cannot be placed: unknown anchor, or cycle warn_unplaced(pending) @option_specs.each { |name, spec| ordered[name] = spec if pending.key?(name) } break end ordered end |
#own_children_of(path) ⇒ Hash{Symbol => Command}
Children declared on the node itself, without the ones exposed by its mount.
83 84 85 86 87 |
# File 'lib/aspera/cli/command/registry.rb', line 83 def own_children_of(path) registry, local_path = resolve(path) return registry.own_children_of(local_path) unless registry.equal?(self) @children_index[local_path] || {} end |
#register(spec, before: nil) ⇒ Command
Register a Command. Raises if the full_path is already registered. Also updates the children index so children_of remains O(1).
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/aspera/cli/command/registry.rb', line 104 def register(spec, before: nil) path = spec.full_path raise ArgumentError, "Duplicate command path: #{path.inspect}" if @specs.key?(path) # Index: parent_path -> { child_id -> spec } parent = path[0..-2] # [] for root-level commands siblings = (@children_index[parent] ||= {}) raise ArgumentError, "#{path.inspect}: no command #{before.inspect} to place it before" unless before.nil? || siblings.key?(before) @specs[path] = spec if before.nil? siblings[spec.id] = spec else @children_index[parent] = siblings.each_with_object({}) do |(id, sibling), placed| placed[spec.id] = spec if id.eql?(before) placed[id] = sibling end end spec end |
#register_option(spec, before: nil) ⇒ Option
Register an Option. Raises if the option name is already registered.
221 222 223 224 225 |
# File 'lib/aspera/cli/command/registry.rb', line 221 def register_option(spec, before: nil) raise ArgumentError, "Duplicate option: #{spec.name.inspect}" if @option_specs.key?(spec.name) @option_before[spec.name] = before unless before.nil? @option_specs[spec.name] = spec end |
#resolve(path) ⇒ Array(Command::Registry, Array<Symbol>)
Find the registry owning path, following mounts.
A host child always takes precedence over a mounted child with the same id.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aspera/cli/command/registry.rb', line 50 def resolve(path) path = Array(path) path.each_index do |i| prefix = path[0, i] mount = @specs[prefix]&.mount next if mount.nil? || @children_index[prefix]&.key?(path[i]) || !mount.accepts?(path[i]) return mount.registry.resolve(mount.at + path[i..]) end [self, path] end |
#validate!(plugin_class: nil) ⇒ self
Cross-spec consistency checks.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/aspera/cli/command/registry.rb', line 278 def validate!(plugin_class: nil) @children_index.each do |parent_path, children| # Rule: every non-root parent path that appears in the children index must have # a registered Command. A missing parent means commands_under(:x) was used # without a matching command :x declaration. unless parent_path.empty? || @specs.key?(parent_path) raise ArgumentError, "commands_under(#{parent_path.map(&:inspect).join(', ')}) used but #{parent_path.last.inspect} has no command declaration" end # Rule: an alias designates a single command, and does not hide a sibling command aliases = children.values.flat_map { |c| Array(c.aliases) } conflicts = aliases.select { |a| children.key?(a) || aliases.count(a) > 1 }.uniq raise ArgumentError, "#{parent_path.inspect}: alias conflicts with a sibling command or alias: #{conflicts.inspect}" unless conflicts.empty? end @specs.each_value do |spec| path = spec.full_path validate_arguments(path, spec.arguments, plugin_class) # Rule: methods called on the plugin instance exist if plugin_class {setup: spec.setup, condition: spec.condition}.each do |attribute, method_name| raise ArgumentError, "#{path.inspect}: no method #{method_name} on #{plugin_class} for #{attribute}:" unless method_name.nil? || instance_method?(plugin_class, method_name) end end if (mount = spec.mount) # Rule: a mount needs an instance method, no action, and must point to existing target nodes raise ArgumentError, "#{path.inspect}: mount requires instance:" if mount.instance.nil? # Mount arguments precede the arguments of the mounted command: they cannot be optional raise ArgumentError, "#{path.inspect}: mount arguments must be mandatory" unless mount.arguments.all?(&:mandatory) validate_arguments(path, mount.arguments, plugin_class) raise ArgumentError, "#{path.inspect}: mount and action: are exclusive" if spec.action raise ArgumentError, "#{path.inspect}: mount and read_only: are exclusive" if spec.read_only.eql?(true) raise ArgumentError, "#{path.inspect}: mount at #{mount.at.inspect} not found in #{mount.plugin}" unless mount.at.empty? || mount.registry[mount.at] target_ids = mount.registry.children_of(mount.at).keys unknown = Array(mount.only) + Array(mount.except) - target_ids raise ArgumentError, "#{path.inspect}: mount only/except unknown in #{mount.plugin}: #{unknown.inspect}" unless unknown.empty? instance_defined = plugin_class.nil? || instance_method?(plugin_class, mount.instance) raise ArgumentError, "#{path.inspect}: no method #{mount.instance} on #{plugin_class}" unless instance_defined next end # Rule: only a leaf is executed, so only a leaf is read-only raise ArgumentError, "#{path.inspect}: read_only: only on a leaf" if spec.read_only.eql?(true) && @children_index[path]&.any? next if @children_index[path]&.any? # intermediate node: skip action = spec.action if action.nil? next unless plugin_class # Rule: leaf commands with no explicit action must have a matching instance method action = Command.action_method(path) unless instance_method?(plugin_class, action) raise ArgumentError, "#{path.inspect}: no action: and no method #{action} on #{plugin_class}" end end action = plugin_class.instance_method(action) if action.is_a?(Symbol) && plugin_class && instance_method?(plugin_class, action) # Rule: the action receives the whole dispatch context as keywords (setup results, arguments), # so it must accept any keyword (`**`): a lambda or method with fixed arity would raise ArgumentError. # A non-lambda Proc ignores extra keywords. next if action.is_a?(Symbol) || (action.is_a?(Proc) && !action.lambda?) raise ArgumentError, "#{path.inspect}: action must accept any keyword (**), parameters: #{action.parameters.inspect}" unless action.parameters.any? { |kind, _| kind.eql?(:keyrest) } end self end |