Class: Jets::Commands::Base

Inherits:
Thor
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/commands/base.rb

Direct Known Subclasses

Clean, Clean::Log, Db, Dotenv, Dynamodb, Dynamodb::Migrate, Gems, Import, Main

Class Method Summary collapse

Class Method Details

.autocomplete(full_command) ⇒ Object

If this fails to find a match then return the original full command



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jets/commands/base.rb', line 112

def autocomplete(full_command)
  return nil if full_command.nil? # jets help

  eager_load!

  words = full_command.split(':')
  namespace = words[0..-2].join(':') if words.size > 1
  command = words.last

  # Thor's normalize_command_name autocompletes the command but then we need to add the namespace back
  begin
    thor_subclass = klass_from_namespace(namespace) # could NameError
    command = thor_subclass.normalize_command_name(command) # could Thor::AmbiguousCommandError
    [namespace, command].compact.join(':')
  rescue NameError
    full_command # return original full_command
  rescue Thor::AmbiguousCommandError => e
    puts "Unable to autodetect the command name. #{e.message}."
    full_command # return original full_command
  end
end

Use Jets banner instead of Thor to account for namespaces in commands.



71
72
73
74
75
76
77
# File 'lib/jets/commands/base.rb', line 71

def banner(command, namespace = nil, subcommand = false)
  namespace = namespace_from_class(self)
  command_name = command.usage # set with desc when defining tht Thor class
  namespaced_command = [namespace, command_name].compact.join(':')

  "jets #{namespaced_command}"
end

.dispatch(command, given_args, given_opts, config) ⇒ Object

Hacky way to handle error for ‘jets new’ when no project name is passed in to avoid this error:

required arguments 'project_name' (Thor::RequiredArgumentMissingError)


34
35
36
37
38
39
# File 'lib/jets/commands/base.rb', line 34

def dispatch(command, given_args, given_opts, config)
  if given_args.reject{|s| s =~ /^-/} == ['new'] # user forgot to pass a project name
    given_args = ['help', 'new']
  end
  super
end

.eager_load!Object

For help menu to list all commands, we must eager load the command classes.

There is special eager_load logic here because this is called super early as part of the CLI start. We cannot assume we have full access to to the project yet.



138
139
140
141
142
# File 'lib/jets/commands/base.rb', line 138

def eager_load!
  return if Jets::Turbo.afterburner?

  Jets::Autoloaders.cli.eager_load
end

.help_list(all = false) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jets/commands/base.rb', line 84

def help_list(all=false)
  # hack to show hidden comands when requested
  Thor::HiddenCommand.class_eval do
    def hidden?; false; end
  end if all

  list = []
  eager_load!
  subclasses.each do |klass|
    commands = klass.printable_commands(true, false)
    commands.reject! { |array| array[0].include?(':help') }
    list += commands
  end

  list.sort_by! { |array| array[0] }
end

.inherited(base) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/jets/commands/base.rb', line 46

def inherited(base)
  super

  if base.name
    self.subclasses << base
  end
end

.klass_from_namespace(namespace) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/jets/commands/base.rb', line 101

def klass_from_namespace(namespace)
  if namespace.nil?
    Jets::Commands::Main
  else
    class_name = namespace.gsub(':','/')
    class_name = "Jets::Commands::#{class_name.camelize}"
    class_name.constantize
  end
end

.namespace_from_class(klass) ⇒ Object



79
80
81
82
# File 'lib/jets/commands/base.rb', line 79

def namespace_from_class(klass)
  namespace = klass.to_s.sub('Jets::Commands::', '').underscore.gsub('/',':')
  namespace unless namespace == "main"
end

.namespaced_commandsObject

Fully qualifed task names. Examples:

build
process:controller
dynamodb:migrate:down


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jets/commands/base.rb', line 58

def namespaced_commands
  eager_load!
  subclasses.map do |klass|
    # This all_tasks is part of Thor not the lambda/dsl.rb
    klass.all_tasks.keys.map do |task_name|
      klass = klass.to_s.sub('Jets::Commands::','')
      namespace = klass =~ /^Main/ ? nil : klass.underscore.gsub('/',':')
      [namespace, task_name].compact.join(':')
    end
  end.flatten.sort
end

.perform(full_command, thor_args) ⇒ Object

thor_args is an array of commands. Examples:

["help"]
["dynamodb:migrate"]

Same signature as RakeCommand.perform. Not using full_command.



25
26
27
28
# File 'lib/jets/commands/base.rb', line 25

def perform(full_command, thor_args)
  config = {} # doesnt seem like config is used
  dispatch(nil, thor_args, nil, config)
end

.subclassesObject

Track all command subclasses.



42
43
44
# File 'lib/jets/commands/base.rb', line 42

def subclasses
  @subclasses ||= []
end