Add type signatures for UnpackStrategy::Zip
.
This commit is contained in:
parent
a582b6e371
commit
ab33bc638d
@ -279,6 +279,7 @@ Sorbet/FalseSigil:
|
|||||||
- 'Homebrew/test/**/Casks/**/*.rb'
|
- 'Homebrew/test/**/Casks/**/*.rb'
|
||||||
|
|
||||||
Sorbet/StrictSigil:
|
Sorbet/StrictSigil:
|
||||||
|
Enabled: true
|
||||||
Include:
|
Include:
|
||||||
- '**/*.rbi'
|
- '**/*.rbi'
|
||||||
|
|
||||||
|
@ -1,9 +1,19 @@
|
|||||||
# typed: false
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "system_command"
|
||||||
|
|
||||||
module UnpackStrategy
|
module UnpackStrategy
|
||||||
class Zip
|
class Zip
|
||||||
module MacOSZipExtension
|
module MacOSZipExtension
|
||||||
|
extend T::Sig
|
||||||
|
|
||||||
|
include UnpackStrategy
|
||||||
|
include SystemCommand::Mixin
|
||||||
|
|
||||||
|
using Magic
|
||||||
|
|
||||||
|
sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).returns(T.untyped) }
|
||||||
def extract_to_dir(unpack_dir, basename:, verbose:)
|
def extract_to_dir(unpack_dir, basename:, verbose:)
|
||||||
if merge_xattrs && contains_extended_attributes?(path)
|
if merge_xattrs && contains_extended_attributes?(path)
|
||||||
# We use ditto directly, because dot_clean has issues if the __MACOSX
|
# We use ditto directly, because dot_clean has issues if the __MACOSX
|
||||||
@ -16,7 +26,7 @@ module UnpackStrategy
|
|||||||
end
|
end
|
||||||
|
|
||||||
result = begin
|
result = begin
|
||||||
super
|
T.let(super, SystemCommand::Result)
|
||||||
rescue ErrorDuringExecution => e
|
rescue ErrorDuringExecution => e
|
||||||
raise unless e.stderr.include?("End-of-central-directory signature not found.")
|
raise unless e.stderr.include?("End-of-central-directory signature not found.")
|
||||||
|
|
||||||
@ -47,6 +57,13 @@ module UnpackStrategy
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
sig { params(path: Pathname).returns(T::Boolean) }
|
||||||
|
def contains_extended_attributes?(path)
|
||||||
|
path.zipinfo.grep(/(^__MACOSX|\._)/).any?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
private_constant :MacOSZipExtension
|
private_constant :MacOSZipExtension
|
||||||
|
|
||||||
|
9
Library/Homebrew/extend/os/mac/unpack_strategy/zip.rbi
Normal file
9
Library/Homebrew/extend/os/mac/unpack_strategy/zip.rbi
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# typed: strict
|
||||||
|
|
||||||
|
module UnpackStrategy
|
||||||
|
class Zip
|
||||||
|
module MacOSZipExtension
|
||||||
|
include Kernel
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -6,3 +6,6 @@
|
|||||||
|
|
||||||
--ignore
|
--ignore
|
||||||
/test/.gem
|
/test/.gem
|
||||||
|
|
||||||
|
--dsl-plugins
|
||||||
|
sorbet/triggers.yml
|
||||||
|
21
Library/Homebrew/sorbet/plugins/unpack_strategy_magic.rb
Normal file
21
Library/Homebrew/sorbet/plugins/unpack_strategy_magic.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# typed: strict
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
source = ARGV[5]
|
||||||
|
|
||||||
|
/\busing +Magic\b/.match(source) do |_|
|
||||||
|
puts <<-RUBY
|
||||||
|
# typed: strict
|
||||||
|
|
||||||
|
class ::Pathname
|
||||||
|
sig { returns(String) }
|
||||||
|
def magic_number; end
|
||||||
|
|
||||||
|
sig { returns(String) }
|
||||||
|
def file_type; end
|
||||||
|
|
||||||
|
sig { returns(T::Array[String]) }
|
||||||
|
def zipinfo; end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
5
Library/Homebrew/sorbet/triggers.yml
Normal file
5
Library/Homebrew/sorbet/triggers.yml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
ruby_extra_args:
|
||||||
|
- --disable-gems
|
||||||
|
|
||||||
|
triggers:
|
||||||
|
using: sorbet/plugins/unpack_strategy_magic.rb
|
@ -7,12 +7,16 @@ require "system_command"
|
|||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
module UnpackStrategy
|
module UnpackStrategy
|
||||||
|
extend T::Sig
|
||||||
|
extend T::Helpers
|
||||||
|
|
||||||
include SystemCommand::Mixin
|
include SystemCommand::Mixin
|
||||||
|
|
||||||
# Helper module for identifying the file type.
|
# Helper module for identifying the file type.
|
||||||
module Magic
|
module Magic
|
||||||
# Length of the longest regex (currently Tar).
|
# Length of the longest regex (currently Tar).
|
||||||
MAX_MAGIC_NUMBER_LENGTH = 262
|
MAX_MAGIC_NUMBER_LENGTH = 262
|
||||||
|
private_constant :MAX_MAGIC_NUMBER_LENGTH
|
||||||
|
|
||||||
refine Pathname do
|
refine Pathname do
|
||||||
def magic_number
|
def magic_number
|
||||||
@ -125,11 +129,16 @@ module UnpackStrategy
|
|||||||
@merge_xattrs = merge_xattrs
|
@merge_xattrs = merge_xattrs
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract(to: nil, basename: nil, verbose: false)
|
abstract!
|
||||||
|
sig { abstract.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).returns(T.untyped) }
|
||||||
|
def extract_to_dir(unpack_dir, basename:, verbose:); end
|
||||||
|
private :extract_to_dir
|
||||||
|
|
||||||
|
def extract(to: nil, basename: nil, verbose: nil)
|
||||||
basename ||= path.basename
|
basename ||= path.basename
|
||||||
unpack_dir = Pathname(to || Dir.pwd).expand_path
|
unpack_dir = Pathname(to || Dir.pwd).expand_path
|
||||||
unpack_dir.mkpath
|
unpack_dir.mkpath
|
||||||
extract_to_dir(unpack_dir, basename: basename, verbose: verbose)
|
extract_to_dir(unpack_dir, basename: Pathname(basename), verbose: verbose || false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_nestedly(to: nil, basename: nil, verbose: false, prioritise_extension: false)
|
def extract_nestedly(to: nil, basename: nil, verbose: false, prioritise_extension: false)
|
||||||
|
5
Library/Homebrew/unpack_strategy.rbi
Normal file
5
Library/Homebrew/unpack_strategy.rbi
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# typed: strict
|
||||||
|
|
||||||
|
module UnpackStrategy
|
||||||
|
include Kernel
|
||||||
|
end
|
@ -1,27 +1,31 @@
|
|||||||
# typed: true
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module UnpackStrategy
|
module UnpackStrategy
|
||||||
# Strategy for unpacking ZIP archives.
|
# Strategy for unpacking ZIP archives.
|
||||||
class Zip
|
class Zip
|
||||||
|
extend T::Sig
|
||||||
|
|
||||||
include UnpackStrategy
|
include UnpackStrategy
|
||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
sig { returns(T::Array[String]) }
|
||||||
def self.extensions
|
def self.extensions
|
||||||
[".zip"]
|
[".zip"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(path: Pathname).returns(T::Boolean) }
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\APK(\003\004|\005\006)/n)
|
path.magic_number.match?(/\APK(\003\004|\005\006)/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def contains_extended_attributes?(path)
|
sig do
|
||||||
path.zipinfo.grep(/(^__MACOSX|\._)/).any?
|
override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean)
|
||||||
|
.returns(SystemCommand::Result)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_to_dir(unpack_dir, basename:, verbose:)
|
def extract_to_dir(unpack_dir, basename:, verbose:)
|
||||||
quiet_flags = verbose ? [] : ["-qq"]
|
quiet_flags = verbose ? [] : ["-qq"]
|
||||||
result = system_command! "unzip",
|
result = system_command! "unzip",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user