Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need less entries in require.rb #818

Merged
merged 5 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tapioca/commands/gem.rb
Expand Up @@ -124,7 +124,7 @@ def loader

sig { returns(Gemfile) }
def bundle
@bundle ||= Gemfile.new
@bundle ||= Gemfile.new(@exclude)
end

sig { void }
Expand Down
49 changes: 47 additions & 2 deletions lib/tapioca/gemfile.rb
Expand Up @@ -16,6 +16,50 @@ class Gemfile
)
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)

Expand All @@ -25,8 +69,9 @@ class Gemfile
sig { returns(T::Array[String]) }
attr_reader(:missing_specs)

sig { void }
def initialize
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)
Expand Down
243 changes: 0 additions & 243 deletions sorbet/rbi/gems/activemodel-serializers-xml@1.0.2.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions sorbet/rbi/gems/activemodel@6.1.4.6.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.