lib/tasks/zip.rb in buildr-1.2.5 vs lib/tasks/zip.rb in buildr-1.2.6

- old
+ new

@@ -15,26 +15,29 @@ attr_reader :root def initialize(root, path) @root = root @path = path.blank? ? path : "#{path}/" - @files = FileList[] + @includes = FileList[] + @excludes = [] # Expand source files added to this path. - expand_src = proc { @files.map{ |file| file.to_s }.uniq } + expand_src = proc { @includes.map{ |file| file.to_s }.uniq } @sources = [ expand_src ] # Add files and directories added to this path. @actions = [] << proc do |file_map| 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 - file_map[dest] = file + unless excluded?(path) + if File.directory?(path) + in_directory path do |file, rel_path| + dest = "#{@path}#{rel_path}" + puts "Adding #{dest}" if Rake.application.options.trace + file_map[dest] = file + end + else + puts "Adding #{@path}#{File.basename(path)}" if Rake.application.options.trace + file_map["#{@path}#{File.basename(path)}"] = path end - else - puts "Adding #{@path}#{File.basename(path)}" if Rake.application.options.trace - file_map["#{@path}#{File.basename(path)}"] = path end end end end @@ -47,11 +50,11 @@ def include(*args) options = args.pop if Hash === args.last files = args.flatten if options.nil? || options.empty? - @files.include *files.map { |file| file.to_s } + @includes.include *files.flatten elsif options[:path] sans_path = options.reject { |k,v| k == :path } path(options[:path]).include *files + [sans_path] elsif options[:as] raise "You can only use the :as option in combination with the :path option" unless options.size == 1 @@ -72,11 +75,13 @@ alias :add :include # :call-seq: # exclude(*files) => self def exclude(*files) - @files.exclude *files + files = files.flatten.map(&:to_s) + @excludes |= files + @excludes |= files.reject { |f| f =~ /\*$/ }.map { |f| "#{f}/*" } self end # :call-seq: # merge(*files) => Merge @@ -125,32 +130,37 @@ def include_as(source, as) @sources << proc { source } @actions << proc do |file_map| file = source.to_s - if File.directory?(file) - in_directory(file) do |file, 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 + unless excluded?(file) + if File.directory?(file) + in_directory file do |file, 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 + file_map["#{@path}#{as}"] = file end - else - puts "Adding #{@path}#{as}" if Rake.application.options.trace - file_map["#{@path}#{as}"] = file end end end - def in_directory(dir, excludes = nil) + def in_directory(dir) prefix = Regexp.new("^" + Regexp.escape(File.dirname(dir) + File::SEPARATOR)) - FileList["#{dir}/**/*"]. - reject { |file| File.directory?(file) || (excludes && excludes.exclude?(file)) }. + FileList.recursive(dir).reject { |file| excluded?(file) }. each { |file| yield file, file.sub(prefix, "") } end + def excluded?(file) + @excludes.any? { |exclude| File.fnmatch(exclude, file) } + end + end # Extend one Zip file into another. class ZipExpander #:nodoc: @@ -353,11 +363,11 @@ # We need to check that any file we include is not newer than the # contents of the Zip. The file itself but also the directory it's # coming from, since some tasks touch the directory, e.g. when the # content of target/classes is included into a WAR. most_recent = @paths.collect { |name, path| path.sources }.flatten. - each { |src| File.directory?(src) ? FileList["#{src}/**/*"] | [src] : src }.flatten. + each { |src| File.directory?(src) ? FileList.recursive(src) | [src] : src }.flatten. select { |file| File.exist?(file) }.collect { |file| File.stat(file).mtime }.max File.stat(name).mtime < (most_recent || Rake::EARLY) || super end protected @@ -374,12 +384,10 @@ raise ArgumentError, "This task does not support the option #{key}." end end - - # The ZipTask creates a new Zip file. You can include any number of files and and directories, # use exclusion patterns, and include files into specific directories. # # For example: # zip("test.zip").tap do |task| @@ -393,19 +401,24 @@ private def create_from(file_map) Zip::ZipFile.open(name, Zip::ZipFile::CREATE) do |zip| zip.restore_permissions = true + mkpath = lambda do |dir| + unless dir == "." || zip.find_entry(dir) + mkpath.call File.dirname(dir) + zip.mkdir dir + end + end + file_map.each do |path, content| - 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 + mkpath.call File.dirname(path) + if content.respond_to?(:call) + zip.get_output_stream(path) { |output| content.call(output) } + elsif content.nil? || File.directory?(content.to_s) + mkpath.call path else - zip.mkdir path unless zip.find_entry(path) + zip.add path, content.to_s end end end end