2021-01-08 11:10:24 -05:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "utils/link"
|
|
|
|
|
|
|
|
# Helper functions for generating shell completions.
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
module Completions
|
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
module_function
|
|
|
|
|
2021-01-11 12:24:48 -05:00
|
|
|
sig { void }
|
|
|
|
def link!
|
2021-01-08 11:10:24 -05:00
|
|
|
write_completions_option "yes"
|
2021-01-11 12:24:48 -05:00
|
|
|
Tap.each do |tap|
|
|
|
|
Utils::Link.link_completions tap.path, "brew completions link"
|
|
|
|
end
|
2021-01-08 11:10:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
sig { void }
|
|
|
|
def unlink!
|
|
|
|
write_completions_option "no"
|
2021-01-11 12:24:48 -05:00
|
|
|
Tap.each do |tap|
|
|
|
|
next if tap.official?
|
|
|
|
|
|
|
|
Utils::Link.unlink_completions tap.path
|
|
|
|
end
|
2021-01-08 11:10:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def link_completions?
|
|
|
|
read_completions_option == "yes"
|
|
|
|
end
|
|
|
|
|
2021-01-11 12:24:48 -05:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def completions_to_link?
|
|
|
|
shells = %w[bash fish zsh]
|
|
|
|
Tap.each do |tap|
|
|
|
|
next if tap.official?
|
|
|
|
|
|
|
|
shells.each do |shell|
|
|
|
|
return true if (tap.path/"completions/#{shell}").exist?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { params(option: String).returns(String) }
|
|
|
|
def read_completions_option(option: "linkcompletions")
|
2021-01-08 11:10:24 -05:00
|
|
|
HOMEBREW_REPOSITORY.cd do
|
2021-01-11 12:24:48 -05:00
|
|
|
Utils.popen_read("git", "config", "--get", "homebrew.#{option}").chomp
|
2021-01-08 11:10:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-11 12:24:48 -05:00
|
|
|
sig { params(state: String, option: String).void }
|
|
|
|
def write_completions_option(state, option: "linkcompletions")
|
2021-01-08 11:10:24 -05:00
|
|
|
HOMEBREW_REPOSITORY.cd do
|
2021-01-11 12:24:48 -05:00
|
|
|
T.unsafe(self).safe_system "git", "config", "--replace-all", "homebrew.#{option}", state.to_s
|
2021-01-08 11:10:24 -05:00
|
|
|
end
|
|
|
|
end
|
2021-01-11 12:24:48 -05:00
|
|
|
|
|
|
|
sig { void }
|
|
|
|
def show_completions_message_if_needed
|
|
|
|
return if read_completions_option(option: "completionsmessageshown") == "yes"
|
|
|
|
return unless completions_to_link?
|
|
|
|
|
|
|
|
T.unsafe(self).ohai "Homebrew completions for external commands are unlinked by default!"
|
|
|
|
T.unsafe(self).puts <<~EOS
|
|
|
|
To opt-in to automatically linking Homebrew shell competion files, run:
|
|
|
|
brew completions link
|
|
|
|
Then, follow the directions at #{Formatter.url("https://docs.brew.sh/Shell-Completion")}
|
|
|
|
EOS
|
|
|
|
|
|
|
|
write_completions_option("yes", option: "completionsmessageshown")
|
|
|
|
end
|
2021-01-08 11:10:24 -05:00
|
|
|
end
|