Class: RBS::EnvironmentLoader
- Inherits:
-
Object
- Object
- RBS::EnvironmentLoader
show all
- Defined in:
- lib/rbs/environment_loader.rb
Defined Under Namespace
Classes: UnknownLibraryError
Constant Summary
collapse
- Library =
_ = Struct.new(:name, :version, keyword_init: true)
- DEFAULT_CORE_ROOT =
Pathname(_ = __dir__) + "../../core"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(core_root: DEFAULT_CORE_ROOT, repository: Repository.new) ⇒ EnvironmentLoader
Returns a new instance of EnvironmentLoader.
33
34
35
36
37
38
39
|
# 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
|
Instance Attribute Details
#core_root ⇒ Object
Returns the value of attribute core_root.
15
16
17
|
# File 'lib/rbs/environment_loader.rb', line 15
def core_root
@core_root
end
|
#dirs ⇒ Object
Returns the value of attribute dirs.
19
20
21
|
# File 'lib/rbs/environment_loader.rb', line 19
def dirs
@dirs
end
|
#libs ⇒ Object
Returns the value of attribute libs.
18
19
20
|
# File 'lib/rbs/environment_loader.rb', line 18
def libs
@libs
end
|
#repository ⇒ Object
Returns the value of attribute repository.
16
17
18
|
# File 'lib/rbs/environment_loader.rb', line 16
def repository
@repository
end
|
Class Method Details
.gem_sig_path(name, version) ⇒ Object
23
24
25
26
27
28
29
30
31
|
# 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 Method Details
#add(path: nil, library: nil, version: nil) ⇒ Object
41
42
43
44
45
46
47
48
|
# 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
|
#add_collection(collection_config) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/rbs/environment_loader.rb', line 50
def add_collection(collection_config)
warn "warning: rbs collection is experimental, and the behavior may change until RBS v2.0"
collection_config.check_rbs_availability!
repository.add(collection_config.repo_path)
collection_config.gems.each do |gem|
add(library: gem['name'], version: gem['version'])
end
end
|
#each_decl ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/rbs/environment_loader.rb', line 127
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 ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/rbs/environment_loader.rb', line 82
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) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/rbs/environment_loader.rb', line 105
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
62
63
64
65
66
67
68
|
# File 'lib/rbs/environment_loader.rb', line 62
def has_library?(library:, version:)
if self.class.gem_sig_path(library, version) || repository.lookup(library, version)
true
else
false
end
end
|
#load(env:) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/rbs/environment_loader.rb', line 70
def load(env:)
loaded = []
each_decl do |decl, buf, source, path|
env << decl
loaded << [decl, path, source]
end
loaded
end
|