Class: RBS::EnvironmentLoader
- Inherits:
-
Object
- Object
- RBS::EnvironmentLoader
show all
- Includes:
- FileFinder
- Defined in:
- lib/rbs/environment_loader.rb
Defined Under Namespace
Classes: Library, UnknownLibraryError
Constant Summary
collapse
- DEFAULT_CORE_ROOT =
Pathname(_ = __dir__) + "../../core"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from FileFinder
each_file
Constructor Details
#initialize(core_root: DEFAULT_CORE_ROOT, repository: Repository.new) ⇒ EnvironmentLoader
Returns a new instance of EnvironmentLoader.
40
41
42
43
44
45
46
|
# File 'lib/rbs/environment_loader.rb', line 40
def initialize(core_root: DEFAULT_CORE_ROOT, repository: Repository.new)
@core_root = core_root
@repository = repository
@libs = Set.new
@dirs = []
end
|
Instance Attribute Details
#core_root ⇒ Object
Returns the value of attribute core_root.
20
21
22
|
# File 'lib/rbs/environment_loader.rb', line 20
def core_root
@core_root
end
|
#dirs ⇒ Object
Returns the value of attribute dirs.
24
25
26
|
# File 'lib/rbs/environment_loader.rb', line 24
def dirs
@dirs
end
|
#libs ⇒ Object
Returns the value of attribute libs.
23
24
25
|
# File 'lib/rbs/environment_loader.rb', line 23
def libs
@libs
end
|
#repository ⇒ Object
Returns the value of attribute repository.
21
22
23
|
# File 'lib/rbs/environment_loader.rb', line 21
def repository
@repository
end
|
Class Method Details
.gem_sig_path(name, version) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/rbs/environment_loader.rb', line 28
def self.gem_sig_path(name, version)
requirements = []
requirements << version if version
spec = Gem::Specification.find_by_name(name, *requirements)
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, resolve_dependencies: true) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/rbs/environment_loader.rb', line 48
def add(path: nil, library: nil, version: nil, resolve_dependencies: true)
case
when path
dirs << path
when library
case library
when 'rubygems', 'set'
RBS.logger.warn "`#{library}` has been moved to core library, so it is always loaded. Remove explicit loading `#{library}`"
return
end
if libs.add?(Library.new(name: library, version: version)) && resolve_dependencies
resolve_dependencies(library: library, version: version)
end
end
end
|
#add_collection(lockfile) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/rbs/environment_loader.rb', line 80
def add_collection(lockfile)
lockfile.check_rbs_availability!
repository.add(lockfile.fullpath)
lockfile.gems.each_value do |gem|
name = gem[:name]
locked_version = gem[:version]
if (source = gem[:source]).is_a?(Collection::Sources::Rubygems)
unless source.has?(name, locked_version)
if (spec, _ = self.class.gem_sig_path(name, nil))
RBS.logger.warn { "Loading type definition from gem `#{name}-#{spec.version}` because locked version `#{locked_version}` is unavailable. Try `rbs collection update` to fix the (potential) issue." }
locked_version = spec.version.to_s
end
end
end
add(library: gem[:name], version: locked_version, resolve_dependencies: false)
end
end
|
#each_dir ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/rbs/environment_loader.rb', line 131
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_signature ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/rbs/environment_loader.rb', line 154
def each_signature
files = Set[]
each_dir do |source, dir|
skip_hidden = !source.is_a?(Pathname)
FileFinder.each_file(dir, skip_hidden: skip_hidden) do |path|
next if files.include?(path)
files << path
buffer = Buffer.new(name: path.to_s, content: path.read(encoding: "UTF-8"))
_, dirs, decls = Parser.parse_signature(buffer)
yield source, path, buffer, decls, dirs
end
end
end
|
#has_library?(library:, version:) ⇒ Boolean
104
105
106
107
108
109
110
|
# File 'lib/rbs/environment_loader.rb', line 104
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/rbs/environment_loader.rb', line 112
def load(env:)
loaded = []
if @core_root && libs.none? { |lib| lib.name == 'stringio' }
add(library: 'stringio', version: nil)
end
each_signature do |source, path, buffer, decls, dirs|
decls.each do |decl|
loaded << [decl, path, source]
end
env.add_signature(buffer: buffer, directives: dirs, decls: decls)
end
loaded
end
|
#resolve_dependencies(library:, version:) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/rbs/environment_loader.rb', line 65
def resolve_dependencies(library:, version:)
[Collection::Sources::Rubygems.instance, Collection::Sources::Stdlib.instance].each do |source|
next unless source.has?(library, version)
unless version
version = source.versions(library).last or raise
end
source.dependencies_of(library, version)&.each do |dep|
add(library: dep['name'], version: nil)
end
return
end
end
|