Module: Hackmac::AssetTools

Included in:
Hackmac, KextUpgrader, OCUpgrader
Defined in:
lib/hackmac/asset_tools.rb

Overview

A module that provides utility methods for creating temporary directories and decompressing files.

The AssetTools module offers helper methods designed to simplify common tasks when working with temporary file operations and compressed assets. It includes functionality for isolating code execution within temporary directories and extracting various compressed file formats.

Examples:

include Hackmac::AssetTools

isolate do |dir|
  # Code executed within temporary directory
end

decompress('archive.tar.gz')

Instance Method Summary collapse

Instance Method Details

#decompress(name) ⇒ void (private)

This method returns an undefined value.

The decompress method extracts compressed files in either ZIP or tar.gz format

This method takes a filename and attempts to decompress it using the appropriate system command based on the file extension. It provides visual feedback during the decompression process and raises an error if the operation fails.

Parameters:

  • name (String)

    the path to the compressed file to be decompressed

Raises:

  • (RuntimeError)

    raised when the file extension is not supported or decompression commands fail



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hackmac/asset_tools.rb', line 55

def decompress(name)
  print "Decompressing #{name.inspect}"
  case name
  when /\.zip\z/i
    system "unzip #{name.inspect}" or fail "Could not unzip #{name.inspect}"
  when /\.tar\.gz\z/i
    system "tar xfz #{name.inspect}" or fail "Could not tar xfz #{name.inspect}"
  else
    fail "Cannot decompress #{name.inspect}"
  end
  puts "done!"
end

#isolate {|dir| ... } ⇒ Object (private)

The isolate method creates a temporary directory and yields control to a block within that directory context.

This method establishes a temporary working directory using Dir.mktmpdir, changes the current working directory to that location, and then executes the provided block within that context. After the block completes, it ensures proper cleanup of the temporary directory.

directory context

Yields:

  • (dir)

    the block to execute within the temporary

Returns:

  • (Object)

    the return value of the yielded block



33
34
35
36
37
38
39
# File 'lib/hackmac/asset_tools.rb', line 33

def isolate
  Dir.mktmpdir do |dir|
    cd dir do
      yield dir
    end
  end
end