class Licensed::Sources::Bundler

Constants

DEFAULT_WITHOUT_GROUPS

Public Instance Methods

definition() click to toggle source
# File lib/licensed/sources/bundler.rb, line 74
def definition
  @definition ||= begin
    definition = ::Bundler::Definition.build(::Bundler.default_gemfile, ::Bundler.default_lockfile, nil)
    definition.extend Licensed::Bundler::DefinitionExtensions
    definition.force_exclude_groups = exclude_groups
    definition
  end
end
enabled?() click to toggle source
# File lib/licensed/sources/bundler.rb, line 43
def enabled?
  # if Bundler isn't loaded, this enumerator won't work!
  return false unless defined?(::Bundler)

  with_application_environment { ::Bundler.default_lockfile&.exist? }
rescue ::Bundler::GemfileNotFound
  false
end
enumerate_dependencies() click to toggle source
# File lib/licensed/sources/bundler.rb, line 52
def enumerate_dependencies
  with_application_environment do
    definition.specs.map do |spec|
      next if spec.name == config["name"]

      error = spec.error if spec.respond_to?(:error)
      Dependency.new(
        name: spec.name,
        version: spec.version.to_s,
        path: spec.full_gem_path,
        loaded_from: spec.loaded_from,
        errors: Array(error),
        metadata: {
          "type"     => Bundler.type,
          "summary"  => spec.summary,
          "homepage" => spec.homepage
        }
      )
    end
  end
end
exclude_groups() click to toggle source

Returns any groups to exclude specified from both licensed configuration and bundler configuration. Defaults to [:development, :test] + ::Bundler.settings[:without]

# File lib/licensed/sources/bundler.rb, line 86
def exclude_groups
  @exclude_groups ||= begin
    exclude = Array(config.dig("bundler", "without"))
    exclude = DEFAULT_WITHOUT_GROUPS if exclude.empty?
    exclude.uniq.map(&:to_sym)
  end
end
with_application_environment() { || ... } click to toggle source

helper to clear all bundler environment around a yielded block

# File lib/licensed/sources/bundler.rb, line 95
def with_application_environment
  backup = nil

  ::Bundler.ui.silence do
    if ::Bundler.root != config.source_path
      backup = ENV.to_hash
      ENV.replace(::Bundler.original_env)

      # reset bundler to load from the current app's source path
      ::Bundler.reset!
    end

    # ensure the bundler environment is loaded before enumeration
    ::Bundler.load

    yield
  end
ensure
  if backup
    # restore bundler configuration
    ENV.replace(backup)
    ::Bundler.reset!
  end

  # reload the bundler environment after enumeration
  ::Bundler.load
end