refactor: derive canonical commands from core actions and centralize aliases

This commit is contained in:
2026-10-11 02:48:13 +02:00
parent 9a34669c08
commit 54bb196192
12 changed files with 352 additions and 88 deletions
+17 -6
View File
@@ -58,26 +58,37 @@ module Vamp
# @type [Module<Vamp::Core>]
Core = Module.new
# @param entries [Array<Hash{String => String, nil}>] action name and optional Ex alias descriptors
# @param entries [Array<Hash{String => String, nil}>] action name and optional canonical Ex name
# @param command_aliases [Hash{String => String}] shorthand to canonical commands
# @return [Array<Module, Vamp::Action>] frozen action namespaces and leaves
def self.install_actions(entries)
def self.install_actions(entries, command_aliases)
raise TypeError, "core action catalog must be an Array" unless entries.is_a?(Array)
raise TypeError, "command aliases must be a Hash" unless command_aliases.is_a?(Hash)
namespaces = [Core]
aliases = {}
commands = {}
entries.sort_by { |entry| entry.fetch("name").count(".") }.each do |entry|
raise TypeError, "core action descriptor must be a Hash" unless entry.is_a?(Hash)
install_action(entry.fetch("name"), namespaces)
ex = entry.fetch("ex")
aliases[string!(ex, "core Ex alias").freeze] = true unless ex.nil?
canonical = string!(ex, "core Ex command").freeze unless ex.nil?
commands[canonical] = canonical unless ex.nil?
end
@builtin_commands = aliases.freeze
canonical_commands = commands.keys
command_aliases.each do |name, canonical|
name = string!(name, "command alias").freeze
canonical = string!(canonical, "canonical command").freeze
raise ArgumentError, "unknown canonical command: #{canonical}" unless canonical_commands.include?(canonical)
commands[name] = canonical
end
@builtin_commands = commands.freeze
namespaces.each(&:freeze)
end
# @param name [String] binding command name
# @return [Boolean] whether the catalog owns this textual Ex alias
# @return [Boolean] whether the catalog owns this canonical command or shorthand
def self.builtin_command?(name)
@builtin_commands.key?(name)
end