Skip to content

Commit

Permalink
Add type signatures for Tap.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 23, 2024
1 parent a13804f commit 03278ab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/diagnostic.rb
Expand Up @@ -537,7 +537,7 @@ def check_casktap_integrity
core_cask_tap = CoreCaskTap.instance
return unless core_cask_tap.installed?

broken_tap(core_cask_tap) || examine_git_origin(core_cask_tap.git_repo, core_cask_tap.remote)
broken_tap(core_cask_tap) || examine_git_origin(core_cask_tap.git_repo, T.must(core_cask_tap.remote))
end

sig { returns(T.nilable(String)) }
Expand Down
73 changes: 61 additions & 12 deletions Library/Homebrew/tap.rb
Expand Up @@ -55,6 +55,7 @@ def self.fetch(user, repo = T.unsafe(nil))
cache.fetch(cache_key) { |key| cache[key] = Tap.new(user, repo) }
end

# sig { params(path: T.any(String, Pathname)).returns(T.nilable(T.attached_class)) }
def self.from_path(path)
match = File.expand_path(path).match(HOMEBREW_TAP_PATH_REGEX)
return if match.blank? || match[:user].blank? || match[:repo].blank?
Expand Down Expand Up @@ -105,6 +106,7 @@ def self.install_default_cask_tap_if_necessary(force: false)
attr_reader :git_repo

# @private
sig { params(user: String, repo: String).void }
def initialize(user, repo)
@user = user
@repo = repo
Expand All @@ -115,6 +117,7 @@ def initialize(user, repo)
end

# Clear internal cache.
sig { void }
def clear_cache
@remote = nil
@repo_var = nil
Expand Down Expand Up @@ -163,6 +166,7 @@ def ensure_installed!

# The remote path to this {Tap}.
# e.g. `https://github.com/user/homebrew-repo`
sig { returns(T.nilable(String)) }
def remote
return default_remote unless installed?

Expand All @@ -171,6 +175,7 @@ def remote

# The remote repository name of this {Tap}.
# e.g. `user/homebrew-repo`
sig { returns(T.nilable(String)) }
def remote_repo
@remote_repo ||= remote&.delete_prefix("https://github.com/")
&.delete_prefix("[email protected]:")
Expand All @@ -184,30 +189,35 @@ def default_remote
end

# @private
sig { returns(String) }
def repo_var
@repo_var ||= path.relative_path_from(TAP_DIRECTORY).to_s.tr("^A-Za-z0-9", "_").upcase
end

# True if this {Tap} is a Git repository.
sig { returns(T::Boolean) }
def git?
git_repo.git_repo?
end

# Git branch for this {Tap}.
sig { returns(T.nilable(String)) }
def git_branch
raise TapUnavailableError, name unless installed?

git_repo.branch_name
end

# Git HEAD for this {Tap}.
sig { returns(T.nilable(String)) }
def git_head
raise TapUnavailableError, name unless installed?

@git_head ||= git_repo.head_ref
end

# Time since last git commit for this {Tap}.
sig { returns(T.nilable(String)) }
def git_last_commit
raise TapUnavailableError, name unless installed?

Expand All @@ -223,16 +233,19 @@ def issues_url
"#{default_remote}/issues"
end

sig { returns(String) }
def to_s
name
end

# True if this {Tap} is an official Homebrew tap.
sig { returns(T::Boolean) }
def official?
user == "Homebrew"
end

# True if the remote of this {Tap} is a private repository.
sig { returns(T::Boolean) }
def private?
return @private if instance_variable_defined?(:@private)

Expand All @@ -248,11 +261,13 @@ def config
end

# True if this {Tap} has been installed.
sig { returns(T::Boolean) }
def installed?
path.directory?
end

# True if this {Tap} is not a full clone.
sig { returns(T::Boolean) }
def shallow?
(path/".git/shallow").exist?
end
Expand All @@ -278,6 +293,16 @@ def core_cask_tap?
# @param custom_remote [Boolean] If set, change the tap's remote if already installed.
# @param verify [Boolean] If set, verify all the formula, casks and aliases in the tap are valid.
# @param force [Boolean] If set, force core and cask taps to install even under API mode.
sig {
params(
quiet: T::Boolean,
clone_target: T.nilable(T.any(String, Pathname)),
force_auto_update: T.nilable(T::Boolean),
custom_remote: T::Boolean,
verify: T::Boolean,
force: T::Boolean,
).void
}
def install(quiet: false, clone_target: nil, force_auto_update: nil,
custom_remote: false, verify: false, force: false)
require "descriptions"
Expand All @@ -295,7 +320,7 @@ def install(quiet: false, clone_target: nil, force_auto_update: nil,
requested_remote = clone_target || default_remote

if installed? && !custom_remote
raise TapRemoteMismatchError.new(name, @remote, requested_remote) if clone_target && requested_remote != remote
raise TapRemoteMismatchError.new(name, remote, requested_remote) if clone_target && requested_remote != remote
raise TapAlreadyTappedError, name if force_auto_update.nil? && !shallow?
end

Expand Down Expand Up @@ -403,6 +428,7 @@ def install(quiet: false, clone_target: nil, force_auto_update: nil,
EOS
end

sig { void }
def link_completions_and_manpages
command = "brew tap --repair"
Utils::Link.link_manpages(path, command)
Expand All @@ -415,6 +441,7 @@ def link_completions_and_manpages
end
end

sig { params(requested_remote: T.nilable(String), quiet: T::Boolean).void }
def fix_remote_configuration(requested_remote: nil, quiet: false)
if requested_remote.present?
path.cd do
Expand Down Expand Up @@ -448,6 +475,7 @@ def fix_remote_configuration(requested_remote: nil, quiet: false)
end

# Uninstall this {Tap}.
sig { params(manual: T::Boolean).void }
def uninstall(manual: false)
require "descriptions"
raise TapUnavailableError, name unless installed?
Expand Down Expand Up @@ -484,6 +512,7 @@ def uninstall(manual: false)
end

# True if the {#remote} of {Tap} is customized.
sig { returns(T::Boolean) }
def custom_remote?
!remote&.casecmp(default_remote)&.zero?
end
Expand Down Expand Up @@ -526,6 +555,8 @@ def relative_cask_path(token)
.delete_prefix("#{path}/")
end

# @private
sig { returns(T::Array[String]) }
def contents
contents = []

Expand Down Expand Up @@ -830,6 +861,11 @@ def ==(other)
self.class == other.class && name == other.name
end

sig {
override.params(
block: T.nilable(T.proc.params(arg0: T.attached_class).returns(T.untyped)),
).returns(T::Enumerable[T.attached_class])
}
def self.each(&block)
return to_enum unless block

Expand Down Expand Up @@ -882,6 +918,7 @@ def alias_file_to_name(file)
"#{name}/#{file.basename}"
end

sig { params(list: Symbol, formula_or_cask: String, value: T.untyped).returns(T.untyped) }
def audit_exception(list, formula_or_cask, value = nil)
return false if audit_exceptions.blank?
return false unless audit_exceptions.key? list
Expand All @@ -893,14 +930,15 @@ def audit_exception(list, formula_or_cask, value = nil)
list.include? formula_or_cask
when Hash
return false unless list.include? formula_or_cask
return list[formula_or_cask] if value.blank?
return list[formula_or_cask] if value.nil?

list[formula_or_cask] == value
end
end

private

sig { returns(T::Boolean) }
def read_or_set_private_config
case config["private"]
when "true" then true
Expand Down Expand Up @@ -986,7 +1024,7 @@ class CoreTap < AbstractCoreTap
# @private
sig { void }
def initialize
super "Homebrew", "core"
super("Homebrew", "core")
end

sig { override.void }
Expand All @@ -996,14 +1034,24 @@ def ensure_installed!
super
end

sig { returns(String) }
sig { override.returns(T.nilable(String)) }
def remote
return super if Homebrew::EnvConfig.no_install_from_api?

Homebrew::EnvConfig.core_git_remote
end

# CoreTap never allows shallow clones (on request from GitHub).
sig {
override.params(
quiet: T::Boolean,
clone_target: T.nilable(T.any(String, Pathname)),
force_auto_update: T.nilable(T::Boolean),
custom_remote: T::Boolean,
verify: T::Boolean,
force: T::Boolean,
).void
}
def install(quiet: false, clone_target: nil, force_auto_update: nil,
custom_remote: false, verify: false, force: false)
remote = Homebrew::EnvConfig.core_git_remote # set by HOMEBREW_CORE_GIT_REMOTE
Expand All @@ -1021,15 +1069,15 @@ def install(quiet: false, clone_target: nil, force_auto_update: nil,
end

# @private
sig { params(manual: T::Boolean).void }
sig { override.params(manual: T::Boolean).void }
def uninstall(manual: false)
raise "Tap#uninstall is not available for CoreTap" if Homebrew::EnvConfig.no_install_from_api?

super
end

# @private
sig { returns(T::Boolean) }
sig { override.returns(T::Boolean) }
def core_tap?
true
end
Expand All @@ -1041,7 +1089,7 @@ def linuxbrew_core?
end

# @private
sig { returns(Pathname) }
sig { override.returns(Pathname) }
def formula_dir
@formula_dir ||= begin
ensure_installed!
Expand Down Expand Up @@ -1083,15 +1131,15 @@ def formula_renames
end

# @private
sig { returns(Hash) }
sig { override.returns(T::Hash[String, String]) }
def tap_migrations
@tap_migrations ||= if Homebrew::EnvConfig.no_install_from_api?
ensure_installed!
super
else
migrations, = Homebrew::API.fetch_json_api_file "formula_tap_migrations.jws.json",
stale_seconds: TAP_MIGRATIONS_STALE_SECONDS
migrations
T.cast(migrations, T::Hash[String, String])
end
end

Expand Down Expand Up @@ -1182,7 +1230,7 @@ class CoreCaskTap < AbstractCoreTap
# @private
sig { void }
def initialize
super "Homebrew", "cask"
super("Homebrew", "cask")
end

# @private
Expand Down Expand Up @@ -1234,14 +1282,15 @@ def cask_renames
end
end

sig { override.returns(Hash) }
# @private
sig { override.returns(T::Hash[String, String]) }
def tap_migrations
@tap_migrations ||= if Homebrew::EnvConfig.no_install_from_api?
super
else
migrations, = Homebrew::API.fetch_json_api_file "cask_tap_migrations.jws.json",
stale_seconds: TAP_MIGRATIONS_STALE_SECONDS
migrations
T.cast(migrations, T::Hash[String, String])
end
end
end
Expand Down

0 comments on commit 03278ab

Please sign in to comment.