aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils
diff options
context:
space:
mode:
authorJoshua McKinney2016-09-30 18:22:53 -0500
committerJoshua McKinney2017-02-26 15:40:52 -0600
commit25396d9c4d7a7a111eebf02f0ebdbb0e69aad3b0 (patch)
treef91be666cdc54f1a69f418a7a54068509105dd23 /Library/Homebrew/utils
parenta74b7ade66e340c55b3bea7b6226fa68a311b4ce (diff)
downloadbrew-25396d9c4d7a7a111eebf02f0ebdbb0e69aad3b0.tar.bz2
Install tap command completions and manpages
Taps can include completion scripts for external commands under `completions/bash`, `completions/fish`, or `completions/zsh`. `brew tap` will automatically install these into the correct directories during install.
Diffstat (limited to 'Library/Homebrew/utils')
-rw-r--r--Library/Homebrew/utils/link.rb70
1 files changed, 70 insertions, 0 deletions
diff --git a/Library/Homebrew/utils/link.rb b/Library/Homebrew/utils/link.rb
new file mode 100644
index 000000000..095dba209
--- /dev/null
+++ b/Library/Homebrew/utils/link.rb
@@ -0,0 +1,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