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}
RUBY_PACKER_ERROR =
"The bundler source cannot be used from the executable built with ruby-packer.  Please install licensed using `gem install` or using bundler."

Instance Attribute Summary

Attributes inherited from Source

#config

Instance Method Summary collapse

Methods inherited from Source

#dependencies, full_type, #ignored?, inherited, #initialize, type, type_and_version

Constructor Details

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

Instance Method Details

#definitionObject



81
82
83
84
85
86
87
88
# File 'lib/licensed/sources/bundler.rb', line 81

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)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/licensed/sources/bundler.rb', line 44

def enabled?
  # running a ruby-packer-built licensed exe when ruby isn't available
  # could lead to errors if the host ruby doesn't exist
  return false if ruby_packer? && !Licensed::Shell.tool_available?("ruby")

  # 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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/licensed/sources/bundler.rb', line 57

def enumerate_dependencies
  raise Licensed::Sources::Source::Error.new(RUBY_PACKER_ERROR) if ruby_packer?

  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



93
94
95
96
97
98
99
# File 'lib/licensed/sources/bundler.rb', line 93

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

#ruby_packer?Boolean

Returns whether the current licensed execution is running ruby-packer

Returns:

  • (Boolean)


131
132
133
# File 'lib/licensed/sources/bundler.rb', line 131

def ruby_packer?
  @ruby_packer ||= RbConfig::TOPDIR =~ /__enclose_io_memfs__/
end

#with_application_environmentObject

helper to clear all bundler environment around a yielded block



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/licensed/sources/bundler.rb', line 102

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