diff options
Diffstat (limited to 'controllers')
-rw-r--r-- | controllers/instance.rb | 6 | ||||
-rw-r--r-- | controllers/session.rb | 27 |
2 files changed, 9 insertions, 24 deletions
diff --git a/controllers/instance.rb b/controllers/instance.rb index 5635658..bee4dfb 100644 --- a/controllers/instance.rb +++ b/controllers/instance.rb @@ -4,6 +4,7 @@ require 'yaml' require_relative '../logic/helpers/hash' require_relative '../components/provider' +require_relative '../components/storage' require_relative './interfaces/repl' require_relative './session' @@ -34,7 +35,10 @@ module NanoBot def load_cartridge!(path) @cartridge = Logic::Helpers::Hash.symbolize_keys( - YAML.safe_load(File.read(path), permitted_classes: [Symbol]) + YAML.safe_load( + File.read(Components::Storage.cartridge_path(path)), + permitted_classes: [Symbol] + ) ) inject_environment_variables!(@cartridge) diff --git a/controllers/session.rb b/controllers/session.rb index fadb21b..9277a84 100644 --- a/controllers/session.rb +++ b/controllers/session.rb @@ -5,6 +5,7 @@ require 'babosa' require 'fileutils' require_relative '../logic/helpers/hash' +require_relative '../components/storage' module NanoBot module Controllers @@ -22,7 +23,9 @@ module NanoBot if @stateless @state = { history: [] } else - build_path_and_ensure_state_file!(state.strip) + @state_path = Components::Storage.build_path_and_ensure_state_file!( + state.strip, @cartridge + ) @state = load_state end end @@ -44,28 +47,6 @@ module NanoBot File.write(@state_path, JSON.generate(@state)) end - def build_path_and_ensure_state_file!(key) - path = Logic::Helpers::Hash.fetch(@cartridge, %i[state directory]) - - path = "#{user_home!.sub(%r{/$}, '')}/.local/share/.nano-bots" if path.nil? || path.empty? - - path = "#{path.sub(%r{/$}, '')}/nano-bots-rb/#{@cartridge[:name].to_slug.normalize}" - path = "#{path}/#{@cartridge[:version].to_slug.normalize}/#{key.to_slug.normalize}" - path = "#{path}/state.json" - - @state_path = path - - FileUtils.mkdir_p(File.dirname(@state_path)) - - File.write(@state_path, JSON.generate({ key:, history: [] })) unless File.exist?(@state_path) - end - - def user_home! - [Dir.home, `echo ~`.strip, '~'].find do |candidate| - !candidate.nil? && !candidate.empty? - end - end - def boot(mode:) return unless Logic::Helpers::Hash.fetch(@cartridge, %i[behaviors boot instruction]) |