unpack_strategy/*: Convert to Sorbet typed: strict

This commit is contained in:
Issy Long 2024-07-11 19:57:12 +01:00
parent bffb470c57
commit edb8055c76
No known key found for this signature in database
33 changed files with 92 additions and 47 deletions

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,13 +11,15 @@ module UnpackStrategy
[".air"] [".air"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
mime_type = "application/vnd.adobe.air-application-installer-package+zip" mime_type = "application/vnd.adobe.air-application-installer-package+zip"
path.magic_number.match?(/.{59}#{Regexp.escape(mime_type)}/) path.magic_number.match?(/.{59}#{Regexp.escape(mime_type)}/)
end end
sig { returns(T.nilable(T::Array[Cask::Cask])) }
def dependencies def dependencies
@dependencies ||= [Cask::CaskLoader.load("adobe-air")] @dependencies ||= T.let([Cask::CaskLoader.load("adobe-air")], T.nilable(T::Array[Cask::Cask]))
end end
AIR_APPLICATION_INSTALLER = AIR_APPLICATION_INSTALLER =

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "directory" require_relative "directory"
@ -6,8 +6,9 @@ require_relative "directory"
module UnpackStrategy module UnpackStrategy
# Strategy for unpacking Bazaar archives. # Strategy for unpacking Bazaar archives.
class Bazaar < Directory class Bazaar < Directory
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
super && (path/".bzr").directory? !!(super && (path/".bzr").directory?)
end end
private private

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,6 +11,7 @@ module UnpackStrategy
[".bz2"] [".bz2"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\ABZh/n) path.magic_number.match?(/\ABZh/n)
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,6 +11,7 @@ module UnpackStrategy
[".cab"] [".cab"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\AMSCF/n) path.magic_number.match?(/\AMSCF/n)
end end
@ -23,8 +24,9 @@ module UnpackStrategy
verbose: verbose:
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["cabextract"]] @dependencies ||= T.let([Formula["cabextract"]], T.nilable(T::Array[Formula]))
end end
end end
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "tar" require_relative "tar"
@ -11,6 +11,7 @@ module UnpackStrategy
[".Z"] [".Z"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\A\037\235/n) path.magic_number.match?(/\A\037\235/n)
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "directory" require_relative "directory"
@ -6,8 +6,9 @@ require_relative "directory"
module UnpackStrategy module UnpackStrategy
# Strategy for unpacking CVS repositories. # Strategy for unpacking CVS repositories.
class Cvs < Directory class Cvs < Directory
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
super && (path/"CVS").directory? !!(super && (path/"CVS").directory?)
end end
end end
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,6 +11,7 @@ module UnpackStrategy
[] []
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.directory? path.directory?
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "uncompressed" require_relative "uncompressed"
@ -11,6 +11,7 @@ module UnpackStrategy
[".sh", ".bash"] [".sh", ".bash"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\A#!\s*\S+/n) || path.magic_number.match?(/\A#!\s*\S+/n) ||
path.magic_number.match?(/\AMZ/n) path.magic_number.match?(/\AMZ/n)

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "system_command" require "system_command"
@ -14,6 +14,7 @@ module UnpackStrategy
[] []
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
return false unless path.magic_number.match?(/\ASQLite format 3\000/n) return false unless path.magic_number.match?(/\ASQLite format 3\000/n)

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[] []
end end
sig { override.params(_path: Pathname).returns(T::Boolean) }
def self.can_extract?(_path) def self.can_extract?(_path)
false false
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["unar"]] @dependencies ||= T.let([Formula["unar"]], T.nilable(T::Array[Formula]))
end end
private private

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "directory" require_relative "directory"
@ -6,8 +6,9 @@ require_relative "directory"
module UnpackStrategy module UnpackStrategy
# Strategy for unpacking Git repositories. # Strategy for unpacking Git repositories.
class Git < Directory class Git < Directory
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
super && (path/".git").directory? !!(super && (path/".git").directory?)
end end
end end
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,6 +11,7 @@ module UnpackStrategy
[".gz"] [".gz"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\A\037\213/n) path.magic_number.match?(/\A\037\213/n)
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "uncompressed" require_relative "uncompressed"
@ -11,6 +11,7 @@ module UnpackStrategy
[".apk", ".jar"] [".apk", ".jar"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
return false unless Zip.can_extract?(path) return false unless Zip.can_extract?(path)

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[".lha", ".lzh"] [".lha", ".lzh"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\A..-(lh0|lh1|lz4|lz5|lzs|lh\\40|lhd|lh2|lh3|lh4|lh5)-/n) path.magic_number.match?(/\A..-(lh0|lh1|lz4|lz5|lzs|lh\\40|lhd|lh2|lh3|lh4|lh5)-/n)
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["lha"]] @dependencies ||= T.let([Formula["lha"]], T.nilable(T::Array[Formula]))
end end
private private

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "uncompressed" require_relative "uncompressed"
@ -11,6 +11,7 @@ module UnpackStrategy
[".rock"] [".rock"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
return false unless Zip.can_extract?(path) return false unless Zip.can_extract?(path)

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[".lz"] [".lz"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\ALZIP/n) path.magic_number.match?(/\ALZIP/n)
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["lzip"]] @dependencies ||= T.let([Formula["lzip"]], T.nilable(T::Array[Formula]))
end end
private private

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[".lzma"] [".lzma"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\A\]\000\000\200\000/n) path.magic_number.match?(/\A\]\000\000\200\000/n)
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["xz"]] @dependencies ||= T.let([Formula["xz"]], T.nilable(T::Array[Formula]))
end end
private private

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "directory" require_relative "directory"
@ -6,12 +6,14 @@ require_relative "directory"
module UnpackStrategy module UnpackStrategy
# Strategy for unpacking Mercurial repositories. # Strategy for unpacking Mercurial repositories.
class Mercurial < Directory class Mercurial < Directory
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
super && (path/".hg").directory? !!(super && (path/".hg").directory?)
end end
private private
sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).void }
def extract_to_dir(unpack_dir, basename:, verbose:) def extract_to_dir(unpack_dir, basename:, verbose:)
system_command! "hg", system_command! "hg",
args: ["--cwd", path, "archive", "--subrepos", "-y", "-t", "files", unpack_dir], args: ["--cwd", path, "archive", "--subrepos", "-y", "-t", "files", unpack_dir],

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "uncompressed" require_relative "uncompressed"
@ -15,6 +15,7 @@ module UnpackStrategy
] ]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
return false unless Zip.can_extract?(path) return false unless Zip.can_extract?(path)

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "uncompressed" require_relative "uncompressed"
@ -11,6 +11,7 @@ module UnpackStrategy
[".otf"] [".otf"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\AOTTO/n) path.magic_number.match?(/\AOTTO/n)
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[".7z"] [".7z"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\A7z\xBC\xAF\x27\x1C/n) path.magic_number.match?(/\A7z\xBC\xAF\x27\x1C/n)
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["p7zip"]] @dependencies ||= T.let([Formula["p7zip"]], T.nilable(T::Array[Formula]))
end end
private private

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,6 +11,7 @@ module UnpackStrategy
[".pax"] [".pax"]
end end
sig { override.params(_path: Pathname).returns(T::Boolean) }
def self.can_extract?(_path) def self.can_extract?(_path)
false false
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "uncompressed" require_relative "uncompressed"
@ -11,6 +11,7 @@ module UnpackStrategy
[".pkg", ".mkpg"] [".pkg", ".mkpg"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.extname.match?(/\A.m?pkg\Z/) && path.extname.match?(/\A.m?pkg\Z/) &&
(path.directory? || path.magic_number.match?(/\Axar!/n)) (path.directory? || path.magic_number.match?(/\Axar!/n))

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[".rar"] [".rar"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\ARar!/n) path.magic_number.match?(/\ARar!/n)
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["libarchive"]] @dependencies ||= T.let([Formula["libarchive"]], T.nilable(T::Array[Formula]))
end end
private private

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "generic_unar" require_relative "generic_unar"
@ -11,6 +11,7 @@ module UnpackStrategy
[] []
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\AMZ/n) && path.magic_number.match?(/\AMZ/n) &&
path.file_type.include?("self-extracting archive") path.file_type.include?("self-extracting archive")

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "generic_unar" require_relative "generic_unar"
@ -11,6 +11,7 @@ module UnpackStrategy
[".sit"] [".sit"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\AStuffIt/n) path.magic_number.match?(/\AStuffIt/n)
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "directory" require_relative "directory"
@ -6,12 +6,14 @@ require_relative "directory"
module UnpackStrategy module UnpackStrategy
# Strategy for unpacking Subversion repositories. # Strategy for unpacking Subversion repositories.
class Subversion < Directory class Subversion < Directory
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
super && (path/".svn").directory? !!(super && (path/".svn").directory?)
end end
private private
sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).void }
def extract_to_dir(unpack_dir, basename:, verbose:) def extract_to_dir(unpack_dir, basename:, verbose:)
system_command! "svn", system_command! "svn",
args: ["export", "--force", ".", unpack_dir], args: ["export", "--force", ".", unpack_dir],

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "system_command" require "system_command"
@ -22,6 +22,7 @@ module UnpackStrategy
] ]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
return true if path.magic_number.match?(/\A.{257}ustar/n) return true if path.magic_number.match?(/\A.{257}ustar/n)

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require_relative "uncompressed" require_relative "uncompressed"
@ -11,6 +11,7 @@ module UnpackStrategy
[".ttc", ".ttf"] [".ttc", ".ttf"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
# TrueType Font # TrueType Font
path.magic_number.match?(/\A\000\001\000\000\000/n) || path.magic_number.match?(/\A\000\001\000\000\000/n) ||

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,6 +11,7 @@ module UnpackStrategy
[".xar"] [".xar"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\Axar!/n) path.magic_number.match?(/\Axar!/n)
end end

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[".xz"] [".xz"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\A\xFD7zXZ\x00/n) path.magic_number.match?(/\A\xFD7zXZ\x00/n)
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["xz"]] @dependencies ||= T.let([Formula["xz"]], T.nilable(T::Array[Formula]))
end end
private private

View File

@ -23,6 +23,7 @@ module UnpackStrategy
.returns(SystemCommand::Result) .returns(SystemCommand::Result)
} }
def extract_to_dir(unpack_dir, basename:, verbose:) def extract_to_dir(unpack_dir, basename:, verbose:)
odebug "in unpack_strategy, zip, extract_to_dir, verbose: #{verbose.inspect}"
unzip = if which("unzip").blank? unzip = if which("unzip").blank?
begin begin
Formula["unzip"] Formula["unzip"]

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module UnpackStrategy module UnpackStrategy
@ -11,12 +11,14 @@ module UnpackStrategy
[".zst"] [".zst"]
end end
sig { override.params(path: Pathname).returns(T::Boolean) }
def self.can_extract?(path) def self.can_extract?(path)
path.magic_number.match?(/\x28\xB5\x2F\xFD/n) path.magic_number.match?(/\x28\xB5\x2F\xFD/n)
end end
sig { returns(T.nilable(T::Array[Formula])) }
def dependencies def dependencies
@dependencies ||= [Formula["zstd"]] @dependencies ||= T.let([Formula["zstd"]], T.nilable(T::Array[Formula]))
end end
private private