Debian Codegraph @

lib/bundler/cli/install.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 class CLI::Install
5 attr_reader :options
6 def initialize(options)
7 @options = options
8 end
9
10 def run
11 Bundler.ui.level = "warn" if options[:quiet]
12
13 warn_if_root
14
15 if options[:local]
16 Bundler.self_manager.restart_with_locked_bundler_if_needed
17 else
18 Bundler.self_manager.install_locked_bundler_and_restart_with_it_if_needed
19 end
20
21 Bundler::SharedHelpers.set_env "RB_USER_INSTALL", "1" if Gem.freebsd_platform?
22
23 # Disable color in deployment mode
24 Bundler.ui.shell = Thor::Shell::Basic.new if options[:deployment]
25
26 check_for_options_conflicts
27
28 check_trust_policy
29
30 if options[:deployment] || options[:frozen] || Bundler.frozen_bundle?
31 unless Bundler.default_lockfile.exist?
32 flag = "--deployment flag" if options[:deployment]
33 flag ||= "--frozen flag" if options[:frozen]
34 flag ||= "deployment setting" if Bundler.settings[:deployment]
35 flag ||= "frozen setting" if Bundler.settings[:frozen]
36 raise ProductionError, "The #{flag} requires a lockfile. Please make " \
37 "sure you have checked your #{SharedHelpers.relative_lockfile_path} into version control " \
38 "before deploying."
39 end
40
41 options[:local] = true if Bundler.app_cache.exist?
42
43 Bundler.settings.set_command_option :deployment, true if options[:deployment]
44 Bundler.settings.set_command_option :frozen, true if options[:frozen]
45 end
46
47 # When install is called with --no-deployment, disable deployment mode
48 if options[:deployment] == false
49 Bundler.settings.set_command_option :frozen, nil
50 options[:system] = true
51 end
52
53 normalize_settings
54
55 Bundler::Fetcher.disable_endpoint = options["full-index"]
56
57 if options["binstubs"]
58 Bundler::SharedHelpers.major_deprecation 2,
59 "The --binstubs option will be removed in favor of `bundle binstubs --all`",
60 removed_message: "The --binstubs option have been removed in favor of `bundle binstubs --all`"
61 end
62
63 Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.feature_flag.plugins?
64
65 definition = Bundler.definition
66 definition.validate_runtime!
67
68 installer = Installer.install(Bundler.root, definition, options)
69
70 Bundler.settings.temporary(cache_all_platforms: options[:local] ? false : Bundler.settings[:cache_all_platforms]) do
71 Bundler.load.cache(nil, options[:local]) if Bundler.app_cache.exist? && !options["no-cache"] && !Bundler.frozen_bundle?
72 end
73
74 Bundler.ui.confirm "Bundle complete! #{dependencies_count_for(definition)}, #{gems_installed_for(definition)}."
75 Bundler::CLI::Common.output_without_groups_message(:install)
76
77 if Bundler.use_system_gems?
78 Bundler.ui.confirm "Use `bundle info [gemname]` to see where a bundled gem is installed."
79 else
80 relative_path = Bundler.configured_bundle_path.base_path_relative_to_pwd
81 Bundler.ui.confirm "Bundled gems are installed into `#{relative_path}`"
82 end
83
84 Bundler::CLI::Common.output_post_install_messages installer.post_install_messages
85
86 warn_ambiguous_gems
87
88 if CLI::Common.clean_after_install?
89 require_relative "clean"
90 Bundler::CLI::Clean.new(options).run
91 end
92
93 Bundler::CLI::Common.output_fund_metadata_summary
94 rescue Gem::InvalidSpecificationException
95 Bundler.ui.warn "You have one or more invalid gemspecs that need to be fixed."
96 raise
97 end
98
99 private
100
101 def warn_if_root
102 return if Bundler.settings[:silence_root_warning] || Gem.win_platform? || !Process.uid.zero?
103 Bundler.ui.warn "Don't run Bundler as root. Installing your bundle as root " \
104 "will break this application for all non-root users on this machine.", wrap: true
105 end
106
107 def dependencies_count_for(definition)
108 count = definition.dependencies.count
109 "#{count} Gemfile #{count == 1 ? "dependency" : "dependencies"}"
110 end
111
112 def gems_installed_for(definition)
113 count = definition.specs.count
114 "#{count} #{count == 1 ? "gem" : "gems"} now installed"
115 end
116
117 def check_for_group_conflicts_in_cli_options
118 conflicting_groups = Array(options[:without]) & Array(options[:with])
119 return if conflicting_groups.empty?
120 raise InvalidOption, "You can't list a group in both with and without." \
121 " The offending groups are: #{conflicting_groups.join(", ")}."
122 end
123
124 def check_for_options_conflicts
125 if (options[:path] || options[:deployment]) && options[:system]
126 error_message = String.new
127 error_message << "You have specified both --path as well as --system. Please choose only one option.\n" if options[:path]
128 error_message << "You have specified both --deployment as well as --system. Please choose only one option.\n" if options[:deployment]
129 raise InvalidOption.new(error_message)
130 end
131 end
132
133 def check_trust_policy
134 trust_policy = options["trust-policy"]
135 unless Bundler.rubygems.security_policies.keys.unshift(nil).include?(trust_policy)
136 raise InvalidOption, "RubyGems doesn't know about trust policy '#{trust_policy}'. " \
137 "The known policies are: #{Bundler.rubygems.security_policies.keys.join(", ")}."
138 end
139 Bundler.settings.set_command_option_if_given :"trust-policy", trust_policy
140 end
141
142 def normalize_groups
143 check_for_group_conflicts_in_cli_options
144
145 # need to nil them out first to get around validation for backwards compatibility
146 Bundler.settings.set_command_option :without, nil
147 Bundler.settings.set_command_option :with, nil
148 Bundler.settings.set_command_option :without, options[:without]
149 Bundler.settings.set_command_option :with, options[:with]
150 end
151
152 def normalize_settings
153 Bundler.settings.set_command_option :path, nil if options[:system]
154 Bundler.settings.set_command_option_if_given :path, options[:path]
155
156 if options["standalone"] && Bundler.settings[:path].nil? && !options["local"]
157 Bundler.settings.temporary(path_relative_to_cwd: false) do
158 Bundler.settings.set_command_option :path, "bundle"
159 end
160 end
161
162 bin_option = options["binstubs"]
163 bin_option = nil if bin_option&.empty?
164 Bundler.settings.set_command_option :bin, bin_option if options["binstubs"]
165
166 Bundler.settings.set_command_option_if_given :shebang, options["shebang"]
167
168 Bundler.settings.set_command_option_if_given :jobs, options["jobs"]
169
170 Bundler.settings.set_command_option_if_given :no_prune, options["no-prune"]
171
172 Bundler.settings.set_command_option_if_given :no_install, options["no-install"]
173
174 Bundler.settings.set_command_option_if_given :clean, options["clean"]
175
176 normalize_groups if options[:without] || options[:with]
177
178 options[:force] = options[:redownload]
179 end
180
181 def warn_ambiguous_gems
182 # TODO: remove this when we drop Bundler 1.x support
183 Installer.ambiguous_gems.to_a.each do |name, installed_from_uri, *also_found_in_uris|
184 Bundler.ui.warn "Warning: the gem '#{name}' was found in multiple sources."
185 Bundler.ui.warn "Installed from: #{installed_from_uri}"
186 Bundler.ui.warn "Also found in:"
187 also_found_in_uris.each {|uri| Bundler.ui.warn " * #{uri}" }
188 Bundler.ui.warn "You should add a source requirement to restrict this gem to your preferred source."
189 Bundler.ui.warn "For example:"
190 Bundler.ui.warn " gem '#{name}', :source => '#{installed_from_uri}'"
191 Bundler.ui.warn "Then uninstall the gem '#{name}' (or delete all bundled gems) and then install again."
192 end
193 end
194 end
195 end