summaryrefslogtreecommitdiff
path: root/controllers/cartridges.rb
diff options
context:
space:
mode:
Diffstat (limited to 'controllers/cartridges.rb')
-rw-r--r--controllers/cartridges.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/controllers/cartridges.rb b/controllers/cartridges.rb
new file mode 100644
index 0000000..3e3b192
--- /dev/null
+++ b/controllers/cartridges.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+require_relative '../components/storage'
+require_relative '../logic/helpers/hash'
+require_relative '../logic/cartridge/default'
+
+module NanoBot
+ module Controllers
+ class Cartridges
+ def self.all
+ files = {}
+
+ path = Components::Storage.cartridges_path
+
+ Dir.glob("#{path}/**/*.{yml,yaml}").each do |file|
+ files[Pathname.new(file).realpath] = {
+ base: path,
+ path: Pathname.new(file).realpath
+ }
+ end
+
+ cartridges = []
+
+ files.values.uniq.map do |file|
+ cartridge = Logic::Helpers::Hash.symbolize_keys(
+ YAML.safe_load(File.read(file[:path]), permitted_classes: [Symbol])
+ ).merge({
+ system: {
+ id: file[:path].to_s.sub(/^#{Regexp.escape(file[:base])}/, '').sub(%r{^/}, '').sub(/\.[^.]+\z/,
+ ''),
+ path: file[:path],
+ base: file[:base]
+ }
+ })
+
+ next if cartridge[:meta][:name].nil?
+
+ cartridges << cartridge
+ # rescue StandardError => _e
+ end
+
+ cartridges.sort_by { |cartridge| cartridge[:meta][:name] }
+
+ cartridges.prepend(
+ { system: { id: '-' }, meta: { name: 'Default', symbol: '🤖' } }
+ )
+
+ cartridges
+ end
+ end
+ end
+end