summaryrefslogtreecommitdiff
path: root/controllers/instance.rb
blob: 259a54886f3fac3157006ee3c708ee98625e822c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

require 'yaml'

require_relative '../logic/helpers/hash'
require_relative '../components/provider'
require_relative '../components/storage'
require_relative '../components/stream'
require_relative 'interfaces/repl'
require_relative 'interfaces/eval'
require_relative 'session'

module NanoBot
  module Controllers
    class Instance
      def initialize(cartridge_path:, stream:, state: nil, environment: {})
        @stream = stream

        load_cartridge!(cartridge_path)

        provider = Components::Provider.new(@cartridge[:provider], environment:)

        @session = Session.new(
          provider:, cartridge: @cartridge, state:, stream: @stream, environment:
        )
      end

      def cartridge
        @safe_cartridge
      end

      def state
        @session.state
      end

      def boot(as: 'eval', &block)
        @stream.callback = block if block && @stream.is_a?(Components::Stream)

        Interfaces::REPL.boot(@cartridge, @session, as:)

        return unless @stream.is_a?(Components::Stream)

        @stream.finish
      end

      def prompt
        Interfaces::REPL.prompt(@cartridge)
      end

      def eval(input, as: 'eval', &block)
        @stream.callback = block if block && @stream.is_a?(Components::Stream)

        Interfaces::Eval.evaluate(input, @cartridge, @session, as)

        return unless @stream.is_a?(Components::Stream)

        @stream.finish
      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)
        if !path.is_a?(String) && !path.is_a?(Symbol)
          @cartridge = path
        else
          elected_path = if path.strip == '-'
                           File.expand_path('../static/cartridges/baseline.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(elected_path, permitted_classes: [Symbol])
        end

        @safe_cartridge = Marshal.load(Marshal.dump(@cartridge))

        @cartridge = Logic::Helpers::Hash.symbolize_keys(@cartridge)

        inject_environment_variables!(@cartridge)
      end

      def inject_environment_variables!(node)
        case node
        when Hash
          node.each do |key, value|
            node[key] = inject_environment_variables!(value)
          end
        when Array
          node.each_with_index do |value, index|
            node[index] = inject_environment_variables!(value)
          end
        when String
          node.start_with?('ENV') ? ENV.fetch(node.sub(/^ENV./, ''), nil) : node
        else
          node
        end
      end
    end
  end
end