| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
class Settings |
| 5 |
class Validator |
| 6 |
class Rule |
| 7 |
attr_reader :description |
| 8 |
|
| 9 |
def initialize(keys, description, &validate) |
| 10 |
@keys = keys |
| 11 |
@description = description |
| 12 |
@validate = validate |
| 13 |
end |
| 14 |
|
| 15 |
def validate!(key, value, settings) |
| 16 |
instance_exec(key, value, settings, &@validate) |
| 17 |
end |
| 18 |
|
| 19 |
def fail!(key, value, *reasons) |
| 20 |
reasons.unshift @description |
| 21 |
raise InvalidOption, "Setting `#{key}` to #{value.inspect} failed:\n#{reasons.map {|r| " - #{r}" }.join("\n")}" |
| 22 |
end |
| 23 |
|
| 24 |
def set(settings, key, value, *reasons) |
| 25 |
hash_key = k(key) |
| 26 |
return if settings[hash_key] == value |
| 27 |
reasons.unshift @description |
| 28 |
Bundler.ui.info "Setting `#{key}` to #{value.inspect}, since #{reasons.join(", ")}" |
| 29 |
if value.nil? |
| 30 |
settings.delete(hash_key) |
| 31 |
else |
| 32 |
settings[hash_key] = value |
| 33 |
end |
| 34 |
end |
| 35 |
|
| 36 |
def k(key) |
| 37 |
Bundler.settings.key_for(key) |
| 38 |
end |
| 39 |
end |
| 40 |
|
| 41 |
def self.rules |
| 42 |
@rules ||= Hash.new {|h, k| h[k] = [] } |
| 43 |
end |
| 44 |
private_class_method :rules |
| 45 |
|
| 46 |
def self.rule(keys, description, &blk) |
| 47 |
rule = Rule.new(keys, description, &blk) |
| 48 |
keys.each {|k| rules[k] << rule } |
| 49 |
end |
| 50 |
private_class_method :rule |
| 51 |
|
| 52 |
def self.validate!(key, value, settings) |
| 53 |
rules_to_validate = rules[key] |
| 54 |
rules_to_validate.each {|rule| rule.validate!(key, value, settings) } |
| 55 |
end |
| 56 |
|
| 57 |
rule %w[path path.system], "path and path.system are mutually exclusive" do |key, value, settings| |
| 58 |
if key == "path" && value |
| 59 |
set(settings, "path.system", nil) |
| 60 |
elsif key == "path.system" && value |
| 61 |
set(settings, :path, nil) |
| 62 |
end |
| 63 |
end |
| 64 |
|
| 65 |
rule %w[with without], "a group cannot be in both `with` & `without` simultaneously" do |key, value, settings| |
| 66 |
with = settings.fetch(k(:with), "").split(":").map(&:to_sym) |
| 67 |
without = settings.fetch(k(:without), "").split(":").map(&:to_sym) |
| 68 |
|
| 69 |
other_key = key == "with" ? :without : :with |
| 70 |
other_setting = key == "with" ? without : with |
| 71 |
|
| 72 |
conflicting = with & without |
| 73 |
if conflicting.any? |
| 74 |
fail!(key, value, "`#{other_key}` is current set to #{other_setting.inspect}", "the `#{conflicting.join("`, `")}` groups conflict") |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
rule %w[path], "relative paths are expanded relative to the current working directory" do |key, value, settings| |
| 79 |
next if value.nil? |
| 80 |
|
| 81 |
path = Pathname.new(value) |
| 82 |
next if !path.relative? || !Bundler.feature_flag.path_relative_to_cwd? |
| 83 |
|
| 84 |
path = path.expand_path |
| 85 |
|
| 86 |
root = begin |
| 87 |
Bundler.root |
| 88 |
rescue GemfileNotFound |
| 89 |
Pathname.pwd.expand_path |
| 90 |
end |
| 91 |
|
| 92 |
path = begin |
| 93 |
path.relative_path_from(root) |
| 94 |
rescue ArgumentError |
| 95 |
path |
| 96 |
end |
| 97 |
|
| 98 |
set(settings, key, path.to_s) |
| 99 |
end |
| 100 |
end |
| 101 |
end |
| 102 |
end |