123456789_123456789_123456789_123456789_123456789_

Class: RBS::EnvironmentLoader

Relationships & Source Files
Namespace Children
Exceptions:
Inherits: Object
Defined in: lib/rbs/environment_loader.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(core_root: DEFAULT_CORE_ROOT, repository: Repository.new) ⇒ EnvironmentLoader

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 33

def initialize(core_root: DEFAULT_CORE_ROOT, repository: Repository.new)
  @core_root = core_root
  @repository = repository

  @libs = []
  @dirs = []
end

Class Method Details

.gem_sig_path(name, version)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 23

def self.gem_sig_path(name, version)
  spec = Gem::Specification.find_by_name(name, version)
  path = Pathname(spec.gem_dir) + "sig"
  if path.directory?
    [spec, path]
  end
rescue Gem::MissingSpecError
  nil
end

Instance Attribute Details

#core_root (readonly)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 15

attr_reader :core_root

#dirs (readonly)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 19

attr_reader :dirs

#libs (readonly)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 18

attr_reader :libs

#repository (readonly)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 16

attr_reader :repository

Instance Method Details

#add(path: nil, library: nil, version: nil)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 41

def add(path: nil, library: nil, version: nil)
  case
  when path
    dirs << path
  when library
    libs << Library.new(name: library, version: version)
  end
end

#each_decl

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 115

def each_decl
  files = Set[]

  each_dir do |source, dir|
    skip_hidden = !source.is_a?(Pathname)

    each_file(dir, skip_hidden: skip_hidden, immediate: true) do |path|
      next if files.include?(path)

      files << path
      buffer = Buffer.new(name: path.to_s, content: path.read(encoding: "UTF-8"))

      Parser.parse_signature(buffer).each do |decl|
        yield decl, buffer, source, path
      end
    end
  end
end

#each_dir

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 70

def each_dir
  if root = core_root
    yield :core, root
  end

  libs.each do |lib|
    unless has_library?(version: lib.version, library: lib.name)
      raise UnknownLibraryError.new(lib: lib)
    end

    case
    when from_gem = self.class.gem_sig_path(lib.name, lib.version)
      yield lib, from_gem[1]
    when from_repo = repository.lookup(lib.name, lib.version)
      yield lib, from_repo
    end
  end

  dirs.each do |dir|
    yield dir, dir
  end
end

#each_file(path, immediate:, skip_hidden:, &block)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 93

def each_file(path, immediate:, skip_hidden:, &block)
  case
  when path.file?
    if path.extname == ".rbs" || immediate
      yield path
    end

  when path.directory?
    if path.basename.to_s.start_with?("_")
      if skip_hidden
        unless immediate
          return
        end
      end
    end

    path.children.sort.each do |child|
      each_file(child, immediate: false, skip_hidden: skip_hidden, &block)
    end
  end
end

#has_library?(library:, version:) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 50

def has_library?(library:, version:)
  if self.class.gem_sig_path(library, version) || repository.lookup(library, version)
    true
  else
    false
  end
end

#load(env:)

[ GitHub ]

  
# File 'lib/rbs/environment_loader.rb', line 58

def load(env:)
  # @type var loaded: Array[[AST::Declarations::t, Pathname, source]]
  loaded = []

  each_decl do |decl, buf, source, path|
    env << decl
    loaded << [decl, path, source]
  end

  loaded
end