-
-
Notifications
You must be signed in to change notification settings - Fork 542
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
Alfonso Uceda Pompa
committed
Jan 6, 2016
1 parent
7473a77
commit c2b5773
Showing
5 changed files
with
112 additions
and
0 deletions.
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 |
---|---|---|
@@ -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 |
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,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 |
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 @@ | ||
puts Lotus.env |
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 @@ | ||
puts $0 |