lib/tasks/zip.rb in buildr-1.1.3 vs lib/tasks/zip.rb in buildr-1.2.0
- old
+ new
@@ -27,19 +27,19 @@
@zip = zip
@path = "#{path}/" if path
expand_src = proc { (@files || []).map(&:to_s).uniq }
@sources = [ expand_src ]
@actions = [] << proc do |zip|
- expand_src.call.each do |file|
- if File.directory?(file)
- in_directory(file, @files) do |file, rel_path|
+ expand_src.call.each do |path|
+ if File.directory?(path)
+ in_directory(path, @files) do |file, rel_path|
puts "Adding #{@path}#{rel_path}" if Rake.application.options.trace
zip.add("#{@path}#{rel_path}", file) { true }
end
else
- puts "Adding #{@path}#{File.basename(file)}" if Rake.application.options.trace
- zip.add("#{@path}#{File.basename(file)}", file) { true }
+ puts "Adding #{@path}#{File.basename(path)}" if Rake.application.options.trace
+ zip.add("#{@path}#{File.basename(path)}", path) { true }
end
end
end
end
@@ -56,10 +56,14 @@
path(options[:path]).include *files +[ options.reject { |k,v| k == :path } ]
elsif options[:as]
raise "You can only use the :as option in combination with the :path option" unless options.keys.size == 1
raise "You can only use one file with the :as option" unless files.size == 1
include_as(files.first.to_s, options[:as])
+ elsif options[:from]
+ raise "You can only use the :from option in combination with the :path option" unless options.keys.size == 1
+ raise "You canont use the :from option with file names" unless files.empty?
+ [options[:from]].flatten.each { |path| include_as(path.to_s, ".") }
elsif options[:merge]
raise "You can only use the :merge option in combination with the :path option" unless options.keys.size == 1
files.each { |file| merge file }
elsif options.keys.empty?
(@files ||= FileList[]).include files.map(&:to_s)
@@ -107,10 +111,14 @@
# Returns all the source files.
def sources()
@sources.map(&:call).flatten
end
+ def to_s()
+ @path || ""
+ end
+
protected
def include_as(source, as)
@sources << proc { source }
@actions << proc do |zip|
@@ -208,11 +216,11 @@
# Include files in the ZIP (or current path) and returns self.
#
# This method accepts three options. You can use :path to include files under
# a specific path, for example:
# zip(..).include("foo", :path=>"bar")
- # includes the file bar as foo/bar. See also #path.
+ # includes the file bar as bar/foo. See also #path.
#
# You can use :as to include a file under a different name, for example:
# zip(..).include("foo", :as=>"bar")
# You can use the :as option in combination with the :path option, but only with
# a single file at a time.
@@ -293,29 +301,28 @@
#
# For example:
# package(:jar).with(:manifest=>"MANIFEST_MF")
def with(options)
options.each do |key, value|
- self[key] = value
+ begin
+ send "#{key}=", value
+ rescue NameError
+ if respond_to?(:[]=) # Backward compatible with Buildr 1.1.
+ warn_deprecated "The []= method is deprecated, please use attribute accessors instead."
+ self[key] = value
+ else
+ raise ArgumentError, "This task does not support the option #{key}."
+ end
+ end
end
self
end
- # :call-seq:
- # [name] = value
- #
- # Used by with method to set specific options. For example:
- # package(:jar).with(:manifest=>"MANIFEST_MF")
- # Or:
- # package(:jar)[:manifest] = "MANIFEST_MF"
- def []=(key, value)
- raise ArgumentError, "#{self.class} does not support the option #{key}"
+ def invoke_prerequisites() #:nodoc:
+ prerequisites.concat @paths.collect { |name, path| path.sources }.flatten
+ super
end
-
- def prerequisites() #:nodoc:
- super + @paths.collect { |name, path| path.sources }.flatten.each { |src| file(src) }
- end
def needed?() #:nodoc:
return true unless File.exist?(name)
# You can do something like:
# include("foo", :path=>"foo").exclude("foo/bar", path=>"foo").
@@ -339,10 +346,14 @@
# e.g. creating a manifest file in a JAR.
def create(zip)
@paths.each { |name, obj| obj.actions.each { |action| action[zip] } }
end
+ def []=(key, value) #:nodoc:
+ raise ArgumentError, "This task does not support the option #{key}."
+ end
+
end
# :call-seq:
# zip(file) => ZipTask
#
@@ -394,11 +405,11 @@
# unzip(dir=>zip_file).target.invoke
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(nil)
+ @paths[nil] = FromPath.new(self, nil)
@paths[nil].include "*"
end
# Otherwise, empty unzip creates target as a file when touching.
mkpath target.to_s, :verbose=>false
@@ -466,21 +477,32 @@
#
# This is different from:
# unzip(Dir.pwd=>"test.jar").include("etc/LICENSE")
# which unzips etc/LICENSE into ./etc/LICENSE.
def from_path(name)
- @paths[name] ||= FromPath.new(name)
+ @paths[name] ||= FromPath.new(self, name)
end
+ alias :path :from_path
+ # :call-seq:
+ # root() => Unzip
+ #
+ # Returns the root path, essentially the Unzip object itself. In case you are wondering
+ # down paths and want to go back.
+ def root()
+ self
+ end
+
# Returns the path to the target directory.
def to_s()
target.to_s
end
class FromPath #:nodoc:
- def initialize(path)
+ def initialize(unzip, path)
+ @unzip = unzip
if path
@path = path[-1] == ?/ ? path : path + "/"
else
@path = ""
end
@@ -511,10 +533,20 @@
end
map
end
end
+ # Documented in Unzip.
+ def root()
+ @unzip
+ end
+
+ # The target directory to extract to.
+ def target()
+ @unzip.target
+ end
+
end
end
# :call-seq:
@@ -542,5 +574,22 @@
task.enhance { setup.extract }
end
end
end
+
+
+module Zip #:nodoc:
+ class ZipEntrySet #:nodoc:
+
+ # Make sure entries are returned in sorted order so the ZIP
+ # index is human readable instead of random hashtable order.
+ def entries()
+ @entrySet.values.sort
+ end
+
+ def each(&block)
+ entries.each(&block)
+ end
+
+ end
+end