Skip to content

Commit

Permalink
Merge pull request #17606 from Homebrew/sorbet-strict-devcmd
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMcQuaid committed Jul 4, 2024
2 parents 5e7765c + 4b83521 commit 3773940
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 84 deletions.
4 changes: 3 additions & 1 deletion Library/Homebrew/dev-cmd/audit.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "abstract_command"
Expand Down Expand Up @@ -329,6 +329,7 @@ def run

private

sig { params(results: T::Hash[[Symbol, Pathname], T::Array[T::Hash[Symbol, T.untyped]]]).void }
def print_problems(results)
results.each do |(name, path), problems|
problem_lines = format_problem_lines(problems)
Expand All @@ -343,6 +344,7 @@ def print_problems(results)
end
end

sig { params(problems: T::Array[T::Hash[Symbol, T.untyped]]).returns(T::Array[String]) }
def format_problem_lines(problems)
problems.map do |problem|
status = " #{Formatter.success("[corrected]")}" if problem.fetch(:corrected)
Expand Down
46 changes: 37 additions & 9 deletions Library/Homebrew/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "abstract_command"
Expand All @@ -20,7 +20,7 @@ module DevCmd
class Bottle < AbstractCommand
include FileUtils

BOTTLE_ERB = <<-EOS.freeze
BOTTLE_ERB = T.let(<<-EOS.freeze, String)
bottle do
<% if [HOMEBREW_BOTTLE_DEFAULT_DOMAIN.to_s,
"#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}/bottles"].exclude?(root_url) %>
Expand All @@ -39,9 +39,9 @@ class Bottle < AbstractCommand

MAXIMUM_STRING_MATCHES = 100

ALLOWABLE_HOMEBREW_REPOSITORY_LINKS = [
ALLOWABLE_HOMEBREW_REPOSITORY_LINKS = T.let([
%r{#{Regexp.escape(HOMEBREW_LIBRARY)}/Homebrew/os/(mac|linux)/pkgconfig},
].freeze
].freeze, T::Array[Regexp])

cmd_args do
description <<~EOS
Expand Down Expand Up @@ -110,6 +110,10 @@ def run
end
end

sig {
params(tag: Symbol, digest: T.any(Checksum, String), cellar: T.nilable(T.any(String, Symbol)),
tag_column: Integer, digest_column: Integer).returns(String)
}
def generate_sha256_line(tag, digest, cellar, tag_column, digest_column)
line = "sha256 "
tag_column += line.length
Expand All @@ -125,6 +129,7 @@ def generate_sha256_line(tag, digest, cellar, tag_column, digest_column)
%Q(#{line}"#{digest}")
end

sig { params(bottle: BottleSpecification, root_url_using: T.nilable(String)).returns(String) }
def bottle_output(bottle, root_url_using)
cellars = bottle.checksums.filter_map do |checksum|
cellar = checksum["cellar"]
Expand Down Expand Up @@ -153,12 +158,14 @@ def bottle_output(bottle, root_url_using)
erb.result(erb_binding).gsub(/^\s*$\n/, "")
end

sig { params(filenames: T::Array[String]).returns(T::Array[T::Hash[String, T.untyped]]) }
def parse_json_files(filenames)
filenames.map do |filename|
JSON.parse(File.read(filename))
end
end

sig { params(json_files: T::Array[T::Hash[String, T.untyped]]).returns(T::Hash[String, T.untyped]) }
def merge_json_files(json_files)
json_files.reduce({}) do |hash, json_file|
json_file.each_value do |json_hash|
Expand All @@ -172,6 +179,10 @@ def merge_json_files(json_files)
end
end

sig {
params(old_keys: T::Array[String], old_bottle_spec: BottleSpecification,
new_bottle_hash: T::Hash[String, T.untyped]).returns(T::Array[T::Array[String]])
}
def merge_bottle_spec(old_keys, old_bottle_spec, new_bottle_hash)
mismatches = []
checksums = []
Expand Down Expand Up @@ -214,16 +225,20 @@ def merge_bottle_spec(old_keys, old_bottle_spec, new_bottle_hash)

private

sig {
params(string: String, keg: Keg, ignores: T::Array[String],
formula_and_runtime_deps_names: T.nilable(T::Array[String])).returns(T::Boolean)
}
def keg_contain?(string, keg, ignores, formula_and_runtime_deps_names = nil)
@put_string_exists_header, @put_filenames = nil

print_filename = lambda do |str, filename|
unless @put_string_exists_header
opoo "String '#{str}' still exists in these files:"
@put_string_exists_header = true
@put_string_exists_header = T.let(true, T.nilable(T::Boolean))
end

@put_filenames ||= []
@put_filenames ||= T.let([], T.nilable(T::Array[T.any(String, Pathname)]))

return false if @put_filenames.include?(filename)

Expand Down Expand Up @@ -265,6 +280,7 @@ def keg_contain?(string, keg, ignores, formula_and_runtime_deps_names = nil)
keg_contain_absolute_symlink_starting_with?(string, keg) || result
end

sig { params(string: String, keg: Keg).returns(T::Boolean) }
def keg_contain_absolute_symlink_starting_with?(string, keg)
absolute_symlinks_start_with_string = []
keg.find do |pn|
Expand All @@ -283,6 +299,7 @@ def keg_contain_absolute_symlink_starting_with?(string, keg)
!absolute_symlinks_start_with_string.empty?
end

sig { params(cellar: T.nilable(T.any(String, Symbol))).returns(T::Boolean) }
def cellar_parameter_needed?(cellar)
default_cellars = [
Homebrew::DEFAULT_MACOS_CELLAR,
Expand All @@ -292,6 +309,7 @@ def cellar_parameter_needed?(cellar)
cellar.present? && default_cellars.exclude?(cellar)
end

sig { returns(T.nilable(T::Boolean)) }
def sudo_purge
return unless ENV["HOMEBREW_BOTTLE_SUDO_PURGE"]

Expand Down Expand Up @@ -354,6 +372,7 @@ def setup_tar_and_args!(mtime)
[gnu_tar(gnu_tar_formula), reproducible_gnutar_args(mtime)].freeze
end

sig { params(formula: T.untyped).returns(T::Array[T.untyped]) }
def formula_ignores(formula)
ignores = []
cellar_regex = Regexp.escape(HOMEBREW_CELLAR)
Expand Down Expand Up @@ -384,6 +403,7 @@ def formula_ignores(formula)
ignores.compact
end

sig { params(formula: Formula).void }
def bottle_formula(formula)
local_bottle_json = args.json? && formula.local_bottle_path.present?

Expand Down Expand Up @@ -453,6 +473,8 @@ def bottle_formula(formula)

if local_bottle_json
bottle_path = formula.local_bottle_path
return if bottle_path.blank?

local_filename = bottle_path.basename.to_s

tab_path = Utils::Bottles.receipt_path(bottle_path)
Expand All @@ -471,6 +493,7 @@ def bottle_formula(formula)
else
tar_filename = filename.to_s.sub(/.gz$/, "")
tar_path = Pathname.pwd/tar_filename
return if tar_path.blank?

keg = Keg.new(formula.prefix)
end
Expand Down Expand Up @@ -681,6 +704,7 @@ def bottle_formula(formula)
json_path.write(JSON.pretty_generate(json))
end

sig { returns(T::Hash[String, T.untyped]) }
def merge
bottles_hash = merge_json_files(parse_json_files(args.named))

Expand Down Expand Up @@ -750,7 +774,7 @@ def merge
end
end

all_bottle_hash = T.let(nil, T.nilable(Hash))
all_bottle_hash = T.let(nil, T.nilable(T::Hash[String, T.untyped]))
bottle_hash["bottle"]["tags"].each do |tag, tag_hash|
filename = ::Bottle::Filename.new(
formula_name,
Expand Down Expand Up @@ -801,7 +825,7 @@ def merge
checksums = old_checksums(formula, formula_ast, bottle_hash)
update_or_add = checksums.nil? ? "add" : "update"

checksums&.each(&bottle.method(:sha256))
checksums&.each { |checksum| bottle.sha256(checksum) }
output = bottle_output(bottle, args.root_url_using)
puts output

Expand Down Expand Up @@ -835,8 +859,12 @@ def merge
end
end

sig {
params(formula: Formula, formula_ast: Utils::AST::FormulaAST,
bottle_hash: T::Hash[String, T.untyped]).returns(T.nilable(T::Array[String]))
}
def old_checksums(formula, formula_ast, bottle_hash)
bottle_node = formula_ast.bottle_block
bottle_node = T.cast(formula_ast.bottle_block, T.nilable(RuboCop::AST::BlockNode))
return if bottle_node.nil?
return [] unless args.keep_old?

Expand Down
Loading

0 comments on commit 3773940

Please sign in to comment.