Cache commands list for faster shell completions

This commit is contained in:
Caleb Xu 2020-06-17 18:03:40 -04:00
parent aeafbd1c8f
commit a53c92bd7f
8 changed files with 129 additions and 3 deletions

View File

@ -233,6 +233,10 @@ HOMEBREW_CACHE="${HOMEBREW_CACHE:-${HOMEBREW_DEFAULT_CACHE}}"
HOMEBREW_LOGS="${HOMEBREW_LOGS:-${HOMEBREW_DEFAULT_LOGS}}"
HOMEBREW_TEMP="${HOMEBREW_TEMP:-${HOMEBREW_DEFAULT_TEMP}}"
case "$*" in
--cache) echo "$HOMEBREW_CACHE"; exit 0 ;;
esac
HOMEBREW_USER_AGENT="$HOMEBREW_PRODUCT/$HOMEBREW_USER_AGENT_VERSION ($HOMEBREW_SYSTEM; $HOMEBREW_PROCESSOR $HOMEBREW_OS_USER_AGENT_VERSION)"
curl_version_output="$("$HOMEBREW_CURL" --version 2>/dev/null)"
curl_name_and_version="${curl_version_output%% (*}"

View File

@ -129,6 +129,7 @@ module Homebrew
puts if args.preinstall?
end
Commands.rebuild_commands_completion_list
link_completions_manpages_and_docs
Tap.each(&:link_completions_and_manpages)
end

View File

@ -604,6 +604,7 @@ EOS
-n "$HOMEBREW_UPDATE_FAILED" ||
-n "$HOMEBREW_UPDATE_FORCE" ||
-d "$HOMEBREW_LIBRARY/LinkedKegs" ||
! -f "$HOMEBREW_CACHE/all_commands_list.txt" ||
(-n "$HOMEBREW_DEVELOPER" && -z "$HOMEBREW_UPDATE_PREINSTALL") ]]
then
brew update-report "$@"

View File

@ -143,4 +143,21 @@ module Commands
.select(&:file?)
.sort
end
def rebuild_internal_commands_completion_list
cmds = internal_commands + internal_developer_commands + internal_commands_aliases
file = HOMEBREW_REPOSITORY/"completions/internal_commands_list.txt"
file.delete if file.exist?
file.write(cmds.sort.join("\n") + "\n")
end
def rebuild_commands_completion_list
# Ensure that the cache exists so we can build the commands list
HOMEBREW_CACHE.mkpath
file = HOMEBREW_CACHE/"all_commands_list.txt"
file.delete if file.exist?
file.write(commands(aliases: true).sort.join("\n") + "\n")
end
end

View File

@ -35,10 +35,11 @@ module Homebrew
odie "`brew man --link` is now done automatically by `brew update`." if args.link?
Commands.rebuild_internal_commands_completion_list
regenerate_man_pages
if system "git", "-C", HOMEBREW_REPOSITORY, "diff", "--quiet", "docs/Manpage.md", "manpages"
puts "No changes to manpage output detected."
if system "git", "-C", HOMEBREW_REPOSITORY, "diff", "--quiet", "docs/Manpage.md", "manpages", "completions"
puts "No changes to manpage or completions output detected."
elsif args.fail_if_changed?
Homebrew.failed = true
end

View File

@ -286,6 +286,7 @@ class Tap
config["forceautoupdate"] = force_auto_update unless force_auto_update.nil?
Commands.rebuild_commands_completion_list
link_completions_and_manpages
formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ")
@ -334,6 +335,8 @@ class Tap
path.rmtree
path.parent.rmdir_if_possible
puts "Untapped#{formatted_contents} (#{abv})."
Commands.rebuild_commands_completion_list
clear_cache
end

View File

@ -0,0 +1,95 @@
--cache
--cellar
--config
--env
--prefix
--repo
--repository
--version
-S
-v
abv
analytics
audit
bottle
bump-formula-pr
bump-revision
cask
cat
cleanup
command
commands
config
configure
create
deps
desc
diy
doctor
dr
edit
environment
extract
fetch
formula
gist-logs
help
home
homepage
info
instal
install
install-bundler-gems
irb
leaves
link
linkage
list
ln
log
ls
man
migrate
mirror
missing
options
outdated
pin
postinstall
pr-automerge
pr-publish
pr-pull
pr-upload
prof
pull
readall
reinstall
release-notes
remove
rm
ruby
search
sh
shellenv
style
switch
tap
tap-info
tap-new
test
tests
uninstal
uninstall
unlink
unpack
unpin
untap
up
update
update-report
update-reset
update-test
upgrade
uses
vendor-gems
vendor-install

View File

@ -148,7 +148,11 @@ __brew_all_commands() {
local -a commands
local comp_cachename=brew_all_commands
if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then
commands=($(_call_program brew brew commands --quiet --include-aliases))
HOMEBREW_CACHE=$(brew --cache)
HOMEBREW_REPOSITORY=$(brew --repo)
[[ -f "$HOMEBREW_CACHE/all_commands_list.txt" ]] &&
commands=($(cat "$HOMEBREW_CACHE/all_commands_list.txt")) ||
commands=($(cat "$HOMEBREW_REPOSITORY/completions/internal_commands_list.txt"))
commands=(${commands:#*instal}) # Exclude instal, uninstal, etc.
_store_cache $comp_cachename commands
fi