Class: Rant::Generators::Archive::Base

Inherits:
Object
  • Object
show all
Extended by:
MetaUtils
Defined in:
lib/rant/import/archive.rb

Overview

A subclass has to provide a define_task method to act as a generator.

Direct Known Subclasses

Tgz, Zip

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetaUtils

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

Constructor Details

#initialize(name, files = nil) ⇒ Base

Returns a new instance of Base.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rant/import/archive.rb', line 108

def initialize(name, files = nil)
    self.name = name or raise "package name required"
    @files = files
    @version, @extension, @archive_path = nil
    @rac = nil
    @pkg_task = nil
    @ch = nil
    @files_only = false
    @manifest_task = nil
    @basedir = nil
           @res_files = nil
           @manifest = nil
           @dist_dir_task = nil
end

Instance Attribute Details

#archive_pathObject (readonly)

Returns the value of attribute archive_path.



101
102
103
# File 'lib/rant/import/archive.rb', line 101

def archive_path
  @archive_path
end

#chObject

Caller information, e.g.: => “Rantfile”, :ln => 10



106
107
108
# File 'lib/rant/import/archive.rb', line 106

def ch
  @ch
end

Class Method Details

.rant_gen(rac, ch, args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rant/import/archive.rb', line 22

def self.rant_gen(rac, ch, args, &block)
    pkg_name = args.shift
    unless pkg_name
	rac.abort_at(ch,
	    "#{self} takes at least one argument (package name)")
    end
    opts = nil
    flags = []
    arg = args.shift
    case arg
    when String
	basedir = pkg_name
	pkg_name = arg
    when Symbol
	flags << arg
    else
	opts = arg
    end
    flags << arg while Symbol === (arg = args.shift)
    opts ||= (arg || {})
    unless args.empty?
	rac.abort_at(ch, "#{self}: too many arguments")
    end

    pkg = self.new(pkg_name)
    pkg.basedir = basedir if basedir
    pkg.rac = rac
    pkg.ch = ch
    flags.each { |f|
	case f
	when :manifest
	    pkg.manifest = "MANIFEST"
	when :verbose
	    # TODO
	when :quiet
	    # TODO
	else
	    rac.warn_msg(
		"#{self}: ignoring unknown flag #{flag}")
	end
    }
    if opts.respond_to? :to_hash
	opts = opts.to_hash
    else
	rac.abort_at(ch,
	    "#{self}: option argument has to be a hash.")
    end
    opts.each { |k, v|
	case k
	when :version
	    pkg.version = v
	when :extension
	    pkg.extension = v
	when :files
	    pkg.files = v
	when :manifest
	    pkg.manifest = v
	when :files_only
	    pkg.files_only = v
	else
	    rac.warn_msg(
		"#{self}: ignoring unknown option #{k}")
	end
    }
           desc = pkg.rac.pop_desc
           if opts[:files] and opts[:manifest] || flags.include?(:manifest)
               pkg.define_manifest_task
           end
           pkg.rac.cx.desc desc
    pkg.define_task
    pkg
end

Instance Method Details

#define_manifest_taskObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rant/import/archive.rb', line 200

def define_manifest_task
    return @manifest_task if @manifest_task
    @manifest_task =
               ::Rant::Generators::Task.rant_gen(
                       @rac, @ch, [@manifest]) do |t|
                   t.file_target
	    t.needed {
                       # fl refers to @res_files
                       fl = get_files
		if test ?f, @manifest
                           read_manifest != @res_files.to_ary
		else
		    true
		end
	    }
	    t.act {
                       write_manifest get_files
	    }
	end
end

#get_archive_pathObject

Path to archive without basedir.



142
143
144
145
146
147
148
# File 'lib/rant/import/archive.rb', line 142

def get_archive_path
    return @archive_path if @archive_path
    path = name.dup
    path << "-#@version" if @version
    path << @extension if @extension
    @archive_path = path
end

#get_filesObject

This method sets @res_files to the return value, a list of files to include in the archive.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rant/import/archive.rb', line 152

def get_files
           return @res_files if @res_files
    fl = @files ? @files.dup : []
    if @manifest
               fl = read_manifest unless @files
               fl = Rant::FileList(fl)
               fl.keep(@manifest)
    elsif @files_only
               fl = Rant::FileList(fl)
	fl.no_dirs
           else
               fl = Rant::FileList(fl)
    end
           # remove leading `./' relicts
           @res_files = fl.map! { |fn| fn.sub(/^\.\/(?=.)/,'') }
           if defined?(@dist_path) && @dist_path
               # Remove entries from the dist_path directory, which
               # would create some sort of weird recursion.
               #
               # Normally, the Rantfile writer should care himself,
               # but since I tapped into this trap frequently now...
               @res_files.exclude(/^#{Regexp.escape @dist_path}(\/.*)?$/)
               # exclude the final archive file as well?
           end
           @res_files.uniq!.sort!
end

#pathObject Also known as: to_rant_target

Path to archive file.



132
133
134
135
136
137
138
# File 'lib/rant/import/archive.rb', line 132

def path
    if basedir
	File.join(basedir, get_archive_path)
    else
	get_archive_path
    end
end

#racObject



123
124
125
# File 'lib/rant/import/archive.rb', line 123

def rac
    @rac
end

#rac=(val) ⇒ Object



126
127
128
129
# File 'lib/rant/import/archive.rb', line 126

def rac=(val)
    @rac = val
    @pkg_task = nil
end

#with_manifestObject

Creates an (eventually) temporary manifest file and yields with the path of this file as argument.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rant/import/archive.rb', line 181

def with_manifest
    fl = get_files
    if @manifest
	rac.build @manifest
	yield @manifest
    else
               require 'rant/tempfile' #rant-import:remove
	tf = Rant::Tempfile.new "rant"
	begin
	    fl.each { |path| tf.puts path }
	    tf.close
	    yield(tf.path)
	ensure
	    tf.unlink
	end
    end
    nil
end