Class: Hanami::CLI::Commands::App::Run Private
- Inherits:
-
Hanami::CLI::Command
- Object
- Dry::CLI::Command
- Hanami::CLI::Command
- Hanami::CLI::Commands::App::Run
- 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'
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.
Class.new(StandardError)
Instance Method Summary collapse
-
#call(code_or_path:) ⇒ Object
private
rubocop:disable Metrics/AbcSize.
-
#initialize(command_exit: method(:exit), **opts) ⇒ Run
constructor
private
A new instance of Run.
Methods inherited from Hanami::CLI::Command
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.
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
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.}" raise RunError, "Syntax error in code: #{e.}" rescue NameError => e err.puts "Name error in code: #{e.}" raise RunError, "Name error in code: #{e.}" rescue StandardError => e err.puts "Error executing code: #{e.class}: #{e.}" raise RunError, "Error executing code: #{e.class}: #{e.}" end end rescue RunError @command_exit.call(1) end |