2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
require "ostruct"
|
2018-07-26 10:49:55 +01:00
|
|
|
require "caveats"
|
2019-04-17 18:25:08 +09:00
|
|
|
require "cli/parser"
|
2012-10-20 20:54:11 -05:00
|
|
|
|
2014-06-18 22:41:47 -05:00
|
|
|
module Homebrew
|
2016-09-26 01:44:51 +02:00
|
|
|
module_function
|
|
|
|
|
2018-11-05 12:20:10 +05:30
|
|
|
def link_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
|
|
|
usage_banner <<~EOS
|
2019-08-06 14:17:17 -04:00
|
|
|
`link`, `ln` [<options>] <formula>
|
2018-11-05 12:20:10 +05:30
|
|
|
|
2019-08-20 00:04:14 -04:00
|
|
|
Symlink all of <formula>'s installed files into Homebrew's prefix. This
|
2018-11-05 12:20:10 +05:30
|
|
|
is done automatically when you install formulae but can be useful for DIY
|
|
|
|
installations.
|
|
|
|
EOS
|
|
|
|
switch "--overwrite",
|
2019-08-20 00:04:14 -04:00
|
|
|
description: "Delete files that already exist in the prefix while linking."
|
2018-11-05 12:20:10 +05:30
|
|
|
switch "-n", "--dry-run",
|
2019-08-20 00:04:14 -04:00
|
|
|
description: "List files which would be linked or deleted by "\
|
|
|
|
"`brew link --overwrite` without actually linking or deleting any files."
|
2020-07-27 03:59:52 +02:00
|
|
|
switch "-f", "--force",
|
2019-08-05 15:10:00 +10:00
|
|
|
description: "Allow keg-only formulae to be linked."
|
2020-07-30 18:40:10 +02:00
|
|
|
|
2020-03-04 17:28:15 +00:00
|
|
|
min_named :keg
|
2018-11-05 12:20:10 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-11 20:22:54 +01:00
|
|
|
def link
|
2020-07-30 18:40:10 +02:00
|
|
|
args = link_args.parse
|
2018-11-05 12:20:10 +05:30
|
|
|
|
2020-08-02 04:46:32 +02:00
|
|
|
options = {
|
|
|
|
overwrite: args.overwrite?,
|
|
|
|
dry_run: args.dry_run?,
|
|
|
|
verbose: args.verbose?,
|
|
|
|
}
|
2012-06-17 16:54:20 -05:00
|
|
|
|
2020-08-19 10:34:48 -04:00
|
|
|
args.named.to_kegs.each do |keg|
|
2018-06-05 23:19:18 -04:00
|
|
|
keg_only = Formulary.keg_only?(keg.rack)
|
2018-07-10 09:32:42 +01:00
|
|
|
|
|
|
|
if keg.linked?
|
2012-03-19 12:24:13 +00:00
|
|
|
opoo "Already linked: #{keg}"
|
2018-07-10 09:32:42 +01:00
|
|
|
name_and_flag = if keg_only
|
|
|
|
"--force #{keg.name}"
|
|
|
|
else
|
|
|
|
keg.name
|
|
|
|
end
|
2019-12-13 15:39:55 -05:00
|
|
|
puts <<~EOS
|
|
|
|
To relink:
|
|
|
|
brew unlink #{keg.name} && brew link #{name_and_flag}
|
|
|
|
EOS
|
2013-05-20 21:55:01 -05:00
|
|
|
next
|
2018-07-10 09:32:42 +01:00
|
|
|
end
|
2012-06-17 16:54:20 -05:00
|
|
|
|
2020-08-02 04:46:32 +02:00
|
|
|
if args.dry_run?
|
|
|
|
if args.overwrite?
|
2018-07-10 09:32:42 +01:00
|
|
|
puts "Would remove:"
|
|
|
|
else
|
|
|
|
puts "Would link:"
|
|
|
|
end
|
2020-08-02 04:46:32 +02:00
|
|
|
keg.link(**options)
|
2017-03-23 08:42:27 +00:00
|
|
|
puts_keg_only_path_message(keg) if keg_only
|
2012-06-17 16:54:20 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2018-07-10 09:32:42 +01:00
|
|
|
if keg_only
|
2020-10-30 10:40:10 -07:00
|
|
|
if Homebrew.default_prefix?
|
|
|
|
f = keg.to_formula
|
|
|
|
if f.keg_only_reason.by_macos?
|
|
|
|
caveats = Caveats.new(f)
|
|
|
|
opoo <<~EOS
|
|
|
|
Refusing to link macOS provided/shadowed software: #{keg.name}
|
|
|
|
#{caveats.keg_only_text(skip_reason: true).strip}
|
|
|
|
EOS
|
|
|
|
next
|
|
|
|
end
|
2018-07-10 09:32:42 +01:00
|
|
|
end
|
|
|
|
|
2020-10-30 10:40:10 -07:00
|
|
|
unless args.force?
|
2018-07-10 09:32:42 +01:00
|
|
|
opoo "#{keg.name} is keg-only and must be linked with --force"
|
|
|
|
puts_keg_only_path_message(keg)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-23 00:26:25 -06:00
|
|
|
keg.lock do
|
2014-04-21 09:40:24 -05:00
|
|
|
print "Linking #{keg}... "
|
2018-11-05 12:20:10 +05:30
|
|
|
puts if args.verbose?
|
2014-04-21 09:40:24 -05:00
|
|
|
|
|
|
|
begin
|
2020-08-02 04:46:32 +02:00
|
|
|
n = keg.link(**options)
|
2014-04-21 09:40:24 -05:00
|
|
|
rescue Keg::LinkError
|
|
|
|
puts
|
|
|
|
raise
|
|
|
|
else
|
|
|
|
puts "#{n} symlinks created"
|
2013-01-23 00:26:25 -06:00
|
|
|
end
|
2017-03-23 08:42:27 +00:00
|
|
|
|
2020-04-05 15:44:50 +01:00
|
|
|
puts_keg_only_path_message(keg) if keg_only && !Homebrew::EnvConfig.developer?
|
2012-03-19 00:15:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-23 08:42:27 +00:00
|
|
|
def puts_keg_only_path_message(keg)
|
|
|
|
bin = keg/"bin"
|
|
|
|
sbin = keg/"sbin"
|
|
|
|
return if !bin.directory? && !sbin.directory?
|
|
|
|
|
|
|
|
opt = HOMEBREW_PREFIX/"opt/#{keg.name}"
|
|
|
|
puts "\nIf you need to have this software first in your PATH instead consider running:"
|
2017-04-22 16:28:07 +01:00
|
|
|
puts " #{Utils::Shell.prepend_path_in_profile(opt/"bin")}" if bin.directory?
|
|
|
|
puts " #{Utils::Shell.prepend_path_in_profile(opt/"sbin")}" if sbin.directory?
|
2017-03-23 08:42:27 +00:00
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|