| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
class CLI::Config < Thor |
| 5 |
class_option :parseable, type: :boolean, banner: "Use minimal formatting for more parseable output" |
| 6 |
|
| 7 |
def self.scope_options |
| 8 |
method_option :global, type: :boolean, banner: "Only change the global config" |
| 9 |
method_option :local, type: :boolean, banner: "Only change the local config" |
| 10 |
end |
| 11 |
private_class_method :scope_options |
| 12 |
|
| 13 |
desc "base NAME [VALUE]", "The Bundler 1 config interface", hide: true |
| 14 |
scope_options |
| 15 |
method_option :delete, type: :boolean, banner: "delete" |
| 16 |
def base(name = nil, *value) |
| 17 |
new_args = |
| 18 |
if ARGV.size == 1 |
| 19 |
["config", "list"] |
| 20 |
elsif ARGV.include?("--delete") |
| 21 |
ARGV.map {|arg| arg == "--delete" ? "unset" : arg } |
| 22 |
elsif ARGV.include?("--global") || ARGV.include?("--local") || ARGV.size == 3 |
| 23 |
["config", "set", *ARGV[1..-1]] |
| 24 |
else |
| 25 |
["config", "get", ARGV[1]] |
| 26 |
end |
| 27 |
|
| 28 |
message = "Using the `config` command without a subcommand [list, get, set, unset] is deprecated and will be removed in the future. Use `bundle #{new_args.join(" ")}` instead." |
| 29 |
removed_message = "Using the `config` command without a subcommand [list, get, set, unset] is has been removed. Use `bundle #{new_args.join(" ")}` instead." |
| 30 |
SharedHelpers.major_deprecation 3, message, removed_message: removed_message |
| 31 |
|
| 32 |
Base.new(options, name, value, self).run |
| 33 |
end |
| 34 |
|
| 35 |
desc "list", "List out all configured settings" |
| 36 |
def list |
| 37 |
Base.new(options, nil, nil, self).run |
| 38 |
end |
| 39 |
|
| 40 |
desc "get NAME", "Returns the value for the given key" |
| 41 |
def get(name) |
| 42 |
Base.new(options, name, nil, self).run |
| 43 |
end |
| 44 |
|
| 45 |
desc "set NAME VALUE", "Sets the given value for the given key" |
| 46 |
scope_options |
| 47 |
def set(name, value, *value_) |
| 48 |
Base.new(options, name, value_.unshift(value), self).run |
| 49 |
end |
| 50 |
|
| 51 |
desc "unset NAME", "Unsets the value for the given key" |
| 52 |
scope_options |
| 53 |
def unset(name) |
| 54 |
options[:delete] = true |
| 55 |
Base.new(options, name, nil, self).run |
| 56 |
end |
| 57 |
|
| 58 |
default_task :base |
| 59 |
|
| 60 |
class Base |
| 61 |
attr_reader :name, :value, :options, :scope, :thor |
| 62 |
|
| 63 |
def initialize(options, name, value, thor) |
| 64 |
@options = options |
| 65 |
@name = name |
| 66 |
value = Array(value) |
| 67 |
@value = value.empty? ? nil : value.join(" ") |
| 68 |
@thor = thor |
| 69 |
validate_scope! |
| 70 |
end |
| 71 |
|
| 72 |
def run |
| 73 |
unless name |
| 74 |
warn_unused_scope "Ignoring --#{scope}" |
| 75 |
confirm_all |
| 76 |
return |
| 77 |
end |
| 78 |
|
| 79 |
if options[:delete] |
| 80 |
if !explicit_scope? || scope != "global" |
| 81 |
Bundler.settings.set_local(name, nil) |
| 82 |
end |
| 83 |
if !explicit_scope? || scope != "local" |
| 84 |
Bundler.settings.set_global(name, nil) |
| 85 |
end |
| 86 |
return |
| 87 |
end |
| 88 |
|
| 89 |
if value.nil? |
| 90 |
warn_unused_scope "Ignoring --#{scope} since no value to set was given" |
| 91 |
|
| 92 |
if options[:parseable] |
| 93 |
if value = Bundler.settings[name] |
| 94 |
Bundler.ui.info("#{name}=#{value}") |
| 95 |
end |
| 96 |
return |
| 97 |
end |
| 98 |
|
| 99 |
confirm(name) |
| 100 |
return |
| 101 |
end |
| 102 |
|
| 103 |
Bundler.ui.info(message) if message |
| 104 |
Bundler.settings.send("set_#{scope}", name, new_value) |
| 105 |
end |
| 106 |
|
| 107 |
def confirm_all |
| 108 |
if @options[:parseable] |
| 109 |
thor.with_padding do |
| 110 |
Bundler.settings.all.each do |setting| |
| 111 |
val = Bundler.settings[setting] |
| 112 |
Bundler.ui.info "#{setting}=#{val}" |
| 113 |
end |
| 114 |
end |
| 115 |
else |
| 116 |
Bundler.ui.confirm "Settings are listed in order of priority. The top value will be used.\n" |
| 117 |
Bundler.settings.all.each do |setting| |
| 118 |
Bundler.ui.confirm setting |
| 119 |
show_pretty_values_for(setting) |
| 120 |
Bundler.ui.confirm "" |
| 121 |
end |
| 122 |
end |
| 123 |
end |
| 124 |
|
| 125 |
def confirm(name) |
| 126 |
Bundler.ui.confirm "Settings for `#{name}` in order of priority. The top value will be used" |
| 127 |
show_pretty_values_for(name) |
| 128 |
end |
| 129 |
|
| 130 |
def new_value |
| 131 |
pathname = Pathname.new(value) |
| 132 |
if name.start_with?("local.") && pathname.directory? |
| 133 |
pathname.expand_path.to_s |
| 134 |
else |
| 135 |
value |
| 136 |
end |
| 137 |
end |
| 138 |
|
| 139 |
def message |
| 140 |
locations = Bundler.settings.locations(name) |
| 141 |
if @options[:parseable] |
| 142 |
"#{name}=#{new_value}" if new_value |
| 143 |
elsif scope == "global" |
| 144 |
if !locations[:local].nil? |
| 145 |
"Your application has set #{name} to #{locations[:local].inspect}. " \ |
| 146 |
"This will override the global value you are currently setting" |
| 147 |
elsif locations[:env] |
| 148 |
"You have a bundler environment variable for #{name} set to " \ |
| 149 |
"#{locations[:env].inspect}. This will take precedence over the global value you are setting" |
| 150 |
elsif !locations[:global].nil? && locations[:global] != value |
| 151 |
"You are replacing the current global value of #{name}, which is currently " \ |
| 152 |
"#{locations[:global].inspect}" |
| 153 |
end |
| 154 |
elsif scope == "local" && !locations[:local].nil? && locations[:local] != value |
| 155 |
"You are replacing the current local value of #{name}, which is currently " \ |
| 156 |
"#{locations[:local].inspect}" |
| 157 |
end |
| 158 |
end |
| 159 |
|
| 160 |
def show_pretty_values_for(setting) |
| 161 |
thor.with_padding do |
| 162 |
Bundler.settings.pretty_values_for(setting).each do |line| |
| 163 |
Bundler.ui.info line |
| 164 |
end |
| 165 |
end |
| 166 |
end |
| 167 |
|
| 168 |
def explicit_scope? |
| 169 |
@explicit_scope |
| 170 |
end |
| 171 |
|
| 172 |
def warn_unused_scope(msg) |
| 173 |
return unless explicit_scope? |
| 174 |
return if options[:parseable] |
| 175 |
|
| 176 |
Bundler.ui.warn(msg) |
| 177 |
end |
| 178 |
|
| 179 |
def validate_scope! |
| 180 |
@explicit_scope = true |
| 181 |
scopes = %w[global local].select {|s| options[s] } |
| 182 |
case scopes.size |
| 183 |
when 0 |
| 184 |
@scope = inside_app? ? "local" : "global" |
| 185 |
@explicit_scope = false |
| 186 |
when 1 |
| 187 |
@scope = scopes.first |
| 188 |
else |
| 189 |
raise InvalidOption, |
| 190 |
"The options #{scopes.join " and "} were specified. Please only use one of the switches at a time." |
| 191 |
end |
| 192 |
end |
| 193 |
|
| 194 |
private |
| 195 |
|
| 196 |
def inside_app? |
| 197 |
Bundler.root |
| 198 |
true |
| 199 |
rescue GemfileNotFound |
| 200 |
false |
| 201 |
end |
| 202 |
end |
| 203 |
end |
| 204 |
end |