summaryrefslogtreecommitdiff
path: root/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'controllers')
-rw-r--r--controllers/instance.rb49
-rw-r--r--controllers/interfaces/cli.rb34
-rw-r--r--controllers/session.rb17
3 files changed, 75 insertions, 25 deletions
diff --git a/controllers/instance.rb b/controllers/instance.rb
index f3cbd74..b22a785 100644
--- a/controllers/instance.rb
+++ b/controllers/instance.rb
@@ -12,35 +12,64 @@ require_relative './session'
module NanoBot
module Controllers
class Instance
- def initialize(cartridge_path:, state: nil)
+ def initialize(cartridge_path:, stream:, state: nil)
+ @stream = stream
+
load_cartridge!(cartridge_path)
provider = Components::Provider.new(@cartridge[:provider])
- @session = Session.new(provider:, cartridge: @cartridge, state:)
+ @session = Session.new(provider:, cartridge: @cartridge, state:, stream: @stream)
+ end
+
+ def cartridge
+ puts YAML.dump(@safe_cartridge)
end
- def debug
- @session.debug
+ def state
+ @session.state
end
def eval(input)
Interfaces::Eval.evaluate(input, @cartridge, @session)
+
+ return unless @stream.is_a?(StringIO)
+
+ @stream.flush
+ result = @stream.string.clone
+ @stream.truncate(0)
+ @stream.rewind
+ result
end
def repl
+ if @stream.is_a?(StringIO)
+ @stream.flush
+ @stream = $stdout
+ @session.stream = @stream
+ end
Interfaces::REPL.start(@cartridge, @session)
end
private
def load_cartridge!(path)
- @cartridge = Logic::Helpers::Hash.symbolize_keys(
- YAML.safe_load(
- File.read(Components::Storage.cartridge_path(path)),
- permitted_classes: [Symbol]
- )
- )
+ elected_path = if path.strip == '-'
+ File.expand_path('../static/cartridges/default.yml', __dir__)
+ else
+ Components::Storage.cartridge_path(path)
+ end
+
+ if elected_path.nil?
+ @stream.write("Cartridge file not found: \"#{path}\"\n")
+ raise StandardError, "Cartridge file not found: \"#{path}\""
+ end
+
+ @cartridge = YAML.safe_load(File.read(elected_path), permitted_classes: [Symbol])
+
+ @safe_cartridge = Marshal.load(Marshal.dump(@cartridge))
+
+ @cartridge = Logic::Helpers::Hash.symbolize_keys(@cartridge)
inject_environment_variables!(@cartridge)
end
diff --git a/controllers/interfaces/cli.rb b/controllers/interfaces/cli.rb
index 3574a80..2a93044 100644
--- a/controllers/interfaces/cli.rb
+++ b/controllers/interfaces/cli.rb
@@ -13,20 +13,38 @@ module NanoBot
puts NanoBot::GEM[:version]
exit
when 'help', '', nil
+ puts ''
puts "Nano Bots #{NanoBot::GEM[:version]}"
- puts ' nb cartridge.yml - eval "Hello"'
+ puts ''
+ puts ' nb - - eval "hello"'
+ puts ' nb - - repl'
+ puts ''
+ puts ' nb cartridge.yml - eval "hello"'
puts ' nb cartridge.yml - repl'
- puts ' nb cartridge.yml - debug'
- puts ' nb cartridge.yml STATE-KEY eval "Hello"'
+ puts ''
+ puts ' nb - STATE-KEY eval "hello"'
+ puts ' nb - STATE-KEY repl'
+ puts ''
+ puts ' nb cartridge.yml STATE-KEY eval "hello"'
puts ' nb cartridge.yml STATE-KEY repl'
- puts ' nb cartridge.yml STATE-KEY debug'
+ puts ''
+ puts ' nb - - cartridge'
+ puts ' nb cartridge.yml - cartridge'
+ puts ''
+ puts ' nb - STATE-KEY state'
+ puts ' nb cartridge.yml STATE-KEY state'
+ puts ''
puts ' nb version'
+ puts ' nb help'
+ puts ''
exit
end
params = { cartridge_path: ARGV[0], state: ARGV[1], command: ARGV[2] }
- bot = Instance.new(cartridge_path: params[:cartridge_path], state: params[:state])
+ bot = Instance.new(
+ cartridge_path: params[:cartridge_path], state: params[:state], stream: $stdout
+ )
case params[:command]
when 'eval'
@@ -35,8 +53,10 @@ module NanoBot
bot.eval(params[:input])
when 'repl'
bot.repl
- when 'debug'
- bot.debug
+ when 'state'
+ bot.state
+ when 'cartridge'
+ bot.cartridge
else
raise "TODO: [#{params[:command]}]"
end
diff --git a/controllers/session.rb b/controllers/session.rb
index ff3b8ad..afaca18 100644
--- a/controllers/session.rb
+++ b/controllers/session.rb
@@ -12,12 +12,13 @@ module NanoBot
STREAM_TIMEOUT_IN_SECONDS = 5
class Session
- def initialize(provider:, cartridge:, state: nil)
+ attr_accessor :stream
+
+ def initialize(provider:, cartridge:, state: nil, stream: $stdout)
+ @stream = stream
@provider = provider
@cartridge = cartridge
- @output = $stdout
-
@stateless = state.nil? || state.strip == '-' || state.strip.empty?
if @stateless
@@ -30,7 +31,7 @@ module NanoBot
end
end
- def debug
+ def state
pp({
state: {
path: @state_path,
@@ -67,7 +68,7 @@ module NanoBot
process(input, mode:)
end
- def stream(interface)
+ def streaming(interface)
provider = @provider.settings.key?(:stream) ? @provider.settings[:stream] : true
interface = interface.key?(:stream) ? interface[:stream] : true
@@ -77,7 +78,7 @@ module NanoBot
def process(input, mode:)
interface = Logic::Helpers::Hash.fetch(@cartridge, [:interfaces, mode.to_sym]) || {}
- streaming = stream(interface)
+ streaming = streaming(interface)
input[:interface] = interface
@@ -105,11 +106,11 @@ module NanoBot
end
def flush
- @output.flush
+ @stream.flush
end
def print(content)
- @output.write(content)
+ @stream.write(content)
end
end
end