Class: Licensed::Sources::Bundler

Inherits:
Source
  • Object
show all
Defined in:
lib/licensed/sources/bundler.rb

Defined Under Namespace

Classes: Dependency

Constant Summary collapse

DEFAULT_WITHOUT_GROUPS =
%i{development test}

Instance Attribute Summary

Attributes inherited from Source

#config

Instance Method Summary collapse

Methods inherited from Source

#dependencies, full_type, #ignored?, inherited, #initialize, register_source, require_matched_dependency_version, #source_config, type, type_and_version

Constructor Details

This class inherits a constructor from Licensed::Sources::Source

Instance Method Details

#definitionObject



74
75
76
77
78
79
80
81
# 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?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# 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_dependenciesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_groupsObject

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



86
87
88
89
90
91
92
# 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_environmentObject

helper to clear all bundler environment around a yielded block



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
# 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