Class: Hanami::CLI::Commands::App::Command

Inherits:
Hanami::CLI::Command show all
Defined in:
lib/hanami/cli/commands/app/command.rb

Overview

Base class for hanami CLI commands intended to be executed within an existing Hanami app.

Since:

  • 2.0.0

Defined Under Namespace

Modules: Environment

Constant Summary collapse

ACTION_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

"."

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hanami::CLI::Command

#initialize, new

Constructor Details

This class inherits a constructor from Hanami::CLI::Command

Class Method Details

.inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



52
53
54
55
# File 'lib/hanami/cli/commands/app/command.rb', line 52

def self.inherited(klass)
  super
  klass.prepend(Environment)
end

Instance Method Details

#appHanami::App

Returns the Hanami app class.

Returns:

  • (Hanami::App)

    the Hanami app

Raises:

  • (Hanami::AppLoadError)

    if the app has not been loaded

Since:

  • 2.0.0



65
66
67
68
69
70
71
# File 'lib/hanami/cli/commands/app/command.rb', line 65

def app
  @app ||=
    begin
      require "hanami/prepare"
      Hanami.app
    end
end

#measure(desc) ⇒ Object

Executes a given block and prints string to the out stream with details of the time taken to execute.

If the block returns a falsey value, then a failure message is printed.

Examples:

measure("Reverse the polarity of the neutron flow") do
  # reverses the polarity, returns a truthy value
end
# printed to `out`:
# => Reverse the polarity of the neutron flow in 2s
measure("Disable the time dilation device") do
  # attempts to disable the device, returns a falsey favlue
end
# printed to `out`:
# !!! => Disable the time dilation device FAILED

Since:

  • 2.0.0



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hanami/cli/commands/app/command.rb', line 109

def measure(desc)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = yield
  stop = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  if result
    out.puts "=> #{desc} in #{(stop - start).round(4)}s"
  else
    out.puts "!!! => #{desc.inspect} FAILED"
  end
end

#run_command(klass) ⇒ Object

Runs another CLI command via its command class.

Parameters:

  • klass (Hanami::CLI::Command)
  • args (Array)

    any additional arguments to pass to the command's #call method.

Since:

  • 2.0.0



80
81
82
83
84
85
86
# File 'lib/hanami/cli/commands/app/command.rb', line 80

def run_command(klass, ...)
  klass.new(
    out: out,
    inflector: app.inflector,
    fs: Hanami::CLI::Files,
  ).call(...)
end