brew/Library/Homebrew/unpack_strategy.rb

208 lines
5.5 KiB
Ruby
Raw Normal View History

2023-03-20 13:15:43 -07:00
# typed: true
# frozen_string_literal: true
require "system_command"
2020-08-09 06:09:05 +02:00
# Module containing all available strategies for unpacking archives.
#
# @api private
module UnpackStrategy
extend T::Sig
extend T::Helpers
include SystemCommand::Mixin
2018-07-01 23:35:29 +02:00
def self.strategies
@strategies ||= [
2021-09-16 15:56:31 +01:00
Tar, # Needs to be before Bzip2/Gzip/Xz/Lzma/Zstd.
2018-09-11 19:21:18 +02:00
Pax,
Gzip,
Dmg, # Needs to be before Bzip2/Xz/Lzma.
Lzma,
Xz,
2021-09-16 15:56:31 +01:00
Zstd,
Lzip,
2020-08-09 06:09:05 +02:00
Air, # Needs to be before `Zip`.
Jar, # Needs to be before `Zip`.
LuaRock, # Needs to be before `Zip`.
MicrosoftOfficeXml, # Needs to be before `Zip`.
2018-07-29 10:33:47 +02:00
Zip,
2020-08-09 06:09:05 +02:00
Pkg, # Needs to be before `Xar`.
2018-07-29 10:33:47 +02:00
Xar,
Ttf,
Otf,
Git,
Mercurial,
Subversion,
Cvs,
2020-08-09 06:09:05 +02:00
SelfExtractingExecutable, # Needs to be before `Cab`.
Cab,
Executable,
2018-07-25 23:34:17 +02:00
Bzip2,
Fossil,
Bazaar,
2018-07-27 11:42:34 +02:00
Compress,
P7Zip,
Sit,
Rar,
Lha,
2018-07-01 23:35:29 +02:00
].freeze
end
private_class_method :strategies
def self.from_type(type)
type = {
2018-11-02 17:18:07 +00:00
naked: :uncompressed,
nounzip: :uncompressed,
seven_zip: :p7zip,
}.fetch(type, type)
begin
2018-09-03 21:40:19 +02:00
const_get(type.to_s.split("_").map(&:capitalize).join.gsub(/\d+[a-z]/, &:upcase))
rescue NameError
nil
end
end
def self.from_extension(extension)
2018-09-11 19:21:18 +02:00
strategies.sort_by { |s| s.extensions.map(&:length).max || 0 }
.reverse
.find { |s| s.extensions.any? { |ext| extension.end_with?(ext) } }
end
2018-07-01 23:35:29 +02:00
def self.from_magic(path)
strategies.find { |s| s.can_extract?(path) }
end
def self.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref: nil, merge_xattrs: nil)
strategy = from_type(type) if type
2018-07-01 23:35:29 +02:00
if prioritize_extension && path.extname.present?
strategy ||= from_extension(path.extname)
strategy ||= strategies.select { |s| s < Directory || s == Fossil }
.find { |s| s.can_extract?(path) }
else
strategy ||= from_magic(path)
strategy ||= from_extension(path.extname)
end
strategy ||= Uncompressed
strategy.new(path, ref_type: ref_type, ref: ref, merge_xattrs: merge_xattrs)
2018-07-01 23:35:29 +02:00
end
attr_reader :path, :merge_xattrs
2018-07-01 23:35:29 +02:00
def initialize(path, ref_type: nil, ref: nil, merge_xattrs: nil)
2018-07-01 23:35:29 +02:00
@path = Pathname(path).expand_path
@ref_type = ref_type
@ref = ref
@merge_xattrs = merge_xattrs
2018-07-01 23:35:29 +02:00
end
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
sig {
params(
to: T.nilable(Pathname), basename: T.nilable(T.any(String, Pathname)), verbose: T::Boolean,
).returns(T.untyped)
}
def extract(to: nil, basename: nil, verbose: false)
2018-07-09 20:04:33 +02:00
basename ||= path.basename
2018-07-01 23:35:29 +02:00
unpack_dir = Pathname(to || Dir.pwd).expand_path
unpack_dir.mkpath
extract_to_dir(unpack_dir, basename: Pathname(basename), verbose: verbose)
2018-07-01 23:35:29 +02:00
end
2018-07-16 19:00:49 +02:00
sig {
params(
to: T.nilable(Pathname),
basename: T.nilable(T.any(String, Pathname)),
verbose: T::Boolean,
prioritize_extension: T::Boolean,
).returns(T.untyped)
}
def extract_nestedly(to: nil, basename: nil, verbose: false, prioritize_extension: false)
2018-07-16 19:00:49 +02:00
Dir.mktmpdir do |tmp_unpack_dir|
tmp_unpack_dir = Pathname(tmp_unpack_dir)
extract(to: tmp_unpack_dir, basename: basename, verbose: verbose)
2018-07-16 19:00:49 +02:00
children = tmp_unpack_dir.children
if children.count == 1 && !children.first.directory?
s = UnpackStrategy.detect(children.first, prioritize_extension: prioritize_extension)
2018-07-16 19:00:49 +02:00
s.extract_nestedly(to: to, verbose: verbose, prioritize_extension: prioritize_extension)
2018-07-16 19:00:49 +02:00
next
end
# Ensure all extracted directories are writable.
2023-03-20 13:15:43 -07:00
each_directory(tmp_unpack_dir) do |path|
next if path.writable?
FileUtils.chmod "u+w", path, verbose: verbose
end
Directory.new(tmp_unpack_dir).extract(to: to, verbose: verbose)
2018-07-16 19:00:49 +02:00
end
end
def dependencies
[]
end
2023-03-20 13:15:43 -07:00
# Helper method for iterating over directory trees.
sig {
params(
2023-03-20 13:16:31 -07:00
pathname: Pathname,
_block: T.proc.params(path: Pathname).void,
).returns(T.nilable(Pathname))
2023-03-20 13:15:43 -07:00
}
def each_directory(pathname, &_block)
pathname.find do |path|
yield path if path.directory?
end
end
2018-07-01 23:35:29 +02:00
end
require "unpack_strategy/air"
require "unpack_strategy/bazaar"
require "unpack_strategy/bzip2"
require "unpack_strategy/cab"
require "unpack_strategy/compress"
require "unpack_strategy/cvs"
require "unpack_strategy/directory"
require "unpack_strategy/dmg"
require "unpack_strategy/executable"
require "unpack_strategy/fossil"
require "unpack_strategy/generic_unar"
require "unpack_strategy/git"
require "unpack_strategy/gzip"
require "unpack_strategy/jar"
require "unpack_strategy/lha"
require "unpack_strategy/lua_rock"
require "unpack_strategy/lzip"
require "unpack_strategy/lzma"
require "unpack_strategy/mercurial"
require "unpack_strategy/microsoft_office_xml"
require "unpack_strategy/otf"
require "unpack_strategy/p7zip"
2018-09-11 19:21:18 +02:00
require "unpack_strategy/pax"
require "unpack_strategy/pkg"
require "unpack_strategy/rar"
require "unpack_strategy/self_extracting_executable"
require "unpack_strategy/sit"
require "unpack_strategy/subversion"
require "unpack_strategy/tar"
require "unpack_strategy/ttf"
require "unpack_strategy/uncompressed"
require "unpack_strategy/xar"
require "unpack_strategy/xz"
require "unpack_strategy/zip"
2021-09-16 15:56:31 +01:00
require "unpack_strategy/zstd"