Class: Rant::Generators::Archive::Zip

Inherits:
Base show all
Defined in:
lib/rant/import/archive/zip.rb

Overview

Use this class as a generator to create zip archives.

Direct Known Subclasses

Package::Zip

Instance Attribute Summary

Attributes inherited from Base

#archive_path, #ch

Instance Method Summary collapse

Methods inherited from Base

#define_manifest_task, #get_archive_path, #get_files, #path, #rac, #rac=, rant_gen, #with_manifest

Methods included from MetaUtils

#rant_attr, #rant_flag, #redirect_accessor, #redirect_message, #redirect_reader, #redirect_writer, #string_attr, #valid_attr_name

Constructor Details

#initialize(*args) ⇒ Zip

Returns a new instance of Zip.


12
13
14
15
# File 'lib/rant/import/archive/zip.rb', line 12

def initialize(*args)
    super
    @extension = ".zip"
end

Instance Method Details

#define_rubyzip_taskObject


38
39
40
41
42
# File 'lib/rant/import/archive/zip.rb', line 38

def define_rubyzip_task
    define_cmd_task do |path, t|
               rubyzip t.name, @res_files
    end
end

#define_taskObject

Ensure to set #rac first. Creates a file task wich invokes zip to create a zip archive. Returns the created task.


19
20
21
22
23
24
25
# File 'lib/rant/import/archive/zip.rb', line 19

def define_task
    if ::Rant::Env.have_zip?
	define_zip_task
    else
	define_rubyzip_task
    end
end

#define_zip_taskObject


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rant/import/archive/zip.rb', line 26

def define_zip_task
    define_cmd_task { |path, t|
               # Add -y option to store symlinks instead of
               # referenced files.
	cmd = "zip [email protected] #{t.name}"
	@rac.cmd_msg cmd
	IO.popen cmd, "w" do |z|
	    z.print IO.read(path)
	end
	raise Rant::CommandError.new(cmd, $?) unless $?.success?
    }
end

#rubyzip(fn, files, opts = {:recurse => false}) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rant/import/archive/zip.rb', line 43

def rubyzip fn, files, opts = {:recurse => false}
    require 'rant/archive/rubyzip'
    # rubyzip creates only a new file if fn doesn't exist
    @rac.sys.rm_f fn if test ?e, fn
    @rac.cmd_msg "rubyzip #{fn}"
    Rant::Archive::Rubyzip::ZipFile.open fn,
        Rant::Archive::Rubyzip::ZipFile::CREATE do |z|
        if opts[:recurse]
            require 'find'
            files.each { |f|
                if test ?d, f
                    Find.find(f) { |f2| z.add f2, f2 }
                else
                    z.add f, f
                end
            }
        else
            files.each { |f|
                z.add f, f
            }
        end
    end
    nil
end