link: refactor, reorder and fix bugs.

The change in #4441 broke the handling of the `elsif`s due to the
change in logic. As every block here has a `next` there's no need to do
an `elsif` in here at all. Additionally, reorder the conditions in here
so you get an appropriate message depending on what you're trying to do.
Finally, tweak some of the messaging to remove things that are ignored
and tell people correct commands to run to link things.
This commit is contained in:
Mike McQuaid 2018-07-10 09:32:42 +01:00
parent aa49f97343
commit 7cc970fea2
2 changed files with 51 additions and 39 deletions

View File

@ -27,7 +27,43 @@ module Homebrew
ARGV.kegs.each do |keg| ARGV.kegs.each do |keg|
keg_only = Formulary.keg_only?(keg.rack) keg_only = Formulary.keg_only?(keg.rack)
if HOMEBREW_PREFIX.to_s == "/usr/local" && keg_only
if keg.linked?
opoo "Already linked: #{keg}"
name_and_flag = if keg_only
"--force #{keg.name}"
else
keg.name
end
puts "To relink: brew unlink #{keg.name} && brew link #{name_and_flag}"
next
end
if mode.dry_run
if mode.overwrite
puts "Would remove:"
else
puts "Would link:"
end
keg.link(mode)
puts_keg_only_path_message(keg) if keg_only
next
end
if keg_only
if HOMEBREW_PREFIX.to_s == "/usr/local"
if keg.to_formula.keg_only_reason.reason == :provided_by_macos &&
(MacOS.version >= :mojave ||
MacOS::Xcode.version >= "10.0" ||
MacOS::CLT.version >= "10.0")
opoo <<~EOS
Refusing to link macOS-provided software: #{keg.name}
Instead, pass the full include/library paths to your compiler e.g.:
-I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib
EOS
next
end
if keg.name.start_with?("openssl", "libressl") if keg.name.start_with?("openssl", "libressl")
opoo <<~EOS opoo <<~EOS
Refusing to link: #{keg.name} Refusing to link: #{keg.name}
@ -37,37 +73,14 @@ module Homebrew
-I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib -I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib
EOS EOS
next next
elsif (MacOS.version >= :mojave ||
MacOS::Xcode.version >= "10.0" ||
MacOS::CLT.version >= "10.0") &&
keg.to_formula.keg_only_reason.reason == :provided_by_macos
opoo <<~EOS
Refusing to link macOS-provided software: #{keg.name}
Instead, pass the full include/library paths to your compiler e.g.:
-I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib
EOS
next
end end
elsif keg.linked? end
opoo "Already linked: #{keg}"
puts "To relink: brew unlink #{keg.name} && brew link #{keg.name}" unless ARGV.force?
next
elsif keg_only && !ARGV.force?
opoo "#{keg.name} is keg-only and must be linked with --force" opoo "#{keg.name} is keg-only and must be linked with --force"
puts "Note that doing so can interfere with building software."
puts_keg_only_path_message(keg) puts_keg_only_path_message(keg)
next next
elsif mode.dry_run && mode.overwrite end
puts "Would remove:"
keg.link(mode)
next
elsif mode.dry_run
puts "Would link:"
keg.link(mode)
puts_keg_only_path_message(keg) if keg_only
next
end end
keg.lock do keg.lock do

View File

@ -37,9 +37,8 @@ describe "brew link", :integration_test do
expect { brew "link", "testball1", "SHELL" => "/bin/zsh" } expect { brew "link", "testball1", "SHELL" => "/bin/zsh" }
.to output(/testball1 is keg-only/).to_stderr .to output(/testball1 is keg-only/).to_stderr
.and output(a_string_matching(/Note that doing so can interfere with building software\./) .and output(a_string_matching(/If you need to have this software first in your PATH instead consider running:/)
.and(matching("If you need to have this software first in your PATH instead consider running:") .and(including("echo 'export PATH=\"#{HOMEBREW_PREFIX}/opt/testball1/bin:$PATH\"' >> ~/.zshrc"))).to_stdout
.and(including("echo 'export PATH=\"#{HOMEBREW_PREFIX}/opt/testball1/bin:$PATH\"' >> ~/.zshrc")))).to_stdout
.and be_a_success .and be_a_success
end end
end end