From 7cc970fea233ff7d71cfee2e5efb914aa384357f Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Tue, 10 Jul 2018 09:32:42 +0100 Subject: [PATCH] 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. --- Library/Homebrew/cmd/link.rb | 85 +++++++++++++++----------- Library/Homebrew/test/cmd/link_spec.rb | 5 +- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/Library/Homebrew/cmd/link.rb b/Library/Homebrew/cmd/link.rb index 3f84329730..dac871ade3 100644 --- a/Library/Homebrew/cmd/link.rb +++ b/Library/Homebrew/cmd/link.rb @@ -27,49 +27,62 @@ module Homebrew ARGV.kegs.each do |keg| keg_only = Formulary.keg_only?(keg.rack) - if HOMEBREW_PREFIX.to_s == "/usr/local" && keg_only - if keg.name.start_with?("openssl", "libressl") - opoo <<~EOS - Refusing to link: #{keg.name} - Linking keg-only #{keg.name} means you may end up linking against the insecure, - deprecated system OpenSSL while using the headers from Homebrew's #{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 - 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 - elsif keg.linked? - opoo "Already linked: #{keg}" - puts "To relink: brew unlink #{keg.name} && brew link #{keg.name}" - next - elsif keg_only && !ARGV.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) - next - elsif mode.dry_run && mode.overwrite - puts "Would remove:" - keg.link(mode) + 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 - elsif mode.dry_run - puts "Would link:" + 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") + opoo <<~EOS + Refusing to link: #{keg.name} + Linking keg-only #{keg.name} means you may end up linking against the insecure, + deprecated system OpenSSL while using the headers from Homebrew's #{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 + + unless ARGV.force? + opoo "#{keg.name} is keg-only and must be linked with --force" + puts_keg_only_path_message(keg) + next + end + end + keg.lock do print "Linking #{keg}... " puts if ARGV.verbose? diff --git a/Library/Homebrew/test/cmd/link_spec.rb b/Library/Homebrew/test/cmd/link_spec.rb index 565de5b537..27ccd0bd5b 100644 --- a/Library/Homebrew/test/cmd/link_spec.rb +++ b/Library/Homebrew/test/cmd/link_spec.rb @@ -37,9 +37,8 @@ describe "brew link", :integration_test do expect { brew "link", "testball1", "SHELL" => "/bin/zsh" } .to output(/testball1 is keg-only/).to_stderr - .and output(a_string_matching(/Note that doing so can interfere with building software\./) - .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 output(a_string_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 be_a_success end end