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 Mar 6, 2024
1 parent cddf4c5 commit 32911b7
Show file tree
Hide file tree
Showing 2 changed files with 59 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
70 changes: 58 additions & 12 deletions Library/Homebrew/tap.rb
Expand Up @@ -58,6 +58,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 @@ -140,6 +141,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 @@ -150,6 +152,7 @@ def initialize(user, repo)
end

# Clear internal cache.
sig { void }
def clear_cache
@remote = nil
@repo_var_suffix = nil
Expand Down Expand Up @@ -199,6 +202,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 Down Expand Up @@ -232,25 +236,29 @@ def repo_var_suffix
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 @@ -266,16 +274,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 @@ -293,11 +304,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 @@ -323,6 +336,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 @@ -340,7 +363,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 @@ -448,6 +471,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 @@ -460,6 +484,7 @@ def link_completions_and_manpages
end
end

sig { params(requested_remote: T.nilable(T.any(String, Pathname)), quiet: T::Boolean).void }
def fix_remote_configuration(requested_remote: nil, quiet: false)
if requested_remote.present?
path.cd do
Expand Down Expand Up @@ -493,6 +518,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 @@ -574,6 +600,8 @@ def relative_cask_path(token)
.delete_prefix("#{path}/")
end

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

Expand Down Expand Up @@ -888,6 +916,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 @@ -942,6 +975,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 @@ -953,14 +987,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 @@ -1046,7 +1081,7 @@ class CoreTap < AbstractCoreTap
# @private
sig { void }
def initialize
super "Homebrew", "core"
super("Homebrew", "core")
end

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

sig { returns(T.nilable(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 @@ -1081,15 +1126,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 @@ -1101,7 +1146,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 @@ -1143,7 +1188,7 @@ 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!
Expand All @@ -1153,7 +1198,7 @@ def tap_migrations
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 @@ -1272,7 +1317,7 @@ class CoreCaskTap < AbstractCoreTap
# @private
sig { void }
def initialize
super "Homebrew", "cask"
super("Homebrew", "cask")
end

# @private
Expand Down Expand Up @@ -1324,14 +1369,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 32911b7

Please sign in to comment.