Class: RBS::EnvironmentLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/environment_loader.rb

Defined Under Namespace

Classes: UnknownLibraryNameError

Constant Summary collapse

LibraryPath =
_ = Struct.new(:name, :path, keyword_init: true)
GemPath =
_ = Struct.new(:name, :version, :path, keyword_init: true)
STDLIB_ROOT =
Pathname(_ = __dir__) + "../../stdlib"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdlib_root: STDLIB_ROOT, gem_vendor_path: nil) ⇒ EnvironmentLoader

Returns a new instance of EnvironmentLoader.



27
28
29
30
31
32
# File 'lib/rbs/environment_loader.rb', line 27

def initialize(stdlib_root: STDLIB_ROOT, gem_vendor_path: nil)
  @stdlib_root = stdlib_root
  @gem_vendor_path = gem_vendor_path
  @paths = []
  @no_builtin = false
end

Instance Attribute Details

#gem_vendor_pathObject (readonly)

Returns the value of attribute gem_vendor_path.



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

def gem_vendor_path
  @gem_vendor_path
end

#pathsObject (readonly)

Returns the value of attribute paths.



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

def paths
  @paths
end

#stdlib_rootObject (readonly)

Returns the value of attribute stdlib_root.



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

def stdlib_root
  @stdlib_root
end

Class Method Details

.gem_sig_path(name, version) ⇒ Object



21
22
23
24
25
# File 'lib/rbs/environment_loader.rb', line 21

def self.gem_sig_path(name, version)
  Pathname(Gem::Specification.find_by_name(name, version).gem_dir) + "sig"
rescue Gem::MissingSpecError
  nil
end

.parse_library(lib) ⇒ Object



52
53
54
# File 'lib/rbs/environment_loader.rb', line 52

def self.parse_library(lib)
  _ = lib.split(/:/)
end

Instance Method Details

#add(path: nil, library: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbs/environment_loader.rb', line 34

def add(path: nil, library: nil)
  case
  when path
    @paths << path
  when library
    name, version = self.class.parse_library(library)

    case
    when !version && path = stdlib?(name)
      @paths << LibraryPath.new(name: name, path: path)
    when (path = gem?(name, version))
      @paths << GemPath.new(name: name, version: version, path: path)
    else
      raise UnknownLibraryNameError.new(name: library)
    end
  end
end

#each_declObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rbs/environment_loader.rb', line 115

def each_decl
  if block_given?
    # @type var signature_files: Array[[path | :stdlib, Pathname]]
    signature_files = []

    unless no_builtin?
      each_signature(stdlib_root + "builtin") do |path|
        signature_files << [:stdlib, path]
      end
    end

    each_library_path do |library_path, pathname|
      each_signature(pathname) do |path|
        signature_files << [library_path, path]
      end
    end

    signature_files.each do |lib_path, file_path|
      buffer = Buffer.new(name: file_path.to_s, content: file_path.read)
      Parser.parse_signature(buffer).each do |decl|
        yield decl, buffer, file_path, lib_path
      end
    end
  else
    enum_for :each_decl
  end
end

#each_library_pathObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rbs/environment_loader.rb', line 93

def each_library_path
  paths.each do |path|
    case path
    when Pathname
      yield path, path
    when LibraryPath
      yield path, path.path
    when GemPath
      yield path, path.path
    end
  end
end

#each_signature(path, immediate: true, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbs/environment_loader.rb', line 76

def each_signature(path, immediate: true, &block)
  if block
    case
    when path.file?
      if path.extname == ".rbs" || immediate
        yield path
      end
    when path.directory?
      path.children.each do |child|
        each_signature child, immediate: false, &block
      end
    end
  else
    enum_for :each_signature, path, immediate: immediate
  end
end

#gem?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbs/environment_loader.rb', line 63

def gem?(name, version)
  if path = gem_vendor_path
    # Try vendored RBS first
    gem_dir = path + name
    if gem_dir.directory?
      return gem_dir
    end
  end

  # Try ruby gem library
  self.class.gem_sig_path(name, version)
end

#load(env:) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rbs/environment_loader.rb', line 143

def load(env:)
  # @type var loadeds: Array[[AST::Declarations::t, Pathname, path | :stdlib]]
  loadeds = []

  each_decl do |decl, buffer, file_path, lib_path|
    env.buffers.push(buffer)
    env << decl
    loadeds << [decl, file_path, lib_path]
  end

  loadeds
end

#no_builtin!(skip = true) ⇒ Object



106
107
108
109
# File 'lib/rbs/environment_loader.rb', line 106

def no_builtin!(skip = true)
  @no_builtin = skip
  self
end

#no_builtin?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/rbs/environment_loader.rb', line 111

def no_builtin?
  @no_builtin
end

#stdlib?(name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/rbs/environment_loader.rb', line 56

def stdlib?(name)
  path = stdlib_root + name
  if path.directory?
    path
  end
end