class Buildr::Unzip

An object for unzipping/untarring a file into a target directory. You can tell it to include or exclude only specific files and directories, and also to map files from particular paths inside the zip file into the target directory. Once ready, call extract.

Usually it is more convenient to create a file task for extracting the zip file (see unzip) and pass this object as a prerequisite to other tasks.

See Buildr#unzip.

Attributes

target[RW]

The target directory to extract to.

zip_file[RW]

The zip file to extract.

Public Class Methods

new(args) click to toggle source

Initialize with hash argument of the form target=>zip_file.

# File lib/buildr/packaging/ziptask.rb, line 126
def initialize(args)
  @target, arg_names, zip_file = Buildr.application.resolve_args([args])
  @zip_file = zip_file.first
  @paths = {}
end

Public Instance Methods

add(*files)
Alias for: include
exclude(*files) → self click to toggle source

Exclude all files that match the patterns and return self.

Use exclude to unzip all files except those that match the pattern. You can use exclude in combination with include.

# File lib/buildr/packaging/ziptask.rb, line 233
def exclude(*files)
  if Hash === files.last
    from_path(files.pop[:path]).exclude *files
  else
    from_path(nil).exclude *files
  end
  self
end
extract click to toggle source

Extract the zip/tgz file into the target directory.

You can call this method directly. However, if you are using the unzip method, it creates a file task for the target directory: use that task instead as a prerequisite. For example:

build unzip(dir=>zip_file)

Or:

unzip(dir=>zip_file).target.invoke
# File lib/buildr/packaging/ziptask.rb, line 143
def extract
  # If no paths specified, then no include/exclude patterns
  # specified. Nothing will happen unless we include all files.
  if @paths.empty?
    @paths[nil] = FromPath.new(self, nil)
  end

  # Otherwise, empty unzip creates target as a file when touching.
  mkpath target.to_s
  if zip_file.to_s.match /\.t?gz$/
    #un-tar.gz
    Zlib::GzipReader.open(zip_file.to_s) { |tar|
      Archive::Tar::Minitar::Input.open(tar) do |inp|
        inp.each do |tar_entry|
          @paths.each do |path, patterns|
            patterns.map([tar_entry]).each do |dest, entry|
              next if entry.directory?
              dest = File.expand_path(dest, target.to_s)
              trace "Extracting #{dest}"
              mkpath File.dirname(dest) rescue nil
              File.open(dest, 'wb', entry.mode) {|f| f.write entry.read}
              File.chmod(entry.mode, dest)
            end
          end
        end
      end
    }
  else
    Zip::File.open(zip_file.to_s) do |zip|
      entries = zip.collect
      @paths.each do |path, patterns|
        patterns.map(entries).each do |dest, entry|
          next if entry.directory?
          dest = File.expand_path(dest, target.to_s)
          trace "Extracting #{dest}"
          mkpath File.dirname(dest) rescue nil
          entry.restore_permissions = true
          entry.extract(dest) { true }
        end
      end
    end
  end
  # Let other tasks know we updated the target directory.
  touch target.to_s
end
from_path(name) → Path click to toggle source

Allows you to unzip from a path. Returns an object you can use to specify which files to include/exclude relative to that path. Expands the file relative to that path.

For example:

unzip(Dir.pwd=>'test.jar').from_path('etc').include('LICENSE')

will unzip etc/LICENSE into ./LICENSE.

This is different from:

unzip(Dir.pwd=>'test.jar').include('etc/LICENSE')

which unzips etc/LICENSE into ./etc/LICENSE.

# File lib/buildr/packaging/ziptask.rb, line 256
def from_path(name)
  @paths[name] ||= FromPath.new(self, name)
end
Also aliased as: path
include(*files) → self click to toggle source
include(*files, :path=>name) → self

Include all files that match the patterns and returns self.

Use include if you only want to unzip some of the files, by specifying them instead of using exclusion. You can use include in combination with exclude.

# File lib/buildr/packaging/ziptask.rb, line 216
def include(*files)
  if Hash === files.last
    from_path(files.pop[:path]).include *files
  else
    from_path(nil).include *files
  end
  self
end
Also aliased as: add
included?(entry_name) click to toggle source

reads the includes/excludes and apply them to the entry_name

# File lib/buildr/packaging/ziptask.rb, line 190
def included?(entry_name)
  @paths.each do |path, patterns|
    return true if path.nil?
    if entry_name =~ /^#{path}/
      short = entry_name.sub(path, '')
      if patterns.include.any? { |pattern| File.fnmatch(pattern, entry_name) } &&
        !patterns.exclude.any? { |pattern| File.fnmatch(pattern, entry_name) }
        # trace "tar_entry.full_name " + entry_name + " is included"
        return true
      end
    end
  end
  # trace "tar_entry.full_name " + entry_name + " is excluded"
  return false
end
path(name)
Alias for: from_path
root → Unzip click to toggle source

Returns the root path, essentially the Unzip object itself. In case you are wondering down paths and want to go back.

# File lib/buildr/packaging/ziptask.rb, line 266
def root
  self
end
to_s() click to toggle source

Returns the path to the target directory.

# File lib/buildr/packaging/ziptask.rb, line 271
def to_s
  target.to_s
end