Skip to content

Commit

Permalink
stealth console!
Browse files Browse the repository at this point in the history
  • Loading branch information
mgomes committed Sep 26, 2017
1 parent 5e538d1 commit 18f4c87
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PATH
hanami-router (~> 1.0)
hanami-utils (~> 1.0)
hanami-validations (~> 1.0)
thor (~> 0.20)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -75,6 +76,7 @@ GEM
rspec-support (3.6.0)
rspec_junit_formatter (0.3.0)
rspec-core (>= 2, < 4, != 2.12.0)
thor (0.20.0)
transproc (1.0.2)
url_mount (0.2.1)
rack
Expand Down
2 changes: 1 addition & 1 deletion bin/stealth
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
36 changes: 36 additions & 0 deletions lib/stealth/cli.rb
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
22 changes: 22 additions & 0 deletions lib/stealth/cli_base.rb
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
11 changes: 11 additions & 0 deletions lib/stealth/commands/command.rb
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
70 changes: 70 additions & 0 deletions lib/stealth/commands/console.rb
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
1 change: 1 addition & 0 deletions stealth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |s|
s.add_dependency 'hanami-router', '~> 1.0'
s.add_dependency 'hanami-controller', '~> 1.0'
s.add_dependency 'dotenv', '~> 2.0'
s.add_dependency 'thor', '~> 0.20'

s.add_development_dependency 'rspec', '~> 3.6.0'
s.add_development_dependency 'rspec_junit_formatter', '~> 0.3.0'
Expand Down

0 comments on commit 18f4c87

Please sign in to comment.