From 4ebf1116d72deafad63c85cd94c55293f036e253 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 5 Jul 2024 22:29:41 -0700 Subject: [PATCH] Introduce UnpackStrategyImpl --- Library/Homebrew/unpack_strategy.rb | 63 +++++++------------ Library/Homebrew/unpack_strategy/air.rb | 2 +- Library/Homebrew/unpack_strategy/bzip2.rb | 2 +- Library/Homebrew/unpack_strategy/cab.rb | 2 +- Library/Homebrew/unpack_strategy/compress.rb | 2 +- Library/Homebrew/unpack_strategy/directory.rb | 2 +- Library/Homebrew/unpack_strategy/dmg.rb | 8 ++- .../Homebrew/unpack_strategy/executable.rb | 2 +- Library/Homebrew/unpack_strategy/fossil.rb | 2 +- .../Homebrew/unpack_strategy/generic_unar.rb | 2 +- Library/Homebrew/unpack_strategy/gzip.rb | 2 +- Library/Homebrew/unpack_strategy/jar.rb | 2 +- Library/Homebrew/unpack_strategy/lha.rb | 2 +- Library/Homebrew/unpack_strategy/lua_rock.rb | 2 +- Library/Homebrew/unpack_strategy/lzip.rb | 2 +- Library/Homebrew/unpack_strategy/lzma.rb | 2 +- .../unpack_strategy/microsoft_office_xml.rb | 2 +- Library/Homebrew/unpack_strategy/otf.rb | 2 +- Library/Homebrew/unpack_strategy/p7zip.rb | 2 +- Library/Homebrew/unpack_strategy/pax.rb | 2 +- Library/Homebrew/unpack_strategy/pkg.rb | 2 +- Library/Homebrew/unpack_strategy/rar.rb | 2 +- .../self_extracting_executable.rb | 2 +- Library/Homebrew/unpack_strategy/sit.rb | 2 +- Library/Homebrew/unpack_strategy/tar.rb | 2 +- Library/Homebrew/unpack_strategy/ttf.rb | 2 +- .../Homebrew/unpack_strategy/uncompressed.rb | 6 ++ Library/Homebrew/unpack_strategy/xar.rb | 2 +- Library/Homebrew/unpack_strategy/xz.rb | 2 +- Library/Homebrew/unpack_strategy/zip.rb | 4 +- Library/Homebrew/unpack_strategy/zstd.rb | 2 +- 31 files changed, 63 insertions(+), 72 deletions(-) diff --git a/Library/Homebrew/unpack_strategy.rb b/Library/Homebrew/unpack_strategy.rb index a56e76ca58..843a257e2e 100644 --- a/Library/Homebrew/unpack_strategy.rb +++ b/Library/Homebrew/unpack_strategy.rb @@ -6,47 +6,27 @@ require "system_command" # Module containing all available strategies for unpacking archives. module UnpackStrategy extend T::Helpers + include SystemCommand::Mixin + abstract! - # FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed. - AnyStrategy = T.type_alias do # rubocop:disable Style/MutableConstant - T.any( - T.class_of(UnpackStrategy::Tar), - T.class_of(UnpackStrategy::Pax), - T.class_of(UnpackStrategy::Gzip), - T.class_of(UnpackStrategy::Dmg), - T.class_of(UnpackStrategy::Lzma), - T.class_of(UnpackStrategy::Xz), - T.class_of(UnpackStrategy::Zstd), - T.class_of(UnpackStrategy::Lzip), - T.class_of(UnpackStrategy::Air), - T.class_of(UnpackStrategy::Jar), - T.class_of(UnpackStrategy::LuaRock), - T.class_of(UnpackStrategy::MicrosoftOfficeXml), - T.class_of(UnpackStrategy::Zip), - T.class_of(UnpackStrategy::Pkg), - T.class_of(UnpackStrategy::Xar), - T.class_of(UnpackStrategy::Ttf), - T.class_of(UnpackStrategy::Otf), - T.class_of(UnpackStrategy::Git), - T.class_of(UnpackStrategy::Mercurial), - T.class_of(UnpackStrategy::Subversion), - T.class_of(UnpackStrategy::Cvs), - T.class_of(UnpackStrategy::SelfExtractingExecutable), - T.class_of(UnpackStrategy::Cab), - T.class_of(UnpackStrategy::Executable), - T.class_of(UnpackStrategy::Bzip2), - T.class_of(UnpackStrategy::Fossil), - T.class_of(UnpackStrategy::Bazaar), - T.class_of(UnpackStrategy::P7Zip), - T.class_of(UnpackStrategy::Sit), - T.class_of(UnpackStrategy::Rar), - T.class_of(UnpackStrategy::Lha), - ) + # rubocop:disable Style/MutableConstant + UnpackStrategyImpl = T.type_alias { T.all(T::Class[UnpackStrategy], UnpackStrategy::ClassMethods) } + # rubocop:enable Style/MutableConstant + + module ClassMethods + extend T::Helpers + abstract! + + sig { abstract.returns(T::Array[String]) } + def extensions; end + + sig { abstract.params(path: Pathname).returns(T::Boolean) } + def can_extract?(path); end end - include SystemCommand::Mixin + mixes_in_class_methods(ClassMethods) - sig { returns(T.nilable(T::Array[AnyStrategy])) } + sig { returns(T.nilable(T::Array[UnpackStrategyImpl])) } def self.strategies @strategies ||= T.let([ Tar, # Needs to be before Bzip2/Gzip/Xz/Lzma/Zstd. @@ -81,11 +61,11 @@ module UnpackStrategy Sit, Rar, Lha, - ].freeze, T.nilable(T::Array[AnyStrategy])) + ].freeze, T.nilable(T::Array[UnpackStrategyImpl])) end private_class_method :strategies - sig { params(type: Symbol).returns(T.nilable(T.any(T.class_of(UnpackStrategy::Uncompressed), AnyStrategy))) } + sig { params(type: Symbol).returns(T.nilable(UnpackStrategyImpl)) } def self.from_type(type) type = { naked: :uncompressed, @@ -100,7 +80,7 @@ module UnpackStrategy end end - sig { params(extension: String).returns(T.nilable(AnyStrategy)) } + sig { params(extension: String).returns(T.nilable(UnpackStrategyImpl)) } def self.from_extension(extension) return unless strategies @@ -109,7 +89,7 @@ module UnpackStrategy &.find { |s| s.extensions.any? { |ext| extension.end_with?(ext) } } end - sig { params(path: Pathname).returns(T.nilable(AnyStrategy)) } + sig { params(path: Pathname).returns(T.nilable(UnpackStrategyImpl)) } def self.from_magic(path) strategies&.find { |s| s.can_extract?(path) } end @@ -152,7 +132,6 @@ module UnpackStrategy @merge_xattrs = merge_xattrs end - abstract! sig { abstract.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).void } def extract_to_dir(unpack_dir, basename:, verbose:); end private :extract_to_dir diff --git a/Library/Homebrew/unpack_strategy/air.rb b/Library/Homebrew/unpack_strategy/air.rb index c4a6ba601a..26a3c37a1a 100644 --- a/Library/Homebrew/unpack_strategy/air.rb +++ b/Library/Homebrew/unpack_strategy/air.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Air include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".air"] end diff --git a/Library/Homebrew/unpack_strategy/bzip2.rb b/Library/Homebrew/unpack_strategy/bzip2.rb index af1248c466..8eca20c997 100644 --- a/Library/Homebrew/unpack_strategy/bzip2.rb +++ b/Library/Homebrew/unpack_strategy/bzip2.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Bzip2 include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".bz2"] end diff --git a/Library/Homebrew/unpack_strategy/cab.rb b/Library/Homebrew/unpack_strategy/cab.rb index af73d82fdd..26036963a0 100644 --- a/Library/Homebrew/unpack_strategy/cab.rb +++ b/Library/Homebrew/unpack_strategy/cab.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Cab include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".cab"] end diff --git a/Library/Homebrew/unpack_strategy/compress.rb b/Library/Homebrew/unpack_strategy/compress.rb index 0082312214..e2d6ce37f8 100644 --- a/Library/Homebrew/unpack_strategy/compress.rb +++ b/Library/Homebrew/unpack_strategy/compress.rb @@ -6,7 +6,7 @@ require_relative "tar" module UnpackStrategy # Strategy for unpacking compress archives. class Compress < Tar - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".Z"] end diff --git a/Library/Homebrew/unpack_strategy/directory.rb b/Library/Homebrew/unpack_strategy/directory.rb index 90b5ade18e..4df5b09140 100644 --- a/Library/Homebrew/unpack_strategy/directory.rb +++ b/Library/Homebrew/unpack_strategy/directory.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Directory include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [] end diff --git a/Library/Homebrew/unpack_strategy/dmg.rb b/Library/Homebrew/unpack_strategy/dmg.rb index 92c7f8ca49..52036cf8e3 100644 --- a/Library/Homebrew/unpack_strategy/dmg.rb +++ b/Library/Homebrew/unpack_strategy/dmg.rb @@ -121,6 +121,12 @@ module UnpackStrategy end end + sig { override.returns(T::Array[String]) } + def self.extensions = [] + + sig { override.params(_path: Pathname).returns(T::Boolean) } + def self.can_extract?(_path) = false + private sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).returns(T.untyped) } @@ -165,7 +171,7 @@ module UnpackStrategy end private_constant :Mount - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".dmg"] end diff --git a/Library/Homebrew/unpack_strategy/executable.rb b/Library/Homebrew/unpack_strategy/executable.rb index 4c5e712da8..86c381ef13 100644 --- a/Library/Homebrew/unpack_strategy/executable.rb +++ b/Library/Homebrew/unpack_strategy/executable.rb @@ -6,7 +6,7 @@ require_relative "uncompressed" module UnpackStrategy # Strategy for unpacking executables. class Executable < Uncompressed - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".sh", ".bash"] end diff --git a/Library/Homebrew/unpack_strategy/fossil.rb b/Library/Homebrew/unpack_strategy/fossil.rb index 1e2dcc00a8..9cf9baf378 100644 --- a/Library/Homebrew/unpack_strategy/fossil.rb +++ b/Library/Homebrew/unpack_strategy/fossil.rb @@ -9,7 +9,7 @@ module UnpackStrategy include UnpackStrategy extend SystemCommand::Mixin - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [] end diff --git a/Library/Homebrew/unpack_strategy/generic_unar.rb b/Library/Homebrew/unpack_strategy/generic_unar.rb index 0aee0ce3ac..eb8f50e2b0 100644 --- a/Library/Homebrew/unpack_strategy/generic_unar.rb +++ b/Library/Homebrew/unpack_strategy/generic_unar.rb @@ -6,7 +6,7 @@ module UnpackStrategy class GenericUnar include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [] end diff --git a/Library/Homebrew/unpack_strategy/gzip.rb b/Library/Homebrew/unpack_strategy/gzip.rb index 2ece0befdc..23801d70ef 100644 --- a/Library/Homebrew/unpack_strategy/gzip.rb +++ b/Library/Homebrew/unpack_strategy/gzip.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Gzip include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".gz"] end diff --git a/Library/Homebrew/unpack_strategy/jar.rb b/Library/Homebrew/unpack_strategy/jar.rb index 892357b48b..8082d8602c 100644 --- a/Library/Homebrew/unpack_strategy/jar.rb +++ b/Library/Homebrew/unpack_strategy/jar.rb @@ -6,7 +6,7 @@ require_relative "uncompressed" module UnpackStrategy # Strategy for unpacking Java archives. class Jar < Uncompressed - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".apk", ".jar"] end diff --git a/Library/Homebrew/unpack_strategy/lha.rb b/Library/Homebrew/unpack_strategy/lha.rb index 1da3021ad7..c161dff98e 100644 --- a/Library/Homebrew/unpack_strategy/lha.rb +++ b/Library/Homebrew/unpack_strategy/lha.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Lha include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".lha", ".lzh"] end diff --git a/Library/Homebrew/unpack_strategy/lua_rock.rb b/Library/Homebrew/unpack_strategy/lua_rock.rb index a01fcd2f88..b054b9f810 100644 --- a/Library/Homebrew/unpack_strategy/lua_rock.rb +++ b/Library/Homebrew/unpack_strategy/lua_rock.rb @@ -6,7 +6,7 @@ require_relative "uncompressed" module UnpackStrategy # Strategy for unpacking LuaRock archives. class LuaRock < Uncompressed - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".rock"] end diff --git a/Library/Homebrew/unpack_strategy/lzip.rb b/Library/Homebrew/unpack_strategy/lzip.rb index 668aa4fdcf..87fe289fb8 100644 --- a/Library/Homebrew/unpack_strategy/lzip.rb +++ b/Library/Homebrew/unpack_strategy/lzip.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Lzip include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".lz"] end diff --git a/Library/Homebrew/unpack_strategy/lzma.rb b/Library/Homebrew/unpack_strategy/lzma.rb index d529e2de4c..f6de6e0b24 100644 --- a/Library/Homebrew/unpack_strategy/lzma.rb +++ b/Library/Homebrew/unpack_strategy/lzma.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Lzma include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".lzma"] end diff --git a/Library/Homebrew/unpack_strategy/microsoft_office_xml.rb b/Library/Homebrew/unpack_strategy/microsoft_office_xml.rb index 2ad2a40644..dfc4c084f4 100644 --- a/Library/Homebrew/unpack_strategy/microsoft_office_xml.rb +++ b/Library/Homebrew/unpack_strategy/microsoft_office_xml.rb @@ -6,7 +6,7 @@ require_relative "uncompressed" module UnpackStrategy # Strategy for unpacking Microsoft Office documents. class MicrosoftOfficeXml < Uncompressed - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [ ".doc", ".docx", diff --git a/Library/Homebrew/unpack_strategy/otf.rb b/Library/Homebrew/unpack_strategy/otf.rb index 7a75f78c4c..b4839b2a61 100644 --- a/Library/Homebrew/unpack_strategy/otf.rb +++ b/Library/Homebrew/unpack_strategy/otf.rb @@ -6,7 +6,7 @@ require_relative "uncompressed" module UnpackStrategy # Strategy for unpacking OpenType fonts. class Otf < Uncompressed - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".otf"] end diff --git a/Library/Homebrew/unpack_strategy/p7zip.rb b/Library/Homebrew/unpack_strategy/p7zip.rb index 19e0154491..063062d02f 100644 --- a/Library/Homebrew/unpack_strategy/p7zip.rb +++ b/Library/Homebrew/unpack_strategy/p7zip.rb @@ -6,7 +6,7 @@ module UnpackStrategy class P7Zip include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".7z"] end diff --git a/Library/Homebrew/unpack_strategy/pax.rb b/Library/Homebrew/unpack_strategy/pax.rb index 9a47b9d1d5..6d77f990c1 100644 --- a/Library/Homebrew/unpack_strategy/pax.rb +++ b/Library/Homebrew/unpack_strategy/pax.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Pax include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".pax"] end diff --git a/Library/Homebrew/unpack_strategy/pkg.rb b/Library/Homebrew/unpack_strategy/pkg.rb index b48a55244b..e4b288b827 100644 --- a/Library/Homebrew/unpack_strategy/pkg.rb +++ b/Library/Homebrew/unpack_strategy/pkg.rb @@ -6,7 +6,7 @@ require_relative "uncompressed" module UnpackStrategy # Strategy for unpacking macOS package installers. class Pkg < Uncompressed - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".pkg", ".mkpg"] end diff --git a/Library/Homebrew/unpack_strategy/rar.rb b/Library/Homebrew/unpack_strategy/rar.rb index 289da9134c..041bcc0798 100644 --- a/Library/Homebrew/unpack_strategy/rar.rb +++ b/Library/Homebrew/unpack_strategy/rar.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Rar include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".rar"] end diff --git a/Library/Homebrew/unpack_strategy/self_extracting_executable.rb b/Library/Homebrew/unpack_strategy/self_extracting_executable.rb index b32cfa825c..078ce77365 100644 --- a/Library/Homebrew/unpack_strategy/self_extracting_executable.rb +++ b/Library/Homebrew/unpack_strategy/self_extracting_executable.rb @@ -6,7 +6,7 @@ require_relative "generic_unar" module UnpackStrategy # Strategy for unpacking self-extracting executables. class SelfExtractingExecutable < GenericUnar - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [] end diff --git a/Library/Homebrew/unpack_strategy/sit.rb b/Library/Homebrew/unpack_strategy/sit.rb index 7d447eaf69..631c446297 100644 --- a/Library/Homebrew/unpack_strategy/sit.rb +++ b/Library/Homebrew/unpack_strategy/sit.rb @@ -6,7 +6,7 @@ require_relative "generic_unar" module UnpackStrategy # Strategy for unpacking Stuffit archives. class Sit < GenericUnar - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".sit"] end diff --git a/Library/Homebrew/unpack_strategy/tar.rb b/Library/Homebrew/unpack_strategy/tar.rb index c3300e7155..5541f96891 100644 --- a/Library/Homebrew/unpack_strategy/tar.rb +++ b/Library/Homebrew/unpack_strategy/tar.rb @@ -9,7 +9,7 @@ module UnpackStrategy include UnpackStrategy extend SystemCommand::Mixin - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [ ".tar", diff --git a/Library/Homebrew/unpack_strategy/ttf.rb b/Library/Homebrew/unpack_strategy/ttf.rb index d741378d29..3c73a6801e 100644 --- a/Library/Homebrew/unpack_strategy/ttf.rb +++ b/Library/Homebrew/unpack_strategy/ttf.rb @@ -6,7 +6,7 @@ require_relative "uncompressed" module UnpackStrategy # Strategy for unpacking TrueType fonts. class Ttf < Uncompressed - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".ttc", ".ttf"] end diff --git a/Library/Homebrew/unpack_strategy/uncompressed.rb b/Library/Homebrew/unpack_strategy/uncompressed.rb index 5d2c3dd511..25c26652c9 100644 --- a/Library/Homebrew/unpack_strategy/uncompressed.rb +++ b/Library/Homebrew/unpack_strategy/uncompressed.rb @@ -6,6 +6,12 @@ module UnpackStrategy class Uncompressed include UnpackStrategy + sig { override.returns(T::Array[String]) } + def self.extensions = [] + + sig { override.params(_path: Pathname).returns(T::Boolean) } + def self.can_extract?(_path) = false + sig { params( to: T.nilable(Pathname), diff --git a/Library/Homebrew/unpack_strategy/xar.rb b/Library/Homebrew/unpack_strategy/xar.rb index f473d35c79..fe5de4e4be 100644 --- a/Library/Homebrew/unpack_strategy/xar.rb +++ b/Library/Homebrew/unpack_strategy/xar.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Xar include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".xar"] end diff --git a/Library/Homebrew/unpack_strategy/xz.rb b/Library/Homebrew/unpack_strategy/xz.rb index 7ac0ceb4e6..2360aa5fe1 100644 --- a/Library/Homebrew/unpack_strategy/xz.rb +++ b/Library/Homebrew/unpack_strategy/xz.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Xz include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".xz"] end diff --git a/Library/Homebrew/unpack_strategy/zip.rb b/Library/Homebrew/unpack_strategy/zip.rb index dc4d6d350b..59da683f35 100644 --- a/Library/Homebrew/unpack_strategy/zip.rb +++ b/Library/Homebrew/unpack_strategy/zip.rb @@ -6,12 +6,12 @@ module UnpackStrategy class Zip include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".zip"] end - sig { params(path: Pathname).returns(T::Boolean) } + sig { override.params(path: Pathname).returns(T::Boolean) } def self.can_extract?(path) path.magic_number.match?(/\APK(\003\004|\005\006)/n) end diff --git a/Library/Homebrew/unpack_strategy/zstd.rb b/Library/Homebrew/unpack_strategy/zstd.rb index f8cdacb0ab..ee10cd85f4 100644 --- a/Library/Homebrew/unpack_strategy/zstd.rb +++ b/Library/Homebrew/unpack_strategy/zstd.rb @@ -6,7 +6,7 @@ module UnpackStrategy class Zstd include UnpackStrategy - sig { returns(T::Array[String]) } + sig { override.returns(T::Array[String]) } def self.extensions [".zst"] end