Sha256: 59b9a0fcd420858a4b31b64c3867393ef71dfcc5b071f25da44a56743da0edfc

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require_relative '../components/storage'
require_relative '../logic/helpers/hash'
require_relative '../logic/cartridge/default'
require_relative '../logic/cartridge/parser'

module NanoBot
  module Controllers
    class Cartridges
      def self.load(path)
        Logic::Cartridge::Parser.parse(File.read(path), format: File.extname(path))
      end

      def self.all(components: {})
        files = {}

        paths = Components::Storage.cartridges_path(components:)

        paths.split(':').each do |path|
          Dir.glob("#{path}/**/*.{yml,yaml,markdown,mdown,mkdn,md}").each do |file|
            files[Pathname.new(file).realpath] = {
              base: path,
              path: Pathname.new(file).realpath
            }
          end
        end

        cartridges = []

        files.values.uniq.map do |file|
          cartridge = load(file[:path]).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 = cartridges.sort_by { |cartridge| cartridge[:meta][:name] }

        cartridges.prepend(
          { system: { id: '-' }, meta: { name: 'Default', symbol: '🤖' } }
        )

        cartridges
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nano-bots-3.4.0 controllers/cartridges.rb
nano-bots-3.3.0 controllers/cartridges.rb
nano-bots-3.2.0 controllers/cartridges.rb
nano-bots-3.0.1 controllers/cartridges.rb
nano-bots-3.0.0 controllers/cartridges.rb