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
|
# frozen_string_literal: true
require 'babosa'
require_relative '../logic/helpers/hash'
require_relative './crypto'
module NanoBot
module Components
class Storage
def self.build_path_and_ensure_state_file!(key, cartridge, environment: {})
path = [
Logic::Helpers::Hash.fetch(cartridge, %i[state directory]),
ENV.fetch('NANO_BOTS_STATE_DIRECTORY', nil)
].find do |candidate|
!candidate.nil? && !candidate.empty?
end
path = "#{user_home!.sub(%r{/$}, '')}/.local/state/nano-bots" if path.nil?
prefix = environment && (
environment['NANO_BOTS_USER_IDENTIFIER'] ||
environment[:NANO_BOTS_USER_IDENTIFIER]
)
path = "#{path.sub(%r{/$}, '')}/ruby-nano-bots/vault"
if prefix
normalized = prefix.split('/').map do |part|
Crypto.encrypt(
part.to_s.gsub('.', '-').force_encoding('UTF-8').to_slug.normalize,
soft: true
)
end.join('/')
path = "#{path}/#{normalized}"
end
path = "#{path}/#{cartridge[:meta][:author].to_slug.normalize}"
path = "#{path}/#{cartridge[:meta][:name].to_slug.normalize}"
path = "#{path}/#{cartridge[:meta][:version].to_s.gsub('.', '-').to_slug.normalize}"
path = "#{path}/#{Crypto.encrypt(key, soft: true)}"
path = "#{path}/state.json"
FileUtils.mkdir_p(File.dirname(path))
unless File.exist?(path)
File.write(
path,
Crypto.encrypt(JSON.generate({ key:, history: [] }))
)
end
path
end
def self.cartridges_path
[
ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil),
"#{user_home!.sub(%r{/$}, '')}/.local/share/nano-bots/cartridges"
].compact.uniq.filter { |path| File.directory?(path) }.compact.first
end
def self.cartridge_path(path)
partial = File.join(File.dirname(path), File.basename(path, File.extname(path)))
candidates = [
path,
"#{partial}.yml",
"#{partial}.yaml"
]
unless ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY', nil).nil?
directory = ENV.fetch('NANO_BOTS_CARTRIDGES_DIRECTORY').sub(%r{/$}, '')
partial = File.join(File.dirname(partial), File.basename(partial, File.extname(partial)))
partial = partial.sub(%r{^\.?/}, '')
candidates << "#{directory}/#{partial}"
candidates << "#{directory}/#{partial}.yml"
candidates << "#{directory}/#{partial}.yaml"
end
directory = "#{user_home!.sub(%r{/$}, '')}/.local/share/nano-bots/cartridges"
partial = File.join(File.dirname(partial), File.basename(partial, File.extname(partial)))
partial = partial.sub(%r{^\.?/}, '')
candidates << "#{directory}/#{partial}"
candidates << "#{directory}/#{partial}.yml"
candidates << "#{directory}/#{partial}.yaml"
candidates = candidates.uniq
candidates.find do |candidate|
File.exist?(candidate) && File.file?(candidate)
end
end
def self.user_home!
[Dir.home, `echo ~`.strip, '~'].find do |candidate|
!candidate.nil? && !candidate.empty?
end
end
end
end
end
|