Sha256: 24f714920d16dc07c3f593850b8b66dc8ee6f858891968f9f0efb86566420c49

Contents?: true

Size: 1.98 KB

Versions: 14

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module RBS
  module Collection

    # This class represent the configuration file.
    class Config
      class CollectionNotAvailable < StandardError
        def initialize
          super <<~MSG
            rbs collection is not initialized.
            Run `rbs collection install` to install RBSs from collection.
          MSG
        end
      end

      PATH = Pathname('rbs_collection.yaml')

      attr_reader :config_path, :data

      def self.find_config_path
        current = Pathname.pwd

        loop do
          config_path = current.join(PATH)
          return config_path if config_path.exist?
          current = current.join('..')
          return nil if current.root?
        end
      end

      # Generate a rbs lockfile from Gemfile.lock to `config_path`.
      # If `with_lockfile` is true, it respects existing rbs lockfile.
      def self.generate_lockfile(config_path:, definition:, with_lockfile: true)
        config = from_path(config_path)
        lockfile = LockfileGenerator.generate(config: config, definition: definition, with_lockfile: with_lockfile)

        [config, lockfile]
      end

      def self.from_path(path)
        new(YAML.load(path.read), config_path: path)
      end

      def self.to_lockfile_path(config_path)
        config_path.sub_ext('.lock' + config_path.extname)
      end

      def initialize(data, config_path:)
        @data = data
        @config_path = config_path
      end

      def gem(gem_name)
        gems.find { |gem| gem['name'] == gem_name }
      end

      def repo_path
        @config_path.dirname.join repo_path_data
      end

      def repo_path_data
        Pathname(@data["path"])
      end

      def sources
        @sources ||= [
          Sources::Rubygems.instance,
          Sources::Stdlib.instance,
          *@data['sources'].map { |c| Sources.from_config_entry(c, base_directory: @config_path.dirname) }
        ]
      end

      def gems
        @data['gems'] ||= []
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rbs-3.7.0.dev.1 lib/rbs/collection/config.rb
rbs-3.6.1 lib/rbs/collection/config.rb
rbs-3.6.0 lib/rbs/collection/config.rb
rbs-3.6.0.pre.3 lib/rbs/collection/config.rb
rbs-3.6.0.pre.2 lib/rbs/collection/config.rb
rbs-3.6.0.pre.1 lib/rbs/collection/config.rb
rbs-3.6.0.dev.1 lib/rbs/collection/config.rb
rbs-3.5.3 lib/rbs/collection/config.rb
rbs-3.5.2 lib/rbs/collection/config.rb
rbs-3.5.1 lib/rbs/collection/config.rb
rbs-3.5.1.pre.1 lib/rbs/collection/config.rb
rbs-3.5.0 lib/rbs/collection/config.rb
rbs-3.5.0.pre.2 lib/rbs/collection/config.rb
rbs-3.5.0.pre.1 lib/rbs/collection/config.rb