Module: Charyf::Command

Includes:
Behavior
Defined in:
lib/charyf/utils/command.rb,
lib/charyf/utils/command/base.rb,
lib/charyf/utils/command/actions.rb,
lib/charyf/utils/command/behavior.rb,
lib/charyf/utils/commands/help/help.rb,
lib/charyf/utils/commands/cli/cli_command.rb,
lib/charyf/utils/command/environment_argument.rb,
lib/charyf/utils/commands/console/console_command.rb,
lib/charyf/utils/commands/destroy/destroy_command.rb,
lib/charyf/utils/commands/generate/generate_command.rb,
lib/charyf/utils/commands/application/application_command.rb

Defined Under Namespace

Modules: Actions, Behavior, EnvironmentArgument Classes: ApplicationCommand, Base, CliCommand, ConsoleCommand, DestroyCommand, GenerateCommand, HelpCommand

Constant Summary collapse

HELP_MAPPINGS =
%w(-h -? --help)

Class Method Summary collapse

Methods included from Behavior

included

Class Method Details

.environmentObject

:nodoc:



14
15
16
# File 'lib/charyf/utils/command.rb', line 14

def environment # :nodoc:
  ENV['CHARYF_ENV'] ? ENV['CHARYF_ENV'] : 'development'
end

.find_by_namespace(namespace, command_name = nil) ⇒ Object

:nodoc:



58
59
60
61
62
63
64
65
# File 'lib/charyf/utils/command.rb', line 58

def find_by_namespace(namespace, command_name = nil) # :nodoc:
  lookups = [namespace]
  lookups << "#{namespace}:#{command_name}" if command_name
  lookups.concat lookups.map {|lookup| "charyf:#{lookup}"}

  namespaces = subclasses.inject(Hash.new) {|h, n| h[n.namespace] = n; h}
  namespaces[(lookups & namespaces.keys).first]
end

.hidden_commandsObject

:nodoc:



18
19
20
# File 'lib/charyf/utils/command.rb', line 18

def hidden_commands # :nodoc:
  @hidden_commands ||= []
end

.invoke(full_namespace, args = [], **config) ⇒ Object

Receives a namespace, arguments and the behavior to invoke the command.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/charyf/utils/command.rb', line 27

def invoke(full_namespace, args = [], **config)
  load_commands!

  namespace = full_namespace = full_namespace.to_s

  if char = namespace =~ /:(\w+)$/
    command_name, namespace = $1, namespace.slice(0, char)
  else
    command_name = namespace
  end

  command_name, namespace = "help", "help" if command_name.blank? || HELP_MAPPINGS.include?(command_name)
  command_name, namespace = "version", "version" if %w( -v --version ).include?(command_name)

  command = find_by_namespace(namespace, command_name)

  # if command && command.all_commands[command_name]
  if command
    command.perform(command_name, args, config)
  else
    $stderr.puts "Unknown command #{full_namespace}\n\n"
    command_name, namespace = "help", "help"
    command = find_by_namespace(namespace, command_name)
    command.perform(command_name, args, config)
  end

  # else
  #   find_by_namespace("rake").perform(full_namespace, args, config)
  # end
end

.load_commands!Object



22
23
24
# File 'lib/charyf/utils/command.rb', line 22

def load_commands!
  require_relative 'commands/all'
end

:nodoc:



67
68
69
# File 'lib/charyf/utils/command.rb', line 67

def print_commands # :nodoc:
  sorted_groups.each {|b, n| print_list(b, n)}
end

.rootObject

Returns the root of the Charyf engine or app running the command.



83
84
85
86
87
# File 'lib/charyf/utils/command.rb', line 83

def root
  if defined?(APP_PATH)
    Pathname.new(File.expand_path("../..", APP_PATH))
  end
end

.sorted_groupsObject

:nodoc:



71
72
73
74
75
76
77
78
79
80
# File 'lib/charyf/utils/command.rb', line 71

def sorted_groups # :nodoc:
  groups = (subclasses - hidden_commands).group_by {|c| c.namespace.split(":").first}

  groups.keys.each do |key|
    groups[key] = groups[key].flat_map(&:printing_commands).sort
  end

  charyf = groups.delete('charyf')
  [['charyf', charyf]] + groups.sort.to_a
end