Debian Codegraph @

lib/bundler/cli/common.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 module CLI::Common
5 def self.output_post_install_messages(messages)
6 return if Bundler.settings["ignore_messages"]
7 messages.to_a.each do |name, msg|
8 print_post_install_message(name, msg) unless Bundler.settings["ignore_messages.#{name}"]
9 end
10 end
11
12 def self.print_post_install_message(name, msg)
13 Bundler.ui.confirm "Post-install message from #{name}:"
14 Bundler.ui.info msg
15 end
16
17 def self.output_fund_metadata_summary
18 return if Bundler.settings["ignore_funding_requests"]
19 definition = Bundler.definition
20 current_dependencies = definition.requested_dependencies
21 current_specs = definition.specs
22
23 count = current_dependencies.count {|dep| current_specs[dep.name].first.metadata.key?("funding_uri") }
24
25 return if count.zero?
26
27 intro = count > 1 ? "#{count} installed gems you directly depend on are" : "#{count} installed gem you directly depend on is"
28 message = "#{intro} looking for funding.\n Run `bundle fund` for details"
29 Bundler.ui.info message
30 end
31
32 def self.output_without_groups_message(command)
33 return if Bundler.settings[:without].empty?
34 Bundler.ui.confirm without_groups_message(command)
35 end
36
37 def self.without_groups_message(command)
38 command_in_past_tense = command == :install ? "installed" : "updated"
39 groups = Bundler.settings[:without]
40 "Gems in the #{verbalize_groups(groups)} were not #{command_in_past_tense}."
41 end
42
43 def self.verbalize_groups(groups)
44 groups.map! {|g| "'#{g}'" }
45 group_list = [groups[0...-1].join(", "), groups[-1..-1]].
46 reject {|s| s.to_s.empty? }.join(" and ")
47 group_str = groups.size == 1 ? "group" : "groups"
48 "#{group_str} #{group_list}"
49 end
50
51 def self.select_spec(name, regex_match = nil)
52 specs = []
53 regexp = Regexp.new(name) if regex_match
54
55 Bundler.definition.specs.each do |spec|
56 return spec if spec.name == name
57 specs << spec if regexp && spec.name.match?(regexp)
58 end
59
60 default_spec = default_gem_spec(name)
61 specs << default_spec if default_spec
62
63 case specs.count
64 when 0
65 dep_in_other_group = Bundler.definition.current_dependencies.find {|dep|dep.name == name }
66
67 if dep_in_other_group
68 raise GemNotFound, "Could not find gem '#{name}', because it's in the #{verbalize_groups(dep_in_other_group.groups)}, configured to be ignored."
69 else
70 raise GemNotFound, gem_not_found_message(name, Bundler.definition.dependencies)
71 end
72 when 1
73 specs.first
74 else
75 ask_for_spec_from(specs)
76 end
77 rescue RegexpError
78 raise GemNotFound, gem_not_found_message(name, Bundler.definition.dependencies)
79 end
80
81 def self.default_gem_spec(name)
82 gem_spec = Gem::Specification.find_all_by_name(name).last
83 gem_spec if gem_spec&.default_gem?
84 end
85
86 def self.ask_for_spec_from(specs)
87 specs.each_with_index do |spec, index|
88 Bundler.ui.info "#{index.succ} : #{spec.name}", true
89 end
90 Bundler.ui.info "0 : - exit -", true
91
92 num = Bundler.ui.ask("> ").to_i
93 num > 0 ? specs[num - 1] : nil
94 end
95
96 def self.gem_not_found_message(missing_gem_name, alternatives)
97 require_relative "../similarity_detector"
98 message = "Could not find gem '#{missing_gem_name}'."
99 alternate_names = alternatives.map {|a| a.respond_to?(:name) ? a.name : a }
100 suggestions = SimilarityDetector.new(alternate_names).similar_word_list(missing_gem_name)
101 message += "\nDid you mean #{suggestions}?" if suggestions
102 message
103 end
104
105 def self.ensure_all_gems_in_lockfile!(names, locked_gems = Bundler.locked_gems)
106 return unless locked_gems
107
108 locked_names = locked_gems.specs.map(&:name).uniq
109 names.-(locked_names).each do |g|
110 raise GemNotFound, gem_not_found_message(g, locked_names)
111 end
112 end
113
114 def self.configure_gem_version_promoter(definition, options)
115 patch_level = patch_level_options(options)
116 patch_level << :patch if patch_level.empty? && Bundler.settings[:prefer_patch]
117 raise InvalidOption, "Provide only one of the following options: #{patch_level.join(", ")}" unless patch_level.length <= 1
118
119 definition.gem_version_promoter.tap do |gvp|
120 gvp.level = patch_level.first || :major
121 gvp.strict = options[:strict] || options["filter-strict"]
122 gvp.pre = options[:pre]
123 end
124 end
125
126 def self.patch_level_options(options)
127 [:major, :minor, :patch].select {|v| options.keys.include?(v.to_s) }
128 end
129
130 def self.clean_after_install?
131 clean = Bundler.settings[:clean]
132 return clean unless clean.nil?
133 clean ||= Bundler.feature_flag.auto_clean_without_path? && Bundler.settings[:path].nil?
134 clean &&= !Bundler.use_system_gems?
135 clean
136 end
137 end
138 end