|
3 | 3 | require 'dry/cli' |
4 | 4 | require 'dry/cli/completion/command' |
5 | 5 | require_relative '../linear' |
| 6 | +require 'semantic_logger' |
6 | 7 | require 'tty-markdown' |
| 8 | +require 'tty-prompt' |
7 | 9 |
|
8 | 10 | # The Rubyists module is the top-level namespace for all Rubyists projects |
9 | 11 | module Rubyists |
10 | 12 | module Linear |
11 | 13 | # The CLI module is a Dry::CLI::Registry that contains all the commands |
12 | 14 | module CLI |
| 15 | + include SemanticLogger::Loggable |
13 | 16 | extend Dry::CLI::Registry |
14 | 17 |
|
15 | | - # Watch for the call method to be added to a command |
16 | | - module Watcher |
17 | | - def self.extended(_mod) |
18 | | - define_method :method_added do |method_name| |
19 | | - return unless method_name == :call |
20 | | - |
21 | | - prepend Rubyists::Linear::CLI::Caller |
22 | | - end |
23 | | - end |
| 18 | + def self.prompt |
| 19 | + @prompt ||= TTY::Prompt.new |
24 | 20 | end |
25 | 21 |
|
26 | | - # The CommonOptions module contains common options for all commands |
27 | | - module CommonOptions |
28 | | - def self.included(mod) |
29 | | - mod.instance_eval do |
30 | | - extend Rubyists::Linear::CLI::Watcher |
31 | | - option :output, type: :string, default: 'text', values: %w[text json], desc: 'Output format' |
32 | | - option :debug, type: :integer, default: 0, desc: 'Debug level' |
33 | | - end |
| 22 | + def self.register_sub!(command, sub_file, klass) |
| 23 | + # The filename is expected to define a class of the same name, but capitalized |
| 24 | + name = sub_file.basename('.rb').to_s |
| 25 | + subklass = klass.const_get(name.capitalize) |
| 26 | + if (aliases = klass::ALIASES[name.to_sym]) |
| 27 | + command.register name, subklass, aliases: Array(aliases) |
| 28 | + else |
| 29 | + command.register name, subklass |
34 | 30 | end |
| 31 | + end |
35 | 32 |
|
36 | | - def display(subject, options) |
37 | | - return puts(JSON.pretty_generate(subject)) if options[:output] == 'json' |
38 | | - return subject.each { |s| s.display(options) } if subject.respond_to?(:each) |
39 | | - unless subject.respond_to?(:display) |
40 | | - raise SmellsBad, "Cannot display #{subject}, there is no #display method and it is not a collection" |
41 | | - end |
42 | | - |
43 | | - subject.display(options) |
| 33 | + def self.register_subcommands!(command, name, klass) |
| 34 | + Pathname.new(__FILE__).dirname.join("commands/#{name}").glob('*.rb').each do |file| |
| 35 | + require file.expand_path |
| 36 | + register_sub! command, file, klass |
44 | 37 | end |
45 | 38 | end |
46 | 39 |
|
47 | | - # This module is prepended to all commands to log their calls |
48 | | - module Caller |
49 | | - def self.prepended(_mod) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize |
50 | | - Caller.class_eval do |
51 | | - define_method :call do |**method_args| # rubocop:disable Metrics/AbcSize, Metrics/MethodLength |
52 | | - debug = method_args[:debug].to_i |
53 | | - Rubyists::Linear.verbosity = debug |
54 | | - logger.trace "Calling #{self.class} with #{method_args}" |
55 | | - super(**method_args) |
56 | | - rescue SmellsBad => e |
57 | | - logger.error e.message |
58 | | - exit 1 |
59 | | - rescue NotFoundError => e |
60 | | - logger.error e.message |
61 | | - rescue StandardError => e |
62 | | - logger.error e.message |
63 | | - logger.error e.backtrace.join("\n") if Rubyists::Linear.verbosity.positive? |
64 | | - exit 5 |
65 | | - end |
66 | | - end |
| 40 | + def self.load_and_register!(command) |
| 41 | + logger.debug "Registering #{command}" |
| 42 | + name = command.name.split('::').last.downcase |
| 43 | + command_aliases = command::ALIASES[name.to_sym] || [] |
| 44 | + register name, aliases: Array(command_aliases) do |cmd| |
| 45 | + register_subcommands! cmd, name, command |
67 | 46 | end |
68 | 47 | end |
69 | 48 | end |
70 | 49 | end |
71 | 50 |
|
| 51 | + Pathname.new(__FILE__).dirname.join('cli').glob('*.rb').each do |file| |
| 52 | + require file.expand_path |
| 53 | + end |
| 54 | + |
72 | 55 | # Load all our commands |
73 | 56 | Pathname.new(__FILE__).dirname.join('commands').glob('*.rb').each do |file| |
74 | 57 | require file.expand_path |
|
0 commit comments