-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'bundler' | ||
require 'hanami/cli' | ||
require 'stealth/cli' | ||
Stealth::Cli.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'thor' | ||
require 'stealth/cli_base' | ||
require 'stealth/commands/console' | ||
|
||
module Stealth | ||
class Cli < Thor | ||
extend CliBase | ||
|
||
desc 'version', 'Prints stealth version' | ||
long_desc <<-EOS | ||
`stealth version` prints the version of the bundled stealth gem. | ||
EOS | ||
def version | ||
require 'stealth/version' | ||
puts "v#{ Stealth::VERSION }" | ||
end | ||
map %w{--version -v} => :version | ||
|
||
desc 'console', 'Starts a stealth console' | ||
long_desc <<-EOS | ||
`stealth console` starts the interactive stealth console. | ||
$ > stealth console --engine=pry | ||
EOS | ||
method_option :environment, desc: 'Path to environment configuration (config/environment.rb)' | ||
method_option :engine, desc: "Choose a specific console engine: (#{Stealth::Commands::Console::ENGINES.keys.join('/')})" | ||
method_option :help, desc: 'Displays the usage method' | ||
def console | ||
if options[:help] | ||
invoke :help, ['console'] | ||
else | ||
Stealth::Commands::Console.new(options).start | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Stealth | ||
module CliBase | ||
def define_commands(&blk) | ||
class_eval(&blk) if block_given? | ||
end | ||
|
||
def banner(command, nspace = true, subcommand = false) | ||
super(command, nspace, namespace != 'stealth:cli') | ||
end | ||
|
||
def handle_argument_error(command, error, args, arity) | ||
name = [(namespace == 'stealth:cli' ? nil : namespace), command.name].compact.join(" ") | ||
|
||
msg = "ERROR: \"#{basename} #{name}\" was called with " | ||
msg << "no arguments" if args.empty? | ||
msg << "arguments " << args.inspect unless args.empty? | ||
msg << "\nUsage: #{banner(command).inspect}" | ||
|
||
raise Thor::InvocationError, msg | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'stealth' | ||
|
||
module Stealth | ||
module Commands | ||
class Command | ||
def initialize(options) | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'stealth/commands/command' | ||
|
||
module Stealth | ||
module Commands | ||
# REPL that supports different engines. | ||
# | ||
# It is run with: | ||
# | ||
# `bundle exec stealth console` | ||
class Console < Command | ||
module CodeReloading | ||
def reload! | ||
puts 'Reloading...' | ||
Kernel.exec "#{$PROGRAM_NAME} console" | ||
end | ||
end | ||
|
||
# Supported engines | ||
ENGINES = { | ||
'pry' => 'Pry', | ||
'ripl' => 'Ripl', | ||
'irb' => 'IRB' | ||
}.freeze | ||
|
||
DEFAULT_ENGINE = ['irb'].freeze | ||
|
||
attr_reader :options | ||
|
||
def initialize(options) | ||
super(options) | ||
|
||
@options = options | ||
end | ||
|
||
def start | ||
prepare | ||
engine.start | ||
end | ||
|
||
def engine | ||
load_engine options.fetch(:engine) { engine_lookup } | ||
end | ||
|
||
private | ||
|
||
def prepare | ||
# Clear out ARGV so Pry/IRB don't attempt to parse the rest | ||
ARGV.shift until ARGV.empty? | ||
|
||
# Add convenience methods to the main:Object binding | ||
TOPLEVEL_BINDING.eval('self').__send__(:include, CodeReloading) | ||
end | ||
|
||
def engine_lookup | ||
(ENGINES.find { |_, klass| Object.const_defined?(klass) } || DEFAULT_ENGINE).first | ||
end | ||
|
||
def load_engine(engine) | ||
require engine | ||
rescue LoadError | ||
ensure | ||
return Object.const_get( | ||
ENGINES.fetch(engine) do | ||
raise ArgumentError.new("Unknown console engine: `#{engine}'") | ||
end | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters