Sha256: 00a1880e7ed2a3d4dd481c3a1eb826ceb1a1c6733a62994d667b1439e3ebb4e8

Contents?: true

Size: 1.97 KB

Versions: 24

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Utils
    module RequireGems
      class << self
        #
        # Require a gem or file if it's present, otherwise silently fail.
        #
        # names - a string gem name or array of gem names
        #
        def require_if_present(names)
          Array(names).each do |name|
            require name
          rescue LoadError
            Bridgetown.logger.debug "Couldn't load #{name}. Skipping."
            yield(name, version_constraint(name)) if block_given?
            false
          end
        end

        #
        # The version constraint required to activate a given gem.
        #
        # Returns a String version constraint in a parseable form for
        # RubyGems.
        def version_constraint
          "> 0"
        end

        #
        # Require a gem or gems. If it's not present, show a very nice error
        # message that explains everything and is much more helpful than the
        # normal LoadError.
        #
        # names - a string gem name or array of gem names
        #
        def require_with_graceful_fail(names)
          Array(names).each do |name|
            Bridgetown.logger.debug "Requiring:", name.to_s
            require name
          rescue LoadError => e
            Bridgetown.logger.error "Dependency Error:", <<~MSG
              Yikes! It looks like you don't have #{name} or one of its dependencies installed.
              In order to use Bridgetown as currently configured, you'll need to install this gem.

              If you've run Bridgetown with `bundle exec`, ensure that you have included the #{name}
              gem in your Gemfile as well.

              The full error message from Ruby is: '#{e.message}'

              If you run into trouble, you can find helpful resources at https://www.bridgetownrb.com/docs/community/
            MSG
            raise Bridgetown::Errors::MissingDependencyException, name
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
bridgetown-core-1.0.0 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.beta3 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.beta2 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.beta1 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha11 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha10 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha9 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha8 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha7 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha6 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha5 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-0.21.5 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha4 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha3 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha2 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-1.0.0.alpha1 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-0.21.4 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-0.21.3 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-0.21.2 lib/bridgetown-core/utils/require_gems.rb
bridgetown-core-0.21.1 lib/bridgetown-core/utils/require_gems.rb