aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils/link.rb
blob: 095dba20915f964c394c798480b01e89fc77178d (plain)
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
module Utils
  module Link
    module_function

    def link_src_dst_dirs(src_dir, dst_dir, command, link_dir: false)
      return unless src_dir.exist?
      conflicts = []
      src_paths = link_dir ? [src_dir] : src_dir.find
      src_paths.each do |src|
        next if src.directory? && !link_dir
        dst = dst_dir/src.relative_path_from(src_dir)
        if dst.symlink?
          next if src == dst.resolved_path
          dst.unlink
        end
        if dst.exist?
          conflicts << dst
          next
        end
        dst_dir.parent.mkpath
        dst.make_relative_symlink(src)
      end

      return if conflicts.empty?
      onoe <<-EOS.undent
        Could not link:
        #{conflicts.join("\n")}

        Please delete these paths and run `#{command}`.
      EOS
    end
    private_class_method :link_src_dst_dirs

    def unlink_src_dst_dirs(src_dir, dst_dir, unlink_dir: false)
      return unless src_dir.exist?
      src_paths = unlink_dir ? [src_dir] : src_dir.find
      src_paths.each do |src|
        next if src.directory? && !unlink_dir
        dst = dst_dir/src.relative_path_from(src_dir)
        dst.delete if dst.symlink? && src == dst.resolved_path
        dst.parent.rmdir_if_possible
      end
    end
    private_class_method :unlink_src_dst_dirs

    def link_manpages(path, command)
      link_src_dst_dirs(path/"manpages", HOMEBREW_PREFIX/"share/man/man1", command)
    end

    def unlink_manpages(path)
      unlink_src_dst_dirs(path/"manpages", HOMEBREW_PREFIX/"share/man/man1")
    end

    def link_completions(path, command)
      link_src_dst_dirs(path/"completions/bash", HOMEBREW_PREFIX/"etc/bash_completion.d", command)
      link_src_dst_dirs(path/"completions/zsh", HOMEBREW_PREFIX/"share/zsh/site-functions", command)
      link_src_dst_dirs(path/"completions/fish", HOMEBREW_PREFIX/"share/fish/vendor_completions.d", command)
    end

    def unlink_completions(path)
      unlink_src_dst_dirs(path/"completions/bash", HOMEBREW_PREFIX/"etc/bash_completion.d")
      unlink_src_dst_dirs(path/"completions/zsh", HOMEBREW_PREFIX/"share/zsh/site-functions")
      unlink_src_dst_dirs(path/"completions/fish", HOMEBREW_PREFIX/"share/fish/vendor_completions.d")
    end

    def link_docs(path, command)
      link_src_dst_dirs(path/"docs", HOMEBREW_PREFIX/"share/doc/homebrew", command, link_dir: true)
    end
  end
end