-
Notifications
You must be signed in to change notification settings - Fork 121
/
bundler.rb
78 lines (66 loc) · 2.09 KB
/
bundler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true
require "bundler"
module Licensed
module Source
class Bundler
BUNDLER_ENV_KEYS = %w(BUNDLE_GEMFILE).freeze
def initialize(config)
@config = config
end
def enabled?
@config.enabled?(type) && File.exist?(lockfile_path)
end
def type
"rubygem"
end
def dependencies
@dependencies ||= with_local_configuration do
definition.specs_for(groups).map do |spec|
Dependency.new(spec.gem_dir, {
"type" => type,
"name" => spec.name,
"version" => spec.version.to_s,
"summary" => spec.summary,
"homepage" => spec.homepage
})
end
end
end
# Build the bundler definition
def definition
@definition ||= ::Bundler::Definition.build(gemfile_path, lockfile_path, nil)
end
# Returns the bundle definition groups, excluding test and development
def groups
definition.groups - [:test, :development]
end
# Returns the expected path to the Bundler Gemfile
def gemfile_path
@config.pwd.join ::Bundler.default_gemfile.basename.to_s
end
# Returns the expected path to the Bundler Gemfile.lock
def lockfile_path
@config.pwd.join ::Bundler.default_lockfile.basename.to_s
end
private
# helper to clear all bundler environment around a yielded block
def with_local_configuration
# with a clean, original environment
::Bundler.with_original_env do
# bundler restores all ENV at the end of the `with_original_env`
# block. we shouldn't need to restore these values manually
BUNDLER_ENV_KEYS.each { |k| ENV.delete(k) }
# reset all bundler configuration
::Bundler.reset!
# and re-configure with settings for current directory
::Bundler.configure
yield
end
ensure
# restore bundler configuration
::Bundler.reset!
::Bundler.configure
end
end
end
end