lib/autoproj/shell_completion.rb in autoproj-2.14.0 vs lib/autoproj/shell_completion.rb in autoproj-2.15.0
- old
+ new
@@ -1,7 +1,7 @@
-require 'autoproj/cli/main'
-require 'erb'
+require "autoproj/cli/main"
+require "erb"
module Autoproj
# Generates shell completion for code for a given Thor subclass
class ShellCompletion
# The CLI
@@ -9,18 +9,19 @@
# The command name
attr_reader :name
# A hash describing the CLI
attr_reader :cli_metadata
- TEMPLATES_DIR = File.join(File.dirname(__FILE__), 'templates')
+ TEMPLATES_DIR = File.join(File.dirname(__FILE__), "templates")
- def initialize(name = 'autoproj', cli: Autoproj::CLI::Main, command: nil)
+ def initialize(name = "autoproj", cli: Autoproj::CLI::Main, command: nil)
@cli = cli
@name = name
generate_metadata
return unless command
+
@cli_metadata = subcommand_by_name(*command)
@cli_metadata[:name] = "__#{name}"
end
def generate_metadata
@@ -46,21 +47,21 @@
# leaving disabled for now
# TODO: reset subcommand needs a custom completer,
# leaving disabled for now
# TODO: log subcommand needs a custom completer,
# leaving disabled for now
- ['bootstrap', 'envsh', 'reconfigure', 'reset', 'log', 'query',
- 'switch-config', %w[global register], %w[global status],
+ ["bootstrap", "envsh", "reconfigure", "reset", "log", "query",
+ "switch-config", %w[global register], %w[global status],
%w[plugin install], %w[plugin remove], %w[plugin list]].each do |command|
disable_completion(subcommand_by_name(*command))
end
end
def generate
template_file = File.join(TEMPLATES_DIR, self.class::MAIN_FUNCTION_TEMPLATE)
erb = File.read(template_file)
- ::ERB.new(erb, nil, '-').result(binding)
+ ::ERB.new(erb, nil, "-").result(binding)
end
def subcommand_by_name(*name, metadata: cli_metadata)
subcommand = metadata
@@ -71,22 +72,24 @@
end
subcommand
end
def populate_help_subcommands(command_metadata = cli_metadata)
- help_subcommand = subcommand_by_name('help',
+ help_subcommand = subcommand_by_name("help",
metadata: command_metadata)
if help_subcommand
help_subcommand[:options] = []
disable_completion(help_subcommand)
end
command_metadata[:subcommands].each do |subcommand|
- next if subcommand[:name] == 'help'
+ next if subcommand[:name] == "help"
+
populate_help_subcommands(subcommand)
next unless help_subcommand
+
help_subcommand[:subcommands] << { name: subcommand[:name],
aliases: [],
description: subcommand[:description],
options: [],
subcommands: [] }
@@ -96,21 +99,21 @@
def render_subcommand_function(subcommand, options = {})
prefix = options[:prefix] || []
source = []
prefix = (prefix + [subcommand[:name]])
- function_name = prefix.join('_')
+ function_name = prefix.join("_")
depth = prefix.size + 1
template_file = File.join(TEMPLATES_DIR, self.class::SUBCOMMAND_FUNCTION_TEMPLATE)
- erb = ::ERB.new(File.read(template_file), nil, '-')
+ erb = ::ERB.new(File.read(template_file), nil, "-")
source << erb.result(binding)
subcommand[:subcommands].each do |subcommand|
source << render_subcommand_function(subcommand, prefix: prefix)
end
- source.join("\n").strip + "\n"
+ "#{source.join("\n").strip}\n"
end
def subcommand_metadata(cli)
result = []
cli.all_commands.reject { |_, t| t.hidden? }.each do |(name, command)|
@@ -156,9 +159,9 @@
description: option.description }
end
end
def hyphenate(s)
- s.to_s.tr('_', '-')
+ s.to_s.tr("_", "-")
end
end
end