Module: UnpackStrategy Private

Extended by:
T::Helpers
Includes:
Kernel, SystemCommand::Mixin
Included in:
Air, Bzip2, Cab, Directory, Dmg, Fossil, GenericUnar, Gzip, Lha, Lzip, Lzma, P7Zip, Pax, Rar, Tar, Uncompressed, Xar, Xz, Zip, Zstd
Defined in:
unpack_strategy.rb,
extend/os/mac/unpack_strategy/zip.rb,
unpack_strategy/xz.rb,
unpack_strategy/air.rb,
unpack_strategy/cab.rb,
unpack_strategy/cvs.rb,
unpack_strategy/dmg.rb,
unpack_strategy/git.rb,
unpack_strategy/jar.rb,
unpack_strategy/lha.rb,
unpack_strategy/otf.rb,
unpack_strategy/pax.rb,
unpack_strategy/pkg.rb,
unpack_strategy/rar.rb,
unpack_strategy/sit.rb,
unpack_strategy/tar.rb,
unpack_strategy/ttf.rb,
unpack_strategy/xar.rb,
unpack_strategy/zip.rb,
unpack_strategy/gzip.rb,
unpack_strategy/lzip.rb,
unpack_strategy/lzma.rb,
unpack_strategy/zstd.rb,
unpack_strategy/bzip2.rb,
unpack_strategy/p7zip.rb,
unpack_strategy/bazaar.rb,
unpack_strategy/fossil.rb,
unpack_strategy/compress.rb,
unpack_strategy/lua_rock.rb,
unpack_strategy/directory.rb,
unpack_strategy/mercurial.rb,
unpack_strategy/executable.rb,
unpack_strategy/subversion.rb,
unpack_strategy/generic_unar.rb,
unpack_strategy/uncompressed.rb,
unpack_strategy/microsoft_office_xml.rb,
unpack_strategy/self_extracting_executable.rb,
unpack_strategy.rbi

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Air, Bazaar, Bzip2, Cab, Compress, Cvs, Directory, Dmg, Executable, Fossil, GenericUnar, Git, Gzip, Jar, Lha, LuaRock, Lzip, Lzma, Mercurial, MicrosoftOfficeXml, Otf, P7Zip, Pax, Pkg, Rar, SelfExtractingExecutable, Sit, Subversion, Tar, Ttf, Uncompressed, Xar, Xz, Zip, Zstd

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Kernel

#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #with_env, #with_homebrew_path

Methods included from SystemCommand::Mixin

#system_command, #system_command!

Instance Attribute Details

#merge_xattrsObject (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



91
92
93
# File 'unpack_strategy.rb', line 91

def merge_xattrs
  @merge_xattrs
end

#pathObject (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



91
92
93
# File 'unpack_strategy.rb', line 91

def path
  @path
end

Class Method Details

.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref: nil, merge_xattrs: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'unpack_strategy.rb', line 74

def self.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref: nil, merge_xattrs: nil)
  strategy = from_type(type) if type

  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:, merge_xattrs:)
end

.from_extension(extension) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



64
65
66
67
68
# File 'unpack_strategy.rb', line 64

def self.from_extension(extension)
  strategies.sort_by { |s| s.extensions.map(&:length).max || 0 }
            .reverse
            .find { |s| s.extensions.any? { |ext| extension.end_with?(ext) } }
end

.from_magic(path) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



70
71
72
# File 'unpack_strategy.rb', line 70

def self.from_magic(path)
  strategies.find { |s| s.can_extract?(path) }
end

.from_type(type) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'unpack_strategy.rb', line 50

def self.from_type(type)
  type = {
    naked:     :uncompressed,
    nounzip:   :uncompressed,
    seven_zip: :p7zip,
  }.fetch(type, type)

  begin
    const_get(type.to_s.split("_").map(&:capitalize).join.gsub(/\d+[a-z]/, &:upcase))
  rescue NameError
    nil
  end
end

Instance Method Details

#dependenciesObject

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



152
153
154
# File 'unpack_strategy.rb', line 152

def dependencies
  []
end

#each_directory(pathname, &_block) ⇒ Pathname?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Helper method for iterating over directory trees.

Parameters:

Returns:



163
164
165
166
167
# File 'unpack_strategy.rb', line 163

def each_directory(pathname, &_block)
  pathname.find do |path|
    yield path if path.directory?
  end
end

#extract(to: nil, basename: nil, verbose: false) ⇒ T.untyped

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • to (Pathname, nil) (defaults to: nil)
  • basename (String, Pathname, nil) (defaults to: nil)
  • verbose (Boolean) (defaults to: false)

Returns:

  • (T.untyped)


110
111
112
113
114
115
# File 'unpack_strategy.rb', line 110

def extract(to: nil, basename: nil, verbose: false)
  basename ||= path.basename
  unpack_dir = Pathname(to || Dir.pwd).expand_path
  unpack_dir.mkpath
  extract_to_dir(unpack_dir, basename: Pathname(basename), verbose:)
end

#extract_nestedly(to: nil, basename: nil, verbose: false, prioritize_extension: false) ⇒ T.untyped

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • to (Pathname, nil) (defaults to: nil)
  • basename (String, Pathname, nil) (defaults to: nil)
  • verbose (Boolean) (defaults to: false)
  • prioritize_extension (Boolean) (defaults to: false)

Returns:

  • (T.untyped)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'unpack_strategy.rb', line 125

def extract_nestedly(to: nil, basename: nil, verbose: false, prioritize_extension: false)
  Dir.mktmpdir("homebrew-unpack", HOMEBREW_TEMP) do |tmp_unpack_dir|
    tmp_unpack_dir = Pathname(tmp_unpack_dir)

    extract(to: tmp_unpack_dir, basename:, verbose:)

    children = tmp_unpack_dir.children

    if children.size == 1 && !children.fetch(0).directory?
      s = UnpackStrategy.detect(children.first, prioritize_extension:)

      s.extract_nestedly(to:, verbose:, prioritize_extension:)

      next
    end

    # Ensure all extracted directories are writable.
    each_directory(tmp_unpack_dir) do |path|
      next if path.writable?

      FileUtils.chmod "u+w", path, verbose:
    end

    Directory.new(tmp_unpack_dir).extract(to:, verbose:)
  end
end

#initialize(path, ref_type: nil, ref: nil, merge_xattrs: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



93
94
95
96
97
98
# File 'unpack_strategy.rb', line 93

def initialize(path, ref_type: nil, ref: nil, merge_xattrs: nil)
  @path = Pathname(path).expand_path
  @ref_type = ref_type
  @ref = ref
  @merge_xattrs = merge_xattrs
end