Sha256: c2a4e2778c11f37dc606dabe72f89308997516bc5906e4638a85463885fab558

Contents?: true

Size: 1.92 KB

Versions: 28

Compression:

Stored size: 1.92 KB

Contents

# typed: true
# frozen_string_literal: true

module Tapioca
  module BundlerExt
    # 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])
      @enabled = T.let(false, T::Boolean)

      class << self
        extend T::Sig

        sig { params(name: T.untyped).returns(T::Boolean) }
        def excluded?(name)
          @exclude.include?(name)
        end

        def enabled?
          @enabled
        end

        sig do
          type_parameters(:Result).params(
            exclude: T::Array[String],
            blk: T.proc.returns(T.type_parameter(:Result)),
          ).returns(T.type_parameter(:Result))
        end
        def override_require_false(exclude:, &blk)
          @enabled = true
          @exclude = exclude
          blk.call
        ensure
          @enabled = false
        end
      end

      sig { returns(T.untyped).checked(:never) }
      def autorequire
        value = super

        # If autorequire is not enabled, we don't want to force require gems
        return value unless AutoRequireHook.enabled?

        # 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 if value == []

        value
      end

      ::Bundler::Dependency.prepend(self)
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
tapioca-0.16.8 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.7 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.6 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.5 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.4 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.3 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.2 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.1 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.16.0 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.15.1 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.15.0 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.14.4 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.14.3 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.13.3 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.13.2 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.13.1 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.13.0 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.12.0 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.11.17 lib/tapioca/bundler_ext/auto_require_hook.rb
tapioca-0.11.16 lib/tapioca/bundler_ext/auto_require_hook.rb