class Buildr::TarTask

The TarTask creates a new Tar file. You can include any number of files and and directories, use exclusion patterns, and include files into specific directories.

To create a GZipped Tar, either set the gzip option to true, or use the .tgz or .gz suffix.

For example:

tar("test.tgz").tap do |task|
  task.include "srcs"
  task.include "README", "LICENSE"
end

See Buildr#tar and ArchiveTask.

Attributes

gzip[RW]

To create a GZipped Tar, either set this option to true, or use the .tgz/.gz suffix.

mode[RW]

Permission mode for files contained in the Tar. Defaults to 0755.

Public Instance Methods

entry(name) → Entry click to toggle source

Returns a Tar file entry. You can use this to check if the entry exists and its contents, for example:

package(:tar).entry("src/LICENSE").should contain(/Apache Software License/)
# File lib/buildr/packaging/tar.rb, line 52
def entry(entry_name)
  Buildr::TarEntry.new(self, entry_name)
end
with_uncompressed_tar { |tar_entries| ... } click to toggle source

Yields an Archive::Tar::Minitar::Input object to the provided block. Opening, closing and Gzip-decompressing is automatically taken care of.

# File lib/buildr/packaging/tar.rb, line 67
def with_uncompressed_tar &block
  if gzip
    Zlib::GzipReader.open(name) { |tar| Archive::Tar::Minitar.open(tar, &block) }
  else
    Archive::Tar::Minitar.open(name, &block)
  end
end

Private Instance Methods

create_from(file_map, transform_map) click to toggle source
# File lib/buildr/packaging/tar.rb, line 77
def create_from(file_map, transform_map)
  if gzip
    StringIO.new.tap do |io|
      create_tar io, file_map, transform_map
      io.seek 0
      Zlib::GzipWriter.open(name) { |gzip| gzip.write io.read }
    end
  else
    File.open(name, 'wb') { |file| create_tar file, file_map, transform_map }
  end
end
create_tar(out, file_map, transform_map) click to toggle source
# File lib/buildr/packaging/tar.rb, line 89
def create_tar(out, file_map, transform_map)
  Archive::Tar::Minitar::Writer.open(out) do |tar|
    options = { :mode=>mode || '0755', :mtime=>Time.now }

    file_map.each do |path, contents|
      to_transform = []
      transform = transform_map.key?(path)
      if contents.nil?
      elsif File.directory?(contents.to_s)
        stat = File.stat(contents.to_s)
        tar.mkdir(path, options.merge(:mode=>stat.mode, :mtime=>stat.mtime, :uid=>stat.uid, :gid=>stat.gid))
      else
        contents = [contents].flatten

        combined_options = options
        if File.exists?(contents.first.to_s)
          stat = File.stat(contents.first.to_s)
          combined_options = options.merge(:mode=> stat.mode, :mtime=> stat.mtime, :uid=>stat.uid, :gid=> stat.gid)
        elsif contents.first.respond_to?(:mode)
          combined_options = combined_options.merge(:mode => contents.first.mode)
        end


        tar.add_file path, combined_options do |os, opts|
          [contents].flatten.each do |content|
            if content.respond_to?(:call)
              if transform
                output = StringIO.new
                content.call output
                to_transform << output.string
              else
                content.call os
              end
            else
              File.open content.to_s, 'rb' do |is|
                if transform
                  output = StringIO.new
                  while data = is.read(4096)
                    output << data
                  end
                  to_transform << output.string
                else
                  while data = is.read(4096)
                    os.write(data)
                  end
                end
              end
            end
          end
          if transform_map.key?(path)
            os.write(transform_map[path].call(to_transform))
          end
        end
      end

    end
  end
end