Class: Jets::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/cli.rb

Constant Summary collapse

ALIASES =
{
  "g"  => "generate",
  "c"  => "console",
  "s"  => "server",
  "db" => "dbconsole",
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_args = ARGV, **config) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
# File 'lib/jets/cli.rb', line 12

def initialize(given_args=ARGV, **config)
  @given_args = given_args.dup
  @config = config
end

Class Method Details

.start(given_args = ARGV) ⇒ Object



4
5
6
# File 'lib/jets/cli.rb', line 4

def self.start(given_args=ARGV)
  new(given_args).start
end

.thor_tasksObject



8
9
10
# File 'lib/jets/cli.rb', line 8

def self.thor_tasks
  Jets::Commands::Base.namespaced_commands
end

Instance Method Details

#boot_jetsObject

The commands new and help do not call Jets.boot. Main reason is that Jets.boot are ran inside a Jets project folder.

  • jets new - need to generate a project outside a project folder.

  • jets help - don’t need to be in a project folder general help. When you are inside a project folder though, more help commands are available and displayed.



44
45
46
47
48
49
50
# File 'lib/jets/cli.rb', line 44

def boot_jets
  command = thor_args.first
  if !%w[new help].include?(command)
    set_jets_env_from_cli_arg!
    Jets.boot
  end
end

#full_commandObject



96
97
98
99
100
101
102
103
# File 'lib/jets/cli.rb', line 96

def full_command
  # Removes any args that starts with -, those are option args.
  # Also remove "help" flag.
  args = @given_args.reject {|o| o =~ /^-/ } - help_flags
  command = args[0] # first argument should always be the command
  command = ALIASES[command] || command
  Jets::Commands::Base.autocomplete(command)
end

#help_flagsObject

“-h”, “-?”, “–help”, “-D”, “help”


121
122
123
# File 'lib/jets/cli.rb', line 121

def help_flags
  Thor::HELP_MAPPINGS + ["help"]
end

#lookup(full_command) ⇒ Object

  1. look up Thor tasks

  2. look up Rake tasks

  3. help menu with all commands when both Thor and Rake tasks are not found



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jets/cli.rb', line 108

def lookup(full_command)
  thor_task_found = Jets::Commands::Base.namespaced_commands.include?(full_command)
  if thor_task_found
    return Jets::Commands::Base.klass_from_namespace(namespace)
  end

  rake_task_found = Jets::Commands::RakeCommand.namespaced_commands.include?(full_command)
  if rake_task_found
    return Jets::Commands::RakeCommand
  end
end

#main_helpObject



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/jets/cli.rb', line 145

def main_help
  shell = Thor::Shell::Basic.new
  shell.say "Commands:"
  shell.print_table(thor_list, :indent => 2, :truncate => true)

  unless rake_list.empty?
    shell.say "\nCommands via rake:"
    shell.print_table(rake_list, :indent => 2, :truncate => true)
  end

  shell.say "\n"
  shell.say main_help_body
end

#main_help_bodyObject



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/jets/cli.rb', line 175

def main_help_body
  <<-EOL
Add -h to any of the commands for more help.  Examples:

jets call -h
jets routes -h
jets deploy -h
jets status -h
jets dynamodb:create -h
jets db:create -h

EOL
end

#methObject



135
136
137
138
139
140
141
142
143
# File 'lib/jets/cli.rb', line 135

def meth
  return nil unless full_command

  if full_command.include?(':')
    full_command.split(':').pop
  else
    full_command
  end
end

#namespaceObject



125
126
127
128
129
130
131
132
133
# File 'lib/jets/cli.rb', line 125

def namespace
  return nil unless full_command

  if full_command.include?(':')
    words = full_command.split(':')
    words.pop
    words.join(':')
  end
end

#rake_listObject



163
164
165
166
167
168
169
# File 'lib/jets/cli.rb', line 163

def rake_list
  list = Jets::Commands::RakeCommand.formatted_rake_tasks(show_all_tasks)
  list.map do |array|
    array[0] = "jets #{array[0]}"
    array
  end
end

#set_jets_env_from_cli_arg!Object

Adjust JETS_ENV before boot_jets is called for the jets deploy command. Must do this early in the process before Jets.boot because Jets.boot calls Jets.env as part of the bootup process in require_bundle_gems and sets the Jets.env to whatever the JETS_ENV is at the time.

Defaults to development when not set.



59
60
61
62
63
64
65
# File 'lib/jets/cli.rb', line 59

def set_jets_env_from_cli_arg!
  command, env = thor_args[0..1]
  return unless %w[deploy delete].include?(command)
  env = nil if env&.starts_with?('-')
  return unless env
  ENV['JETS_ENV'] = env ? env : 'development'
end

#show_all_tasksObject



171
172
173
# File 'lib/jets/cli.rb', line 171

def show_all_tasks
  @given_args.include?("--all") || @given_args.include?("-A")
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jets/cli.rb', line 17

def start
  command_class = lookup(full_command)
  if command_class
    boot_jets
    command_class.perform(full_command, thor_args)
  elsif version_requested?
    puts Jets.version
  else
    main_help
  end
end

#thor_argsObject

thor_args normalized the args Array to work with our Thor command subclasses.

  1. The namespace is stripe

  2. Help is shifted in front if a help flag is detected



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jets/cli.rb', line 71

def thor_args
  args = @given_args.clone

  help_args = args & help_flags
  unless help_args.empty?
    # Allow using help flags at the end of the command to trigger help menu
    args -= help_flags # remove "help" and help flags from args
    args[0] = meth # first command will always be the meth now since
      # we removed the help flags
    args.unshift("help")
    args.compact!
    return args
  end

  # reassigns the command without the namespace if reached here
  args[0] = meth
  args.compact
end

#thor_listObject



159
160
161
# File 'lib/jets/cli.rb', line 159

def thor_list
  Jets::Commands::Base.help_list(show_all_tasks)
end

#version_requested?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/jets/cli.rb', line 29

def version_requested?
  #   jets --version
  #   jets -v
  version_flags = ["--version", "-v"]
  @given_args.length == 1 && !(@given_args & version_flags).empty?
end