Skip to content

Commit 0a8c8f9

Browse files
authored
Merge pull request #1708 from joshka/feature/install-tap-cmd-completions
install tap cmd completions
2 parents ef85460 + 25396d9 commit 0a8c8f9

File tree

7 files changed

+126
-60
lines changed

7 files changed

+126
-60
lines changed

Library/Homebrew/cmd/list.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def list
7777
lib/ruby/site_ruby/[12].*
7878
lib/ruby/vendor_ruby/[12].*
7979
manpages/brew.1
80+
manpages/brew-cask.1
8081
share/pypy/*
8182
share/pypy3/*
8283
share/info/dir

Library/Homebrew/cmd/tap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module Homebrew
3838

3939
def tap
4040
if ARGV.include? "--repair"
41-
Tap.each(&:link_manpages)
41+
Tap.each(&:link_completions_and_manpages)
4242
elsif ARGV.include? "--list-official"
4343
require "official_taps"
4444
puts OFFICIAL_TAPS.map { |t| "homebrew/#{t}" }

Library/Homebrew/cmd/update-report.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def update_report
104104
puts if ARGV.include?("--preinstall")
105105
end
106106

107-
link_completions_and_docs
108-
Tap.each(&:link_manpages)
107+
link_completions_manpages_and_docs
108+
Tap.each(&:link_completions_and_manpages)
109109

110110
Homebrew.failed = true if ENV["HOMEBREW_UPDATE_FAILED"]
111111

@@ -281,7 +281,7 @@ def migrate_legacy_repository_if_necessary
281281
EOS
282282
end
283283

284-
link_completions_and_docs(new_homebrew_repository)
284+
link_completions_manpages_and_docs(new_homebrew_repository)
285285

286286
ohai "Migrated HOMEBREW_REPOSITORY to #{new_homebrew_repository}!"
287287
puts <<-EOS.undent
@@ -302,16 +302,11 @@ def migrate_legacy_repository_if_necessary
302302
$stderr.puts e.backtrace
303303
end
304304

305-
def link_completions_and_docs(repository = HOMEBREW_REPOSITORY)
305+
def link_completions_manpages_and_docs(repository = HOMEBREW_REPOSITORY)
306306
command = "brew update"
307-
link_src_dst_dirs(repository/"completions/bash",
308-
HOMEBREW_PREFIX/"etc/bash_completion.d", command)
309-
link_src_dst_dirs(repository/"docs",
310-
HOMEBREW_PREFIX/"share/doc/homebrew", command, link_dir: true)
311-
link_src_dst_dirs(repository/"completions/zsh",
312-
HOMEBREW_PREFIX/"share/zsh/site-functions", command)
313-
link_src_dst_dirs(repository/"manpages",
314-
HOMEBREW_PREFIX/"share/man/man1", command)
307+
Utils::Link.link_completions(repository, command)
308+
Utils::Link.link_manpages(repository, command)
309+
Utils::Link.link_docs(repository, command)
315310
rescue => e
316311
ofail <<-EOS.undent
317312
Failed to link all completions, docs and manpages:

Library/Homebrew/tap.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def install(options = {})
236236
raise
237237
end
238238

239-
link_manpages
239+
link_completions_and_manpages
240240

241241
formula_count = formula_files.size
242242
puts "Tapped #{formula_count} formula#{plural(formula_count, "e")} (#{path.abv})" unless quiet
@@ -254,8 +254,10 @@ def install(options = {})
254254
EOS
255255
end
256256

257-
def link_manpages
258-
link_path_manpages(path, "brew tap --repair")
257+
def link_completions_and_manpages
258+
command = "brew tap --repair"
259+
Utils::Link.link_manpages(path, command)
260+
Utils::Link.link_completions(path, command)
259261
end
260262

261263
# uninstall this {Tap}.
@@ -267,23 +269,14 @@ def uninstall
267269
unpin if pinned?
268270
formula_count = formula_files.size
269271
Descriptions.uncache_formulae(formula_names)
270-
unlink_manpages
272+
Utils::Link.unlink_manpages(path)
273+
Utils::Link.unlink_completions(path)
271274
path.rmtree
272275
path.parent.rmdir_if_possible
273276
puts "Untapped #{formula_count} formula#{plural(formula_count, "e")}"
274277
clear_cache
275278
end
276279

277-
def unlink_manpages
278-
return unless (path/"man").exist?
279-
(path/"man").find do |src|
280-
next if src.directory?
281-
dst = HOMEBREW_PREFIX/"share"/src.relative_path_from(path)
282-
dst.delete if dst.symlink? && src == dst.resolved_path
283-
dst.parent.rmdir_if_possible
284-
end
285-
end
286-
287280
# True if the {#remote} of {Tap} is customized.
288281
def custom_remote?
289282
return true unless remote

Library/Homebrew/test/tap_test.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ class Foo < Formula
3030
@cmd_file.parent.mkpath
3131
touch @cmd_file
3232
chmod 0755, @cmd_file
33-
@manpage_file = @path/"man/man1/brew-tap-cmd.1"
33+
@manpage_file = @path/"manpages/brew-tap-cmd.1"
3434
@manpage_file.parent.mkpath
3535
touch @manpage_file
36+
@bash_completion_file = @path/"completions/bash/brew-tap-cmd"
37+
@bash_completion_file.parent.mkpath
38+
touch @bash_completion_file
39+
@zsh_completion_file = @path/"completions/zsh/_brew-tap-cmd"
40+
@zsh_completion_file.parent.mkpath
41+
touch @zsh_completion_file
42+
@fish_completion_file = @path/"completions/fish/brew-tap-cmd.fish"
43+
@fish_completion_file.parent.mkpath
44+
touch @fish_completion_file
3645
end
3746

3847
def setup_git_repo
@@ -208,10 +217,39 @@ def test_install_and_uninstall
208217
shutup { tap.install clone_target: @tap.path/".git" }
209218
assert_predicate tap, :installed?
210219
assert_predicate HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1", :file?
220+
assert_predicate HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd", :file?
221+
assert_predicate HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd", :file?
222+
assert_predicate HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish", :file?
211223
shutup { tap.uninstall }
212224
refute_predicate tap, :installed?
213225
refute_predicate HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1", :exist?
214226
refute_predicate HOMEBREW_PREFIX/"share/man/man1", :exist?
227+
refute_predicate HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd", :exist?
228+
refute_predicate HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd", :exist?
229+
refute_predicate HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish", :exist?
230+
ensure
231+
(HOMEBREW_PREFIX/"etc").rmtree if (HOMEBREW_PREFIX/"etc").exist?
232+
(HOMEBREW_PREFIX/"share").rmtree if (HOMEBREW_PREFIX/"share").exist?
233+
end
234+
235+
def test_link_completions_and_manpages
236+
setup_tap_files
237+
setup_git_repo
238+
tap = Tap.new("Homebrew", "baz")
239+
shutup { tap.install clone_target: @tap.path/".git" }
240+
(HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1").delete
241+
(HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd").delete
242+
(HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd").delete
243+
(HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish").delete
244+
shutup { tap.link_completions_and_manpages }
245+
assert_predicate HOMEBREW_PREFIX/"share/man/man1/brew-tap-cmd.1", :file?
246+
assert_predicate HOMEBREW_PREFIX/"etc/bash_completion.d/brew-tap-cmd", :file?
247+
assert_predicate HOMEBREW_PREFIX/"share/zsh/site-functions/_brew-tap-cmd", :file?
248+
assert_predicate HOMEBREW_PREFIX/"share/fish/vendor_completions.d/brew-tap-cmd.fish", :file?
249+
shutup { tap.uninstall }
250+
ensure
251+
(HOMEBREW_PREFIX/"etc").rmtree if (HOMEBREW_PREFIX/"etc").exist?
252+
(HOMEBREW_PREFIX/"share").rmtree if (HOMEBREW_PREFIX/"share").exist?
215253
end
216254

217255
def test_pin_and_unpin

Library/Homebrew/utils.rb

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require "utils/github"
1010
require "utils/hash"
1111
require "utils/inreplace"
12+
require "utils/link"
1213
require "utils/popen"
1314
require "utils/svn"
1415
require "utils/tty"
@@ -482,38 +483,6 @@ def truncate_text_to_approximate_size(s, max_bytes, options = {})
482483
out
483484
end
484485

485-
def link_src_dst_dirs(src_dir, dst_dir, command, link_dir: false)
486-
return unless src_dir.exist?
487-
conflicts = []
488-
src_paths = link_dir ? [src_dir] : src_dir.find
489-
src_paths.each do |src|
490-
next if src.directory? && !link_dir
491-
dst = dst_dir/src.relative_path_from(src_dir)
492-
if dst.symlink?
493-
next if src == dst.resolved_path
494-
dst.unlink
495-
end
496-
if dst.exist?
497-
conflicts << dst
498-
next
499-
end
500-
dst_dir.parent.mkpath
501-
dst.make_relative_symlink(src)
502-
end
503-
504-
return if conflicts.empty?
505-
onoe <<-EOS.undent
506-
Could not link:
507-
#{conflicts.join("\n")}
508-
509-
Please delete these paths and run `#{command}`.
510-
EOS
511-
end
512-
513-
def link_path_manpages(path, command)
514-
link_src_dst_dirs(path/"man", HOMEBREW_PREFIX/"share/man", command)
515-
end
516-
517486
def migrate_legacy_keg_symlinks_if_necessary
518487
legacy_linked_kegs = HOMEBREW_LIBRARY/"LinkedKegs"
519488
return unless legacy_linked_kegs.directory?

Library/Homebrew/utils/link.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module Utils
2+
module Link
3+
module_function
4+
5+
def link_src_dst_dirs(src_dir, dst_dir, command, link_dir: false)
6+
return unless src_dir.exist?
7+
conflicts = []
8+
src_paths = link_dir ? [src_dir] : src_dir.find
9+
src_paths.each do |src|
10+
next if src.directory? && !link_dir
11+
dst = dst_dir/src.relative_path_from(src_dir)
12+
if dst.symlink?
13+
next if src == dst.resolved_path
14+
dst.unlink
15+
end
16+
if dst.exist?
17+
conflicts << dst
18+
next
19+
end
20+
dst_dir.parent.mkpath
21+
dst.make_relative_symlink(src)
22+
end
23+
24+
return if conflicts.empty?
25+
onoe <<-EOS.undent
26+
Could not link:
27+
#{conflicts.join("\n")}
28+
29+
Please delete these paths and run `#{command}`.
30+
EOS
31+
end
32+
private_class_method :link_src_dst_dirs
33+
34+
def unlink_src_dst_dirs(src_dir, dst_dir, unlink_dir: false)
35+
return unless src_dir.exist?
36+
src_paths = unlink_dir ? [src_dir] : src_dir.find
37+
src_paths.each do |src|
38+
next if src.directory? && !unlink_dir
39+
dst = dst_dir/src.relative_path_from(src_dir)
40+
dst.delete if dst.symlink? && src == dst.resolved_path
41+
dst.parent.rmdir_if_possible
42+
end
43+
end
44+
private_class_method :unlink_src_dst_dirs
45+
46+
def link_manpages(path, command)
47+
link_src_dst_dirs(path/"manpages", HOMEBREW_PREFIX/"share/man/man1", command)
48+
end
49+
50+
def unlink_manpages(path)
51+
unlink_src_dst_dirs(path/"manpages", HOMEBREW_PREFIX/"share/man/man1")
52+
end
53+
54+
def link_completions(path, command)
55+
link_src_dst_dirs(path/"completions/bash", HOMEBREW_PREFIX/"etc/bash_completion.d", command)
56+
link_src_dst_dirs(path/"completions/zsh", HOMEBREW_PREFIX/"share/zsh/site-functions", command)
57+
link_src_dst_dirs(path/"completions/fish", HOMEBREW_PREFIX/"share/fish/vendor_completions.d", command)
58+
end
59+
60+
def unlink_completions(path)
61+
unlink_src_dst_dirs(path/"completions/bash", HOMEBREW_PREFIX/"etc/bash_completion.d")
62+
unlink_src_dst_dirs(path/"completions/zsh", HOMEBREW_PREFIX/"share/zsh/site-functions")
63+
unlink_src_dst_dirs(path/"completions/fish", HOMEBREW_PREFIX/"share/fish/vendor_completions.d")
64+
end
65+
66+
def link_docs(path, command)
67+
link_src_dst_dirs(path/"docs", HOMEBREW_PREFIX/"share/doc/homebrew", command, link_dir: true)
68+
end
69+
end
70+
end

0 commit comments

Comments
 (0)