-
Notifications
You must be signed in to change notification settings - Fork 111
/
gemfile.rb
308 lines (250 loc) · 9.34 KB
/
gemfile.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# typed: strict
# frozen_string_literal: true
require "bundler"
require "logger"
require "yard-sorbet"
module Tapioca
class Gemfile
extend(T::Sig)
Spec = T.type_alias do
T.any(
::Bundler::StubSpecification,
::Gem::Specification
)
end
# This is a module that gets prepended to `Bundler::Dependency` and
# makes sure even gems marked as `require: false` are required during
# `Bundler.require`.
module AutoRequireHook
extend T::Sig
extend T::Helpers
requires_ancestor { ::Bundler::Dependency }
@exclude = T.let([], T::Array[String])
class << self
extend T::Sig
sig { params(exclude: T::Array[String]).returns(T::Array[String]) }
attr_writer :exclude
sig { params(name: T.untyped).returns(T::Boolean) }
def excluded?(name)
@exclude.include?(name)
end
end
sig { returns(T.untyped).checked(:never) }
def autorequire
value = super
# If the gem is excluded, we don't want to force require it, in case
# it has side-effects users don't want. For example, `fakefs` gem, if
# loaded, takes over filesystem operations.
return value if AutoRequireHook.excluded?(name)
# If a gem is marked as `require: false`, then its `autorequire`
# value will be `[]`. But, we want those gems to be loaded for our
# purposes as well, so we return `nil` in those cases, instead, which
# means `require: true`.
return nil if value == []
value
end
::Bundler::Dependency.prepend(self)
end
sig { returns(Bundler::Definition) }
attr_reader(:definition)
sig { returns(T::Array[GemSpec]) }
attr_reader(:dependencies)
sig { returns(T::Array[String]) }
attr_reader(:missing_specs)
sig { params(exclude: T::Array[String]).void }
def initialize(exclude)
AutoRequireHook.exclude = exclude
@gemfile = T.let(File.new(Bundler.default_gemfile), File)
@lockfile = T.let(File.new(Bundler.default_lockfile), File)
@definition = T.let(Bundler::Dsl.evaluate(gemfile, lockfile, {}), Bundler::Definition)
dependencies, missing_specs = load_dependencies
@dependencies = T.let(dependencies, T::Array[GemSpec])
@missing_specs = T.let(missing_specs, T::Array[String])
end
sig { params(gem_name: String).returns(T.nilable(GemSpec)) }
def gem(gem_name)
dependencies.detect { |dep| dep.name == gem_name }
end
sig { void }
def require_bundle
T.unsafe(runtime).require(*groups)
end
private
sig { returns(File) }
attr_reader(:gemfile, :lockfile)
sig { returns([T::Array[GemSpec], T::Array[String]]) }
def load_dependencies
materialized_dependencies, missing_specs = materialize_deps
dependencies = materialized_dependencies
.map { |spec| GemSpec.new(spec) }
.reject { |gem| gem.ignore?(dir) }
.uniq(&:rbi_file_name)
.sort_by(&:rbi_file_name)
[dependencies, missing_specs]
end
sig { returns([T::Enumerable[Spec], T::Array[String]]) }
def materialize_deps
deps = definition.locked_gems.dependencies.values
missing_specs = T::Array[String].new
materialized_dependencies = if definition.resolve.method(:materialize).arity == 1 # Support bundler >= v2.2.25
md = definition.resolve.materialize(deps)
missing_spec_names = md.missing_specs.map(&:name)
missing_specs = T.cast(md.missing_specs.map { |spec| "#{spec.name} (#{spec.version})" }, T::Array[String])
md.to_a.reject { |spec| missing_spec_names.include?(spec.name) }
else
definition.resolve.materialize(deps, missing_specs)
end
[materialized_dependencies, missing_specs]
end
sig { returns(Bundler::Runtime) }
def runtime
Bundler::Runtime.new(File.dirname(gemfile.path), definition)
end
sig { returns(T::Array[Symbol]) }
def groups
definition.groups
end
sig { returns(String) }
def dir
File.expand_path(gemfile.path + "/..")
end
class GemSpec
extend(T::Sig)
IGNORED_GEMS = T.let(["sorbet", "sorbet-static", "sorbet-runtime"].freeze, T::Array[String])
sig { returns(String) }
attr_reader :full_gem_path, :version
sig { returns(T::Array[Pathname]) }
attr_reader :files
sig { params(spec: Spec).void }
def initialize(spec)
@spec = T.let(spec, Tapioca::Gemfile::Spec)
real_gem_path = to_realpath(@spec.full_gem_path)
@full_gem_path = T.let(real_gem_path, String)
@version = T.let(version_string, String)
@exported_rbi_files = T.let(nil, T.nilable(T::Array[String]))
@files = T.let(collect_files, T::Array[Pathname])
end
sig { params(gemfile_dir: String).returns(T::Boolean) }
def ignore?(gemfile_dir)
gem_ignored? || gem_in_app_dir?(gemfile_dir)
end
sig { returns(String) }
def name
@spec.name
end
sig { returns(String) }
def rbi_file_name
"#{name}@#{version}.rbi"
end
sig { params(path: String).returns(T::Boolean) }
def contains_path?(path)
if default_gem?
files.any? { |file| file.to_s == to_realpath(path) }
else
to_realpath(path).start_with?(full_gem_path) || has_parent_gemspec?(path)
end
end
sig { void }
def parse_yard_docs
files.each { |path| YARD.parse(path.to_s, [], Logger::Severity::FATAL) }
end
sig { returns(T::Array[String]) }
def exported_rbi_files
@exported_rbi_files ||= Dir.glob("#{full_gem_path}/rbi/**/*.rbi").sort
end
sig { returns(T::Boolean) }
def export_rbi_files?
exported_rbi_files.any?
end
sig { returns(RBI::MergeTree) }
def exported_rbi_tree
rewriter = RBI::Rewriters::Merge.new(keep: RBI::Rewriters::Merge::Keep::NONE)
exported_rbi_files.each do |file|
rbi = RBI::Parser.parse_file(file)
rewriter.merge(rbi)
end
rewriter.tree
end
private
sig { returns(T::Array[Pathname]) }
def collect_files
if default_gem?
# `Bundler::RemoteSpecification` delegates missing methods to
# `Gem::Specification`, so `files` actually always exists on spec.
T.unsafe(@spec).files.map do |file|
resolve_to_ruby_lib_dir(file)
end
else
@spec.full_require_paths.flat_map do |path|
Pathname.glob((Pathname.new(path) / "**/*.rb").to_s)
end
end
end
sig { returns(T.nilable(T::Boolean)) }
def default_gem?
@spec.respond_to?(:default_gem?) && @spec.default_gem?
end
sig { returns(Regexp) }
def require_paths_prefix_matcher
@require_paths_prefix_matcher = T.let(@require_paths_prefix_matcher, T.nilable(Regexp))
@require_paths_prefix_matcher ||= begin
require_paths = T.unsafe(@spec).require_paths
prefix_matchers = require_paths.map { |rp| Regexp.new("^#{rp}/") }
Regexp.union(prefix_matchers)
end
end
sig { params(file: String).returns(Pathname) }
def resolve_to_ruby_lib_dir(file)
# We want to match require prefixes but fallback to an empty match
# if none of the require prefixes actually match. This is so that
# we can always replace the match with the Ruby lib directory and
# we would have properly resolved the file under the Ruby lib dir.
prefix_matcher = Regexp.union(require_paths_prefix_matcher, //)
ruby_lib_dir = RbConfig::CONFIG["rubylibdir"]
file = file.sub(prefix_matcher, "#{ruby_lib_dir}/")
Pathname.new(file).expand_path
end
sig { returns(String) }
def version_string
version = @spec.version.to_s
version += "-#{@spec.source.revision}" if Bundler::Source::Git === @spec.source
version
end
sig { params(path: String).returns(T::Boolean) }
def has_parent_gemspec?(path)
# For some Git installed gems the location of the loaded file can
# be different from the gem path as indicated by the spec file
#
# To compensate for these cases, we walk up the directory hierarchy
# from the given file and try to match a <gem-name.gemspec> file in
# one of those folders to see if the path really belongs in the given gem
# or not.
return false unless Bundler::Source::Git === @spec.source
parent = Pathname.new(path)
until parent.root?
parent = parent.parent.expand_path
return true if parent.join("#{name}.gemspec").file?
end
false
end
sig { params(path: T.any(String, Pathname)).returns(String) }
def to_realpath(path)
path_string = path.to_s
path_string = File.realpath(path_string) if File.exist?(path_string)
path_string
end
sig { returns(T::Boolean) }
def gem_ignored?
IGNORED_GEMS.include?(name)
end
sig { params(gemfile_dir: String).returns(T::Boolean) }
def gem_in_app_dir?(gemfile_dir)
!gem_in_bundle_path? && full_gem_path.start_with?(gemfile_dir)
end
sig { returns(T::Boolean) }
def gem_in_bundle_path?
full_gem_path.start_with?(Bundler.bundle_path.to_s, Bundler.app_cache.to_s)
end
end
end
end