-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
symlinked.rb
87 lines (70 loc) 路 2.5 KB
/
symlinked.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# typed: true
# frozen_string_literal: true
require "cask/artifact/relocated"
module Cask
module Artifact
# Superclass for all artifacts which are installed by symlinking them to the target location.
#
# @api private
class Symlinked < Relocated
sig { returns(String) }
def self.link_type_english_name
"Symlink"
end
sig { returns(String) }
def self.english_description
"#{english_name} #{link_type_english_name}s"
end
def install_phase(**options)
link(**options)
end
def uninstall_phase(**options)
unlink(**options)
end
def summarize_installed
if target.symlink? && target.exist? && target.readlink.exist?
"#{printable_target} -> #{target.readlink} (#{target.readlink.abv})"
else
string = if target.symlink?
"#{printable_target} -> #{target.readlink}"
else
printable_target
end
Formatter.error(string, label: "Broken Link")
end
end
private
def link(force: false, adopt: false, command: nil, **_options)
unless source.exist?
raise CaskError,
"It seems the #{self.class.link_type_english_name.downcase} " \
"source '#{source}' is not there."
end
if target.exist?
message = "It seems there is already #{self.class.english_article} " \
"#{self.class.english_name} at '#{target}'"
if (force || adopt) && target.symlink? &&
(target.realpath == source.realpath || target.realpath.to_s.start_with?("#{cask.caskroom_path}/"))
opoo "#{message}; overwriting."
Utils.gain_permissions_remove(target, command:)
else
raise CaskError, "#{message}."
end
end
ohai "Linking #{self.class.english_name} '#{source.basename}' to '#{target}'"
create_filesystem_link(command:)
end
def unlink(command: nil, **)
return unless target.symlink?
ohai "Unlinking #{self.class.english_name} '#{target}'"
Utils.gain_permissions_remove(target, command:)
end
def create_filesystem_link(command: nil)
Utils.gain_permissions_mkpath(target.dirname, command:)
command.run! "/bin/ln", args: ["-h", "-f", "-s", "--", source, target],
sudo: !target.dirname.writable?
add_altname_metadata(source, target.basename, command:)
end
end
end
end