Class: Hanami::CLI::Commands::App::Run Private

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

Overview

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

Run a given code or file in the context of the application

This command is useful for running scripts that need to load the application environment. You can pass a Ruby file to be executed, or you can run an interactive Ruby shell (IRB) with the application environment loaded.

Examples:

$ bundle exec hanami run path/to/script.rb $ bundle exec hanami run 'puts Hanami.app["repos.user_repo"].all.count'

Since:

  • 2.0.0

Constant Summary collapse

RunError =

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.new(StandardError)

Instance Method Summary collapse

Methods inherited from Hanami::CLI::Command

#inflector, new

Constructor Details

#initialize(command_exit: method(:exit), **opts) ⇒ Run

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.

Returns a new instance of Run.

Since:

  • 2.0.0



35
36
37
38
# File 'lib/hanami/cli/commands/app/run.rb', line 35

def initialize(command_exit: method(:exit), **opts)
  super(**opts)
  @command_exit = command_exit
end

Instance Method Details

#call(code_or_path:) ⇒ 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.

rubocop:disable Metrics/AbcSize

Since:

  • 2.0.0



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hanami/cli/commands/app/run.rb', line 41

def call(code_or_path:, **)
  require "hanami/prepare"

  if File.exist?(code_or_path)
    validate_file_path!(code_or_path)
    Kernel.load code_or_path
  else
    validate_inline_code!(code_or_path)
    begin
      eval(code_or_path, binding, __FILE__, __LINE__) # rubocop:disable Security/Eval
    rescue SyntaxError => e
      err.puts "Syntax error in code: #{e.message}"
      raise RunError, "Syntax error in code: #{e.message}"
    rescue NameError => e
      err.puts "Name error in code: #{e.message}"
      raise RunError, "Name error in code: #{e.message}"
    rescue StandardError => e
      err.puts "Error executing code: #{e.class}: #{e.message}"
      raise RunError, "Error executing code: #{e.class}: #{e.message}"
    end
  end
rescue RunError
  @command_exit.call(1)
end