lib/tasks/zip.rb in buildr-1.2.3 vs lib/tasks/zip.rb in buildr-1.2.4
- old
+ new
@@ -14,18 +14,17 @@
# Returns the archive from this path.
attr_reader :root
def initialize(root, path)
@root = root
- @path = "#{path}/" if path
+ @path = path.blank? ? path : "#{path}/"
@files = FileList[]
# Expand source files added to this path.
expand_src = proc { @files.map{ |file| file.to_s }.uniq }
@sources = [ expand_src ]
# Add files and directories added to this path.
@actions = [] << proc do |file_map|
- file_map[@path] = nil if @path
expand_src.call.each do |path|
if File.directory?(path)
in_directory(path, @files) do |file, rel_path|
dest = "#{@path}#{rel_path}"
puts "Adding #{dest}" if Rake.application.options.trace
@@ -102,11 +101,13 @@
end
end
# Returns a Path relative to this one.
def path(path)
- path.blank? ? self : @root.path("#{@path}#{path}")
+ return self if path.blank?
+ return root.path(path[1..-1]) if path[0] == ?/
+ root.path("#{@path}#{path}")
end
# Returns all the source files.
def sources() #:nodoc:
@sources.map{ |source| source.call }.flatten
@@ -115,23 +116,24 @@
def add_files(file_map) #:nodoc:
@actions.each { |action| action.call(file_map) }
end
def to_s()
- @path || ""
+ @path
end
protected
def include_as(source, as)
@sources << proc { source }
@actions << proc do |file_map|
file = source.to_s
- file_map[@path] = nil if @path
if File.directory?(file)
in_directory(file) do |file, rel_path|
- dest = as == "." ? (@path || "") + rel_path.split("/")[1..-1].join("/") : "#{@path}#{as}#{rel_path}"
+ path = rel_path.split("/")[1..-1]
+ path.unshift as unless as == "."
+ dest = "#{@path}#{path.join('/')}"
puts "Adding #{dest}" if Rake.application.options.trace
file_map[dest] = file
end
else
puts "Adding #{@path}#{as}" if Rake.application.options.trace
@@ -186,11 +188,11 @@
end
def initialize(*args) #:nodoc:
super
- @paths = { nil=>Path.new(self, nil) }
+ @paths = { ""=>Path.new(self, "") }
@prepares = []
# Make sure we're the last enhancements, so other enhancements can add content.
enhance do
@file_map = {}
@@ -200,11 +202,14 @@
# we need to make sure the archive doesn't exist (e.g. opening an existing Zip will add instead of create).
# We also want to protect against partial updates.
rm name, :verbose=>false rescue nil
mkpath File.dirname(name), :verbose=>false
begin
- @paths.each { |name, object| object.add_files(@file_map) }
+ @paths.each do |name, object|
+ @file_map[name] = nil unless name.blank?
+ object.add_files(@file_map)
+ end
create_from @file_map
rescue
rm name, :verbose=>false rescue nil
raise
end
@@ -243,21 +248,21 @@
#
# The fifth form includes the contents of another archive by expanding it into this archive. For example:
# zip(..).include("foo.zip", :merge=>true).include("bar.zip")
# You can also use the method #merge.
def include(*files)
- @paths[nil].include *files
+ @paths[""].include *files
self
end
alias :add :include
# :call-seq:
# exclude(*files) => self
#
# Excludes files and returns self. Can be used in combination with include to prevent some files from being included.
def exclude(*files)
- @paths[nil].exclude *files
+ @paths[""].exclude *files
self
end
# :call-seq:
# merge(*files) => Merge
@@ -267,11 +272,11 @@
#
# Returns an object that supports two methods: include and exclude. You can use these methods to merge
# only specific files. For example:
# zip(..).merge("src.zip").include("module1/*")
def merge(*files)
- @paths[nil].merge *files
+ @paths[""].merge *files
end
# :call-seq:
# path(name) => Path
#
@@ -282,12 +287,22 @@
# Returns a Path object. The Path object implements all the same methods, like include, exclude, merge
# and so forth. It also implements path and root, so that:
# path("foo").path("bar") == path("foo/bar")
# path("foo").root == root
def path(name)
- return @paths[nil] if name.blank?
- @paths[name] ||= Path.new(self, name)
+ return @paths[""] if name.blank?
+ normalized = name.split("/").inject([]) do |path, part|
+ case part
+ when ".", nil, ""
+ path
+ when ".."
+ path[0...-1]
+ else
+ path << part
+ end
+ end.join("/")
+ @paths[normalized] ||= Path.new(self, normalized)
end
# :call-seq:
# root() => ArchiveTask
#
@@ -379,12 +394,19 @@
def create_from(file_map)
Zip::ZipFile.open(name, Zip::ZipFile::CREATE) do |zip|
zip.restore_permissions = true
file_map.each do |path, content|
- zip.mkdir path unless content || zip.find_entry(path)
- zip.add path, content if String === content
- zip.get_output_stream(path) { |output| content.call(output) } if content.respond_to?(:call)
+ if content
+ File.dirname(path).tap { |dir| zip.mkdir dir unless zip.find_entry(dir) }
+ if content.respond_to?(:call)
+ zip.get_output_stream(path) { |output| content.call(output) }
+ else
+ zip.add path, content.to_s
+ end
+ else
+ zip.mkdir path unless zip.find_entry(path)
+ end
end
end
end
end