Skip to content

Commit

Permalink
Introducing lotus runner command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Uceda Pompa committed Jan 6, 2016
1 parent 7473a77 commit c2b5773
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/lotus/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ def routes
end
end

desc 'runner [FILE|EXPRESSION]', 'Evaluate a file or expression with the preloaded environment and application'
long_desc <<-EOS
`lotus runner` Evaluate a file or expression with the preloaded environment and application.
$ > lotus runner `puts Lotus.env` => The current environment
$ > lotus runner ./my_script.rb => Loads the file.
EOS
method_option :help, desc: 'displays the usage method'
def runner(expression_or_file)
if options[:help]
invoke :help, ['runner']
else
require 'lotus/commands/runner'
Lotus::Commands::Runner.new(options, expression_or_file).start
end
end

require 'lotus/cli_sub_commands/db'
register Lotus::CliSubCommands::DB, 'db', 'db [SUBCOMMAND]', 'manage set of DB operations'

Expand Down
53 changes: 53 additions & 0 deletions lib/lotus/commands/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'lotus/environment'
require 'lotus/application'

module Lotus
module Commands
# Evaluate a file or expression with the preloaded environment and application.
#
# It is run with:
#
# `bundle exec lotus runner ./my_script.rb`
#
# or
#
# `bundle exec lotus runner 'puts Lotus.env'`
#
# @since x.x.x
# @api private
class Runner
# @since x.x.x
# @api private
class NoArgumentGivenException < ::StandardError
end

# @param options [Hash] Environment's options
# @param expression_or_file [String] Expression or file to evaluate
#
# @since x.x.x
# @see Lotus::Environment#initialize
# @see Lotus::Environment#require_application_environment
# @see Lotus::Application.preload!
def initialize(options, expression_or_file)
@expression_or_file = expression_or_file
raise NoArgumentGivenException.new if @expression_or_file.nil?

@environment = Lotus::Environment.new(options)
@environment.require_application_environment
Lotus::Application.preload!
end

# Evaluate a file or expression with the preloaded environment and application.
#
# @since x.x.x
def start
if File.exists?(@expression_or_file)
$0 = @expression_or_file
Kernel.load(@expression_or_file)
else
eval(@expression_or_file, binding, __FILE__, __LINE__)
end
end
end
end
end
39 changes: 39 additions & 0 deletions test/commands/runner_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'test_helper'
require 'lotus/commands/runner'

describe Lotus::Commands::Runner do
before do
Lotus::Application.applications.clear
@old_pwd = Dir.pwd
Dir.chdir 'test/fixtures/microservices'
end

after do
Dir.chdir @old_pwd
end

describe '#start' do
it 'evaluate expression' do
command = Lotus::Commands::Runner.new({}, 'puts Lotus.env')
output, _ = capture_io { command.start }
output.chomp.must_equal 'development'
end

it 'evaluate ruby file' do
command = Lotus::Commands::Runner.new({}, 'apps/backend/scripts/script_001.rb')
output, _ = capture_io { command.start }
output.chomp.must_equal 'development'
end

it 'evaluate ruby file and return $0' do
filename = 'apps/backend/scripts/script_002.rb'
command = Lotus::Commands::Runner.new({}, filename)
output, _ = capture_io { command.start }
output.chomp.must_equal filename
end

it 'no argument given' do
-> { Lotus::Commands::Runner.new({}, nil) }.must_raise Lotus::Commands::Runner::NoArgumentGivenException
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts Lotus.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts $0

0 comments on commit c2b5773

Please sign in to comment.