buildr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From as...@apache.org
Subject svn commit: r705723 - in /incubator/buildr/trunk/lib/buildr: core/checks.rb core/util.rb packaging.rb packaging/archive.rb packaging/gems.rb packaging/tar.rb packaging/zip.rb packaging/ziptask.rb
Date Fri, 17 Oct 2008 19:20:26 GMT
Author: assaf
Date: Fri Oct 17 12:20:26 2008
New Revision: 705723

URL: http://svn.apache.org/viewvc?rev=705723&view=rev
Log:
Speedup load time by lazy-loading RubyZip.  Doing this requires a bit of re-org.
ArchiveTask moves form zip.rb to archive.rb; it's the base task for Zip, Tar and Gem.
ZipTask and UnzipTask introduced in ziptask.rb.
A seperate file, zip.rb is used to autoload RubyZip and monkey patch it (required for efficiently sorting entries, other things).
Extensions to ArchiveTask and Zip that previously existed in checks.rb are now located in archive.rb and zip.rb, respectively.

Added:
    incubator/buildr/trunk/lib/buildr/packaging/archive.rb
      - copied, changed from r705722, incubator/buildr/trunk/lib/buildr/packaging/zip.rb
    incubator/buildr/trunk/lib/buildr/packaging/ziptask.rb
Modified:
    incubator/buildr/trunk/lib/buildr/core/checks.rb
    incubator/buildr/trunk/lib/buildr/core/util.rb
    incubator/buildr/trunk/lib/buildr/packaging.rb
    incubator/buildr/trunk/lib/buildr/packaging/gems.rb
    incubator/buildr/trunk/lib/buildr/packaging/tar.rb
    incubator/buildr/trunk/lib/buildr/packaging/zip.rb

Modified: incubator/buildr/trunk/lib/buildr/core/checks.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/core/checks.rb?rev=705723&r1=705722&r2=705723&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/core/checks.rb (original)
+++ incubator/buildr/trunk/lib/buildr/core/checks.rb Fri Oct 17 12:20:26 2008
@@ -15,10 +15,7 @@
 
 
 require 'buildr/core/project'
-require 'buildr/packaging/zip'
-#require 'test/unit'
-#require 'spec/matchers'
-#require 'spec/expectations'
+autoload :Spec, 'spec'
 
 
 module Buildr
@@ -252,131 +249,6 @@
 end
 
 
-module Zip #:nodoc:
-  class ZipEntry
-
-    # :call-seq:
-    #   exist() => boolean
-    #
-    # Returns true if this entry exists.
-    def exist?()
-      Zip::ZipFile.open(zipfile) { |zip| zip.file.exist?(@name) }
-    end
-
-    # :call-seq:
-    #   empty?() => boolean
-    #
-    # Returns true if this entry is empty.
-    def empty?()
-      Zip::ZipFile.open(zipfile) { |zip| zip.file.read(@name) }.empty?
-    end
-
-    # :call-seq:
-    #   contain(patterns*) => boolean
-    #
-    # Returns true if this ZIP file entry matches against all the arguments. An argument may be
-    # a string or regular expression.
-    def contain?(*patterns)
-      content = Zip::ZipFile.open(zipfile) { |zip| zip.file.read(@name) }
-      patterns.map { |pattern| Regexp === pattern ? pattern : Regexp.new(Regexp.escape(pattern.to_s)) }.
-        all? { |pattern| content =~ pattern }
-    end
-
-  end
-end
-
-
-class Buildr::ArchiveTask
-
-  class Path #:nodoc:
-
-    # :call-seq:
-    #   exist() => boolean
-    #
-    # Returns true if this path exists. This only works if the path has any entries in it,
-    # so exist on path happens to be the opposite of empty.
-    def exist?()
-      !entries.empty?
-    end
-
-    # :call-seq:
-    #   empty?() => boolean
-    #
-    # Returns true if this path is empty (has no other entries inside).
-    def empty?()
-      entries.all? { |entry| entry.empty? }
-    end
-
-    # :call-seq:
-    #   contain(file*) => boolean
-    #
-    # Returns true if this ZIP file path contains all the specified files. You can use relative
-    # file names and glob patterns (using *, **, etc).
-    def contain?(*files)
-      files.all? { |file| entries.detect { |entry| File.fnmatch(file, entry.to_s, File::FNM_PATHNAME) } }
-    end
-
-    # :call-seq:
-    #   entry(name) => ZipEntry
-    #
-    # Returns a ZIP file entry. You can use this to check if the entry exists and its contents,
-    # for example:
-    #   package(:jar).path("META-INF").entry("LICENSE").should contain(/Apache Software License/)
-    def entry(name)
-      root.entry("#{@path}#{name}")
-    end
-
-  protected
-
-    def entries() #:nodoc:
-      return root.entries unless @path
-      @entries ||= root.entries.inject([]) { |selected, entry|
-        selected << entry.name.sub(@path, "") if entry.name.index(@path) == 0
-        selected
-      }
-    end
-
-  end
-
-  # :call-seq:
-  #   empty?() => boolean
-  #
-  # Returns true if this ZIP file is empty (has no other entries inside).
-  def empty?()
-    path("").empty
-  end
-
-  # :call-seq:
-  #   contain(file*) => boolean
-  #
-  # Returns true if this ZIP file contains all the specified files. You can use absolute
-  # file names and glob patterns (using *, **, etc).
-  def contain?(*files)
-    path("").contain?(*files)
-  end
-
-end
-
-
-class Buildr::ZipTask #:nodoc:
-
-  # :call-seq:
-  #   entry(name) => Entry
-  #
-  # Returns a ZIP file entry. You can use this to check if the entry exists and its contents,
-  # for example:
-  #   package(:jar).entry("META-INF/LICENSE").should contain(/Apache Software License/)
-  def entry(entry_name)
-    ::Zip::ZipEntry.new(name, entry_name)
-  end
-
-  def entries() #:nodoc:
-    @entries ||= Zip::ZipFile.open(name) { |zip| zip.entries }
-  end
-
-end
-
-
 class Buildr::Project
   include Buildr::Checks
 end

Modified: incubator/buildr/trunk/lib/buildr/core/util.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/core/util.rb?rev=705723&r1=705722&r2=705723&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/core/util.rb (original)
+++ incubator/buildr/trunk/lib/buildr/core/util.rb Fri Oct 17 12:20:26 2008
@@ -16,6 +16,7 @@
 
 require 'rbconfig'
 autoload :Pathname, 'pathname'
+autoload :Tempfile, 'tempfile'
 autoload :YAML, 'yaml'
 autoload :REXML, 'rexml/document'
 gem 'xml-simple' ; autoload :XmlSimple, 'xmlsimple'

Modified: incubator/buildr/trunk/lib/buildr/packaging.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/packaging.rb?rev=705723&r1=705722&r2=705723&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/packaging.rb (original)
+++ incubator/buildr/trunk/lib/buildr/packaging.rb Fri Oct 17 12:20:26 2008
@@ -14,8 +14,10 @@
 # the License.
 
 
-require 'buildr/packaging/zip'
-require 'buildr/packaging/tar'
 require 'buildr/packaging/artifact'
 require 'buildr/packaging/package'
+require 'buildr/packaging/archive'
+require 'buildr/packaging/ziptask'
+require 'buildr/packaging/tar'
 require 'buildr/packaging/gems'
+autoload :Zip, 'buildr/packaging/zip'

Copied: incubator/buildr/trunk/lib/buildr/packaging/archive.rb (from r705722, incubator/buildr/trunk/lib/buildr/packaging/zip.rb)
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/packaging/archive.rb?p2=incubator/buildr/trunk/lib/buildr/packaging/archive.rb&p1=incubator/buildr/trunk/lib/buildr/packaging/zip.rb&r1=705722&r2=705723&rev=705723&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/packaging/zip.rb (original)
+++ incubator/buildr/trunk/lib/buildr/packaging/archive.rb Fri Oct 17 12:20:26 2008
@@ -14,11 +14,6 @@
 # the License.
 
 
-$LOADED_FEATURES.unshift 'ftools' if RUBY_VERSION >= '1.9.0' # Required to properly load RubyZip under Ruby 1.9
-require 'zip/zip'
-require 'zip/zipfilesystem'
-
-
 module Buildr
 
   # Base class for ZipTask, TarTask and other archives.
@@ -127,7 +122,7 @@
       end
 
       # Returns all the source files.
-      def sources() #:nodoc:
+      def sources #:nodoc:
         @sources.map{ |source| source.call }.flatten
       end
 
@@ -135,7 +130,43 @@
         @actions.each { |action| action.call(file_map) }
       end
 
-      def to_s()
+      # :call-seq:
+      #   exist => boolean
+      #
+      # Returns true if this path exists. This only works if the path has any entries in it,
+      # so exist on path happens to be the opposite of empty.
+      def exist?
+        !entries.empty?
+      end
+
+      # :call-seq:
+      #   empty? => boolean
+      #
+      # Returns true if this path is empty (has no other entries inside).
+      def empty?
+        entries.all? { |entry| entry.empty? }
+      end
+
+      # :call-seq:
+      #   contain(file*) => boolean
+      #
+      # Returns true if this ZIP file path contains all the specified files. You can use relative
+      # file names and glob patterns (using *, **, etc).
+      def contain?(*files)
+        files.all? { |file| entries.detect { |entry| File.fnmatch(file, entry.to_s, File::FNM_PATHNAME) } }
+      end
+
+      # :call-seq:
+      #   entry(name) => ZipEntry
+      #
+      # Returns a ZIP file entry. You can use this to check if the entry exists and its contents,
+      # for example:
+      #   package(:jar).path("META-INF").entry("LICENSE").should contain(/Apache Software License/)
+      def entry(name)
+        root.entry("#{@path}#{name}")
+      end
+
+      def to_s
         @path
       end
 
@@ -172,8 +203,17 @@
         @excludes.any? { |exclude| File.fnmatch(exclude, file, File::FNM_PATHNAME) }
       end
 
+      def entries #:nodoc:
+        return root.entries unless @path
+        @entries ||= root.entries.inject([]) { |selected, entry|
+          selected << entry.name.sub(@path, "") if entry.name.index(@path) == 0
+          selected
+        }
+      end
+
     end
 
+
     class Merge
       def initialize(expanders)
         @expanders = expanders
@@ -361,10 +401,10 @@
     end
 
     # :call-seq:
-    #   root() => ArchiveTask
+    #   root => ArchiveTask
     #
     # Call this on an archive to return itself, and on a path to return the archive.
-    def root()
+    def root
       self
     end
 
@@ -394,7 +434,7 @@
       super
     end
     
-    def needed?() #:nodoc:
+    def needed? #:nodoc:
       return true unless File.exist?(name)
       # You can do something like:
       #   include('foo', :path=>'foo').exclude('foo/bar', path=>'foo').
@@ -412,6 +452,23 @@
       File.stat(name).mtime < (most_recent || Rake::EARLY) || super
     end
 
+    # :call-seq:
+    #   empty? => boolean
+    #
+    # Returns true if this ZIP file is empty (has no other entries inside).
+    def empty?
+      path("").empty
+    end
+
+    # :call-seq:
+    #   contain(file*) => boolean
+    #
+    # Returns true if this ZIP file contains all the specified files. You can use absolute
+    # file names and glob patterns (using *, **, etc).
+    def contain?(*files)
+      path("").contain?(*files)
+    end
+
   protected
 
     # Adds a prepare block. These blocks are called early on for adding more content to
@@ -428,295 +485,5 @@
 
   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|
-  #     task.include 'srcs'
-  #     task.include 'README', 'LICENSE'
-  #   end
-  #
-  # See Buildr#zip and ArchiveTask.
-  class ZipTask < ArchiveTask
-
-    # Compression leve for this Zip.
-    attr_accessor :compression_level
-
-    def initialize(*args) #:nodoc:
-      self.compression_level = Zlib::NO_COMPRESSION
-      super
-    end
-
-  private
-
-    def create_from(file_map)
-      Zip::ZipOutputStream.open name do |zip|
-        seen = {}
-        mkpath = lambda do |dir|
-          unless dir == '.' || seen[dir]
-            mkpath.call File.dirname(dir)
-            zip.put_next_entry(dir + '/', compression_level)
-            seen[dir] = true
-          end
-        end
-
-        file_map.each do |path, content|
-          mkpath.call File.dirname(path)
-          if content.respond_to?(:call)
-            zip.put_next_entry(path, compression_level)
-            content.call zip
-          elsif content.nil? || File.directory?(content.to_s)
-            mkpath.call path
-          else
-            zip.put_next_entry(path, compression_level)
-            File.open content.to_s, 'rb' do |is|
-              while data = is.read(4096)
-                zip << data
-              end
-            end
-          end
-        end
-      end
-    end
-
-  end
-
-
-  # :call-seq:
-  #    zip(file) => ZipTask
-  #
-  # 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|
-  #     task.include 'srcs'
-  #     task.include 'README', 'LICENSE'
-  #   end
-  def zip(file)
-    ZipTask.define_task(file)
-  end
-
-
-  # An object for unzipping a file into a target directory. You can tell it to include
-  # or exclude only specific files and directories, and also to map files from particular
-  # paths inside the zip file into the target directory. Once ready, call #extract.
-  #
-  # Usually it is more convenient to create a file task for extracting the zip file
-  # (see #unzip) and pass this object as a prerequisite to other tasks.
-  #
-  # See Buildr#unzip.
-  class Unzip
-
-    # The zip file to extract.
-    attr_accessor :zip_file
-    # The target directory to extract to.
-    attr_accessor :target
-
-    # Initialize with hash argument of the form target=>zip_file.
-    def initialize(args)
-      @target, arg_names, @zip_file = Buildr.application.resolve_args([args])
-      @paths = {}
-    end
-
-    # :call-seq:
-    #   extract()
-    #
-    # Extract the zip file into the target directory.
-    #
-    # You can call this method directly. However, if you are using the #unzip method,
-    # it creates a file task for the target directory: use that task instead as a
-    # prerequisite. For example:
-    #   build unzip(dir=>zip_file)
-    # Or:
-    #   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(self, nil)
-      end
-
-      # Otherwise, empty unzip creates target as a file when touching.
-      mkpath target.to_s, :verbose=>false
-      Zip::ZipFile.open(zip_file.to_s) do |zip|
-        entries = zip.collect
-        @paths.each do |path, patterns|
-          patterns.map(entries).each do |dest, entry|
-            next if entry.directory?
-            dest = File.expand_path(dest, target.to_s)
-            trace "Extracting #{dest}"
-            mkpath File.dirname(dest), :verbose=>false rescue nil
-            entry.extract(dest) { true }
-          end
-        end
-      end
-      # Let other tasks know we updated the target directory.
-      touch target.to_s, :verbose=>false
-    end
-
-    # :call-seq:
-    #   include(*files) => self
-    #   include(*files, :path=>name) => self
-    #
-    # Include all files that match the patterns and returns self.
-    #
-    # Use include if you only want to unzip some of the files, by specifying
-    # them instead of using exclusion. You can use #include in combination
-    # with #exclude.
-    def include(*files)
-      if Hash === files.last
-        from_path(files.pop[:path]).include *files
-      else
-        from_path(nil).include *files
-      end
-      self
-    end
-    alias :add :include
-
-    # :call-seq:
-    #   exclude(*files) => self
-    #
-    # Exclude all files that match the patterns and return self.
-    #
-    # Use exclude to unzip all files except those that match the pattern.
-    # You can use #exclude in combination with #include.
-    def exclude(*files)
-      if Hash === files.last
-        from_path(files.pop[:path]).exclude *files
-      else
-        from_path(nil).exclude *files
-      end
-      self
-    end
-
-    # :call-seq:
-    #   from_path(name) => Path
-    #
-    # Allows you to unzip from a path. Returns an object you can use to
-    # specify which files to include/exclude relative to that path.
-    # Expands the file relative to that path.
-    #
-    # For example:
-    #   unzip(Dir.pwd=>'test.jar').from_path('etc').include('LICENSE')
-    # will unzip etc/LICENSE into ./LICENSE.
-    #
-    # 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(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(unzip, path)
-        @unzip = unzip
-        if path
-          @path = path[-1] == ?/ ? path : path + '/'
-        else
-          @path = ''
-        end
-      end
-
-      # See UnzipTask#include
-      def include(*files) #:doc:
-        @include ||= []
-        @include |= files
-        self
-      end
-
-      # See UnzipTask#exclude
-      def exclude(*files) #:doc:
-        @exclude ||= []
-        @exclude |= files
-        self
-      end
-
-      def map(entries)
-        includes = @include || ['**/*']
-        excludes = @exclude || []
-        entries.inject({}) do |map, entry|
-          if entry.name =~ /^#{@path}/
-            short = entry.name.sub(@path, '')
-            if includes.any? { |pat| File.fnmatch(pat, short, File::FNM_PATHNAME) } &&
-               !excludes.any? { |pat| File.fnmatch(pat, short, File::FNM_PATHNAME) }
-              map[short] = entry
-            end
-          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:
-  #    unzip(to_dir=>zip_file) => Zip
-  #
-  # Creates a task that will unzip a file into the target directory. The task name
-  # is the target directory, the prerequisite is the file to unzip.
-  #
-  # This method creates a file task to expand the zip file. It returns an Unzip object
-  # that specifies how the file will be extracted. You can include or exclude specific
-  # files from within the zip, and map to different paths.
-  #
-  # The Unzip object's to_s method return the path to the target directory, so you can
-  # use it as a prerequisite. By keeping the Unzip object separate from the file task,
-  # you overlay additional work on top of the file task.
-  #
-  # For example:
-  #   unzip('all'=>'test.zip')
-  #   unzip('src'=>'test.zip').include('README', 'LICENSE') 
-  #   unzip('libs'=>'test.zip').from_path('libs')
-  def unzip(args)
-    target, arg_names, zip_file = Buildr.application.resolve_args([args])
-    task = file(File.expand_path(target.to_s)=>zip_file)
-    Unzip.new(task=>zip_file).tap do |setup|
-      task.enhance { setup.extract }
-    end
-  end
 
 end
-
-
-module Zip #:nodoc:
-
-  class ZipCentralDirectory #:nodoc:
-    # Patch to add entries in alphabetical order.
-    def write_to_stream(io)
-      offset = io.tell
-      @entrySet.sort { |a,b| a.name <=> b.name }.each { |entry| entry.write_c_dir_entry(io) }
-      write_e_o_c_d(io, offset)
-    end
-  end
-
-end 

Modified: incubator/buildr/trunk/lib/buildr/packaging/gems.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/packaging/gems.rb?rev=705723&r1=705722&r2=705723&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/packaging/gems.rb (original)
+++ incubator/buildr/trunk/lib/buildr/packaging/gems.rb Fri Oct 17 12:20:26 2008
@@ -15,7 +15,7 @@
 
 
 require 'buildr/packaging/package'
-require 'buildr/packaging/zip'
+require 'buildr/packaging/archive'
 gem 'rubyforge' ; autoload :RubyForge, 'rubyforge'
 Gem.autoload :Package, 'rubygems/package'
 

Modified: incubator/buildr/trunk/lib/buildr/packaging/tar.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/packaging/tar.rb?rev=705723&r1=705722&r2=705723&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/packaging/tar.rb (original)
+++ incubator/buildr/trunk/lib/buildr/packaging/tar.rb Fri Oct 17 12:20:26 2008
@@ -14,7 +14,7 @@
 # the License.
 
 
-require 'buildr/packaging/zip'
+require 'buildr/packaging/archive'
 gem 'archive-tar-minitar' ; autoload :Archive, 'archive/tar/minitar'
 
 

Modified: incubator/buildr/trunk/lib/buildr/packaging/zip.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/packaging/zip.rb?rev=705723&r1=705722&r2=705723&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/packaging/zip.rb (original)
+++ incubator/buildr/trunk/lib/buildr/packaging/zip.rb Fri Oct 17 12:20:26 2008
@@ -19,704 +19,46 @@
 require 'zip/zipfilesystem'
 
 
-module Buildr
-
-  # Base class for ZipTask, TarTask and other archives.
-  class ArchiveTask < Rake::FileTask
-
-    # Which files go where. All the rules for including, excluding and merging files
-    # are handled by this object.
-    class Path #:nodoc:
-
-      # Returns the archive from this path.
-      attr_reader :root
-      
-      def initialize(root, path)
-        @root = root
-        @path = path.empty? ? path : "#{path}/"
-        @includes = FileList[]
-        @excludes = []
-        # Expand source files added to this path.
-        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|
-            unless excluded?(path)
-              if File.directory?(path)
-                in_directory path do |file, rel_path|
-                  dest = "#{@path}#{rel_path}"
-                  trace "Adding #{dest}"
-                  file_map[dest] = file
-                end
-              else
-                trace "Adding #{@path}#{File.basename(path)}"
-                file_map["#{@path}#{File.basename(path)}"] = path
-              end
-            end
-          end
-        end
-      end
-
-      # :call-seq:
-      #   include(*files) => self
-      #   include(*files, :path=>path) => self
-      #   include(file, :as=>name) => self
-      #   include(:from=>path) => self
-      #   include(*files, :merge=>true) => self
-      def include(*args)
-        options = args.pop if Hash === args.last
-        files = args.flatten
-
-        if options.nil? || options.empty?
-          @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
-          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.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.size == 1
-          files.each { |file| merge file }
-        else
-          raise "Unrecognized option #{options.keys.join(', ')}"
-        end
-        self
-      end
-      alias :add :include
-      alias :<< :include
-
-      # :call-seq:
-      #   exclude(*files) => self
-      def 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
-      #   merge(*files, :path=>name) => Merge
-      def merge(*args)
-        options = Hash === args.last ? args.pop : {}
-        files = args.flatten
-        rake_check_options options, :path
-        raise ArgumentError, "Expected at least one file to merge" if files.empty?
-        path = options[:path] || @path
-        expanders = files.collect do |file|
-          @sources << proc { file.to_s }
-          expander = ZipExpander.new(file)
-          @actions << proc { |file_map| expander.expand(file_map, path) }
-          expander
-        end
-        Merge.new(expanders)
-      end
-
-      # Returns a Path relative to this one.
-      def path(path)
-        return self if path.nil?
-        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
-      end
-
-      def add_files(file_map) #:nodoc:
-        @actions.each { |action| action.call(file_map) }
-      end
-
-      def to_s()
-        @path
-      end
-
-    protected
-
-      def include_as(source, as)
-        @sources << proc { source }
-        @actions << proc do |file_map|
-          file = source.to_s
-          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('/')}"
-                trace "Adding #{dest}"
-                file_map[dest] = file
-              end
-            else
-              trace "Adding #{@path}#{as}"
-              file_map["#{@path}#{as}"] = file
-            end
-          end
-        end
-      end
-
-      def in_directory(dir)
-        prefix = Regexp.new('^' + Regexp.escape(File.dirname(dir) + File::SEPARATOR))
-        Util.recursive_with_dot_files(dir).reject { |file| excluded?(file) }.
-          each { |file| yield file, file.sub(prefix, '') }
-      end
-
-      def excluded?(file)
-        @excludes.any? { |exclude| File.fnmatch(exclude, file, File::FNM_PATHNAME) }
-      end
-
-    end
-
-    class Merge
-      def initialize(expanders)
-        @expanders = expanders
-      end
-
-      def include(*files)
-        @expanders.each { |expander| expander.include(*files) }
-        self
-      end
-      alias :<< :include
-
-      def exclude(*files)
-        @expanders.each { |expander| expander.exclude(*files) }
-        self
-      end
-    end
-
-
-    # Extend one Zip file into another.
-    class ZipExpander #:nodoc:
-
-      def initialize(zip_file)
-        @zip_file = zip_file.to_s
-        @includes = []
-        @excludes = []
-      end
-
-      def include(*files)
-        @includes |= files
-        self
-      end
-      alias :<< :include
-
-      def exclude(*files)
-        @excludes |= files
-        self
-      end
-
-      def expand(file_map, path)
-        @includes = ['**/*'] if @includes.empty?
-        Zip::ZipFile.open(@zip_file) do |source|
-          source.entries.reject { |entry| entry.directory? }.each do |entry|
-            if @includes.any? { |pattern| File.fnmatch(pattern, entry.name, File::FNM_PATHNAME) } &&
-               !@excludes.any? { |pattern| File.fnmatch(pattern, entry.name, File::FNM_PATHNAME) }
-              dest = path =~ /^\/?$/ ? entry.name : Util.relative_path(path + "/" + entry.name)
-              trace "Adding #{dest}"
-              file_map[dest] = lambda { |output| output.write source.read(entry) }
-            end
-          end
-        end
-      end
-
-    end
-
-
-    def initialize(*args) #:nodoc:
-      super
-      clean
-
-      # Make sure we're the last enhancements, so other enhancements can add content.
-      enhance do
-        @file_map = {}
-        enhance do
-          send 'create' if respond_to?(:create)
-          # We're here because the archive file does not exist, or one of the files is newer than the archive contents;
-          # 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 do |name, object|
-              @file_map[name] = nil unless name.empty?
-              object.add_files(@file_map)
-            end
-            create_from @file_map
-          rescue
-            rm name, :verbose=>false rescue nil
-            raise
-          end
-        end
-      end
-    end
-
-    # :call-seq:
-    #   clean => self
-    # 
-    # Removes all previously added content from this archive. 
-    # Use this method if you want to remove default content from a package.
-    # For example, package(:jar) by default includes compiled classes and resources,
-    # using this method, you can create an empty jar and afterwards add the
-    # desired content to it.
-    # 
-    #    package(:jar).clean.include path_to('desired/content')
-    def clean
-      @paths = { '' => Path.new(self, '') }
-      @prepares = []
-      self
-    end
-
-    # :call-seq:
-    #   include(*files) => self
-    #   include(*files, :path=>path) => self
-    #   include(file, :as=>name) => self
-    #   include(:from=>path) => self
-    #   include(*files, :merge=>true) => self
-    #
-    # Include files in this archive, or when called on a path, within that path. Returns self.
-    #
-    # The first form accepts a list of files, directories and glob patterns and adds them to the archive.
-    # For example, to include the file foo, directory bar (including all files in there) and all files under baz:
-    #   zip(..).include('foo', 'bar', 'baz/*')
-    #
-    # The second form is similar but adds files/directories under the specified path. For example,
-    # to add foo as bar/foo:
-    #   zip(..).include('foo', :path=>'bar')
-    # The :path option is the same as using the path method:
-    #   zip(..).path('bar').include('foo')
-    # All other options can be used in combination with the :path option.
-    #
-    # The third form adds a file or directory under a different name. For example, to add the file foo under the
-    # name bar:
-    #   zip(..).include('foo', :as=>'bar')
-    #
-    # The fourth form adds the contents of a directory using the directory as a prerequisite:
-    #   zip(..).include(:from=>'foo')
-    # Unlike <code>include('foo')</code> it includes the contents of the directory, not the directory itself.
-    # Unlike <code>include('foo/*')</code>, it uses the directory timestamp for dependency management.
-    #
-    # 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[''].include *files
-      self
-    end 
-    alias :add :include
-    alias :<< :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[''].exclude *files
-      self
-    end 
-
-    # :call-seq:
-    #   merge(*files) => Merge
-    #   merge(*files, :path=>name) => Merge
-    #
-    # Merges another archive into this one by including the individual files from the merged archive.
-    #
-    # 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[''].merge *files
-    end 
-
-    # :call-seq:
-    #   path(name) => Path
-    #
-    # Returns a path object. Use the path object to include files under a path, for example, to include
-    # the file 'foo' as 'bar/foo':
-    #   zip(..).path('bar').include('foo')
-    #
-    # 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[''] if name.nil?
-      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
-    #
-    # Call this on an archive to return itself, and on a path to return the archive.
-    def root()
-      self
-    end
-
-    # :call-seq:
-    #   with(options) => self
-    #
-    # Passes options to the task and returns self. Some tasks support additional options, for example,
-    # the WarTask supports options like :manifest, :libs and :classes.
-    #
-    # For example:
-    #   package(:jar).with(:manifest=>'MANIFEST_MF')
-    def with(options)
-      options.each do |key, value|
-        begin
-          send "#{key}=", value
-        rescue NoMethodError
-          raise ArgumentError, "#{self.class.name} does not support the option #{key}"
-        end
-      end
-      self
-    end
-
-    def invoke_prerequisites(args, chain) #:nodoc:
-      @prepares.each { |prepare| prepare.call(self) }
-      @prepares.clear
-      @prerequisites |= @paths.collect { |name, path| path.sources }.flatten
-      super
-    end
-    
-    def needed?() #:nodoc:
-      return true unless File.exist?(name)
-      # You can do something like:
-      #   include('foo', :path=>'foo').exclude('foo/bar', path=>'foo').
-      #     include('foo/bar', :path=>'foo/bar')
-      # This will play havoc if we handled all the prerequisites together
-      # under the task, so instead we handle them individually for each path.
-      #
-      # 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) ? Util.recursive_with_dot_files(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
-
-    # Adds a prepare block. These blocks are called early on for adding more content to
-    # the archive, before invoking prerequsities. Anything you add here will be invoked
-    # as a prerequisite and used to determine whether or not to generate this archive.
-    # In contrast, enhance blocks are evaluated after it was decided to create this archive.
-    def prepare(&block)
-      @prepares << block
-    end
-
-    def []=(key, value) #:nodoc:
-      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|
-  #     task.include 'srcs'
-  #     task.include 'README', 'LICENSE'
-  #   end
-  #
-  # See Buildr#zip and ArchiveTask.
-  class ZipTask < ArchiveTask
-
-    # Compression leve for this Zip.
-    attr_accessor :compression_level
-
-    def initialize(*args) #:nodoc:
-      self.compression_level = Zlib::NO_COMPRESSION
-      super
-    end
-
-  private
-
-    def create_from(file_map)
-      Zip::ZipOutputStream.open name do |zip|
-        seen = {}
-        mkpath = lambda do |dir|
-          unless dir == '.' || seen[dir]
-            mkpath.call File.dirname(dir)
-            zip.put_next_entry(dir + '/', compression_level)
-            seen[dir] = true
-          end
-        end
+module Zip #:nodoc:
 
-        file_map.each do |path, content|
-          mkpath.call File.dirname(path)
-          if content.respond_to?(:call)
-            zip.put_next_entry(path, compression_level)
-            content.call zip
-          elsif content.nil? || File.directory?(content.to_s)
-            mkpath.call path
-          else
-            zip.put_next_entry(path, compression_level)
-            File.open content.to_s, 'rb' do |is|
-              while data = is.read(4096)
-                zip << data
-              end
-            end
-          end
-        end
-      end
+  class ZipCentralDirectory #:nodoc:
+    # Patch to add entries in alphabetical order.
+    def write_to_stream(io)
+      offset = io.tell
+      @entrySet.sort { |a,b| a.name <=> b.name }.each { |entry| entry.write_c_dir_entry(io) }
+      write_e_o_c_d(io, offset)
     end
-
-  end
-
-
-  # :call-seq:
-  #    zip(file) => ZipTask
-  #
-  # 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|
-  #     task.include 'srcs'
-  #     task.include 'README', 'LICENSE'
-  #   end
-  def zip(file)
-    ZipTask.define_task(file)
   end
 
 
-  # An object for unzipping a file into a target directory. You can tell it to include
-  # or exclude only specific files and directories, and also to map files from particular
-  # paths inside the zip file into the target directory. Once ready, call #extract.
-  #
-  # Usually it is more convenient to create a file task for extracting the zip file
-  # (see #unzip) and pass this object as a prerequisite to other tasks.
-  #
-  # See Buildr#unzip.
-  class Unzip
-
-    # The zip file to extract.
-    attr_accessor :zip_file
-    # The target directory to extract to.
-    attr_accessor :target
-
-    # Initialize with hash argument of the form target=>zip_file.
-    def initialize(args)
-      @target, arg_names, @zip_file = Buildr.application.resolve_args([args])
-      @paths = {}
-    end
+  class ZipEntry
 
     # :call-seq:
-    #   extract()
-    #
-    # Extract the zip file into the target directory.
+    #   exist() => boolean
     #
-    # You can call this method directly. However, if you are using the #unzip method,
-    # it creates a file task for the target directory: use that task instead as a
-    # prerequisite. For example:
-    #   build unzip(dir=>zip_file)
-    # Or:
-    #   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(self, nil)
-      end
-
-      # Otherwise, empty unzip creates target as a file when touching.
-      mkpath target.to_s, :verbose=>false
-      Zip::ZipFile.open(zip_file.to_s) do |zip|
-        entries = zip.collect
-        @paths.each do |path, patterns|
-          patterns.map(entries).each do |dest, entry|
-            next if entry.directory?
-            dest = File.expand_path(dest, target.to_s)
-            trace "Extracting #{dest}"
-            mkpath File.dirname(dest), :verbose=>false rescue nil
-            entry.extract(dest) { true }
-          end
-        end
-      end
-      # Let other tasks know we updated the target directory.
-      touch target.to_s, :verbose=>false
+    # Returns true if this entry exists.
+    def exist?()
+      Zip::ZipFile.open(zipfile) { |zip| zip.file.exist?(@name) }
     end
 
     # :call-seq:
-    #   include(*files) => self
-    #   include(*files, :path=>name) => self
-    #
-    # Include all files that match the patterns and returns self.
+    #   empty?() => boolean
     #
-    # Use include if you only want to unzip some of the files, by specifying
-    # them instead of using exclusion. You can use #include in combination
-    # with #exclude.
-    def include(*files)
-      if Hash === files.last
-        from_path(files.pop[:path]).include *files
-      else
-        from_path(nil).include *files
-      end
-      self
+    # Returns true if this entry is empty.
+    def empty?()
+      Zip::ZipFile.open(zipfile) { |zip| zip.file.read(@name) }.empty?
     end
-    alias :add :include
 
     # :call-seq:
-    #   exclude(*files) => self
+    #   contain(patterns*) => boolean
     #
-    # Exclude all files that match the patterns and return self.
-    #
-    # Use exclude to unzip all files except those that match the pattern.
-    # You can use #exclude in combination with #include.
-    def exclude(*files)
-      if Hash === files.last
-        from_path(files.pop[:path]).exclude *files
-      else
-        from_path(nil).exclude *files
-      end
-      self
-    end
-
-    # :call-seq:
-    #   from_path(name) => Path
-    #
-    # Allows you to unzip from a path. Returns an object you can use to
-    # specify which files to include/exclude relative to that path.
-    # Expands the file relative to that path.
-    #
-    # For example:
-    #   unzip(Dir.pwd=>'test.jar').from_path('etc').include('LICENSE')
-    # will unzip etc/LICENSE into ./LICENSE.
-    #
-    # 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(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(unzip, path)
-        @unzip = unzip
-        if path
-          @path = path[-1] == ?/ ? path : path + '/'
-        else
-          @path = ''
-        end
-      end
-
-      # See UnzipTask#include
-      def include(*files) #:doc:
-        @include ||= []
-        @include |= files
-        self
-      end
-
-      # See UnzipTask#exclude
-      def exclude(*files) #:doc:
-        @exclude ||= []
-        @exclude |= files
-        self
-      end
-
-      def map(entries)
-        includes = @include || ['**/*']
-        excludes = @exclude || []
-        entries.inject({}) do |map, entry|
-          if entry.name =~ /^#{@path}/
-            short = entry.name.sub(@path, '')
-            if includes.any? { |pat| File.fnmatch(pat, short, File::FNM_PATHNAME) } &&
-               !excludes.any? { |pat| File.fnmatch(pat, short, File::FNM_PATHNAME) }
-              map[short] = entry
-            end
-          end
-          map
-        end
-      end
-
-      # Documented in Unzip.
-      def root()
-        @unzip
-      end
-
-      # The target directory to extract to.
-      def target()
-        @unzip.target
-      end
-
+    # Returns true if this ZIP file entry matches against all the arguments. An argument may be
+    # a string or regular expression.
+    def contain?(*patterns)
+      content = Zip::ZipFile.open(zipfile) { |zip| zip.file.read(@name) }
+      patterns.map { |pattern| Regexp === pattern ? pattern : Regexp.new(Regexp.escape(pattern.to_s)) }.
+        all? { |pattern| content =~ pattern }
     end
 
   end
-
-  # :call-seq:
-  #    unzip(to_dir=>zip_file) => Zip
-  #
-  # Creates a task that will unzip a file into the target directory. The task name
-  # is the target directory, the prerequisite is the file to unzip.
-  #
-  # This method creates a file task to expand the zip file. It returns an Unzip object
-  # that specifies how the file will be extracted. You can include or exclude specific
-  # files from within the zip, and map to different paths.
-  #
-  # The Unzip object's to_s method return the path to the target directory, so you can
-  # use it as a prerequisite. By keeping the Unzip object separate from the file task,
-  # you overlay additional work on top of the file task.
-  #
-  # For example:
-  #   unzip('all'=>'test.zip')
-  #   unzip('src'=>'test.zip').include('README', 'LICENSE') 
-  #   unzip('libs'=>'test.zip').from_path('libs')
-  def unzip(args)
-    target, arg_names, zip_file = Buildr.application.resolve_args([args])
-    task = file(File.expand_path(target.to_s)=>zip_file)
-    Unzip.new(task=>zip_file).tap do |setup|
-      task.enhance { setup.extract }
-    end
-  end
-
 end
-
-
-module Zip #:nodoc:
-
-  class ZipCentralDirectory #:nodoc:
-    # Patch to add entries in alphabetical order.
-    def write_to_stream(io)
-      offset = io.tell
-      @entrySet.sort { |a,b| a.name <=> b.name }.each { |entry| entry.write_c_dir_entry(io) }
-      write_e_o_c_d(io, offset)
-    end
-  end
-
-end 

Added: incubator/buildr/trunk/lib/buildr/packaging/ziptask.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/packaging/ziptask.rb?rev=705723&view=auto
==============================================================================
--- incubator/buildr/trunk/lib/buildr/packaging/ziptask.rb (added)
+++ incubator/buildr/trunk/lib/buildr/packaging/ziptask.rb Fri Oct 17 12:20:26 2008
@@ -0,0 +1,313 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership.  The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+
+require 'buildr/packaging/archive'
+
+
+module Buildr
+
+  # 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|
+  #     task.include 'srcs'
+  #     task.include 'README', 'LICENSE'
+  #   end
+  #
+  # See Buildr#zip and ArchiveTask.
+  class ZipTask < ArchiveTask
+
+    # Compression leve for this Zip.
+    attr_accessor :compression_level
+
+    def initialize(*args) #:nodoc:
+      self.compression_level = Zlib::NO_COMPRESSION
+      super
+    end
+
+    # :call-seq:
+    #   entry(name) => Entry
+    #
+    # Returns a ZIP file entry. You can use this to check if the entry exists and its contents,
+    # for example:
+    #   package(:jar).entry("META-INF/LICENSE").should contain(/Apache Software License/)
+    def entry(entry_name)
+      ::Zip::ZipEntry.new(name, entry_name)
+    end
+
+    def entries #:nodoc:
+      @entries ||= Zip::ZipFile.open(name) { |zip| zip.entries }
+    end
+
+  private
+
+    def create_from(file_map)
+      Zip::ZipOutputStream.open name do |zip|
+        seen = {}
+        mkpath = lambda do |dir|
+          unless dir == '.' || seen[dir]
+            mkpath.call File.dirname(dir)
+            zip.put_next_entry(dir + '/', compression_level)
+            seen[dir] = true
+          end
+        end
+
+        file_map.each do |path, content|
+          mkpath.call File.dirname(path)
+          if content.respond_to?(:call)
+            zip.put_next_entry(path, compression_level)
+            content.call zip
+          elsif content.nil? || File.directory?(content.to_s)
+            mkpath.call path
+          else
+            zip.put_next_entry(path, compression_level)
+            File.open content.to_s, 'rb' do |is|
+              while data = is.read(4096)
+                zip << data
+              end
+            end
+          end
+        end
+      end
+    end
+
+  end
+
+
+  # :call-seq:
+  #    zip(file) => ZipTask
+  #
+  # 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|
+  #     task.include 'srcs'
+  #     task.include 'README', 'LICENSE'
+  #   end
+  def zip(file)
+    ZipTask.define_task(file)
+  end
+
+
+  # An object for unzipping a file into a target directory. You can tell it to include
+  # or exclude only specific files and directories, and also to map files from particular
+  # paths inside the zip file into the target directory. Once ready, call #extract.
+  #
+  # Usually it is more convenient to create a file task for extracting the zip file
+  # (see #unzip) and pass this object as a prerequisite to other tasks.
+  #
+  # See Buildr#unzip.
+  class Unzip
+
+    # The zip file to extract.
+    attr_accessor :zip_file
+    # The target directory to extract to.
+    attr_accessor :target
+
+    # Initialize with hash argument of the form target=>zip_file.
+    def initialize(args)
+      @target, arg_names, @zip_file = Buildr.application.resolve_args([args])
+      @paths = {}
+    end
+
+    # :call-seq:
+    #   extract
+    #
+    # Extract the zip file into the target directory.
+    #
+    # You can call this method directly. However, if you are using the #unzip method,
+    # it creates a file task for the target directory: use that task instead as a
+    # prerequisite. For example:
+    #   build unzip(dir=>zip_file)
+    # Or:
+    #   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(self, nil)
+      end
+
+      # Otherwise, empty unzip creates target as a file when touching.
+      mkpath target.to_s, :verbose=>false
+      Zip::ZipFile.open(zip_file.to_s) do |zip|
+        entries = zip.collect
+        @paths.each do |path, patterns|
+          patterns.map(entries).each do |dest, entry|
+            next if entry.directory?
+            dest = File.expand_path(dest, target.to_s)
+            trace "Extracting #{dest}"
+            mkpath File.dirname(dest), :verbose=>false rescue nil
+            entry.extract(dest) { true }
+          end
+        end
+      end
+      # Let other tasks know we updated the target directory.
+      touch target.to_s, :verbose=>false
+    end
+
+    # :call-seq:
+    #   include(*files) => self
+    #   include(*files, :path=>name) => self
+    #
+    # Include all files that match the patterns and returns self.
+    #
+    # Use include if you only want to unzip some of the files, by specifying
+    # them instead of using exclusion. You can use #include in combination
+    # with #exclude.
+    def include(*files)
+      if Hash === files.last
+        from_path(files.pop[:path]).include *files
+      else
+        from_path(nil).include *files
+      end
+      self
+    end
+    alias :add :include
+
+    # :call-seq:
+    #   exclude(*files) => self
+    #
+    # Exclude all files that match the patterns and return self.
+    #
+    # Use exclude to unzip all files except those that match the pattern.
+    # You can use #exclude in combination with #include.
+    def exclude(*files)
+      if Hash === files.last
+        from_path(files.pop[:path]).exclude *files
+      else
+        from_path(nil).exclude *files
+      end
+      self
+    end
+
+    # :call-seq:
+    #   from_path(name) => Path
+    #
+    # Allows you to unzip from a path. Returns an object you can use to
+    # specify which files to include/exclude relative to that path.
+    # Expands the file relative to that path.
+    #
+    # For example:
+    #   unzip(Dir.pwd=>'test.jar').from_path('etc').include('LICENSE')
+    # will unzip etc/LICENSE into ./LICENSE.
+    #
+    # 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(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(unzip, path)
+        @unzip = unzip
+        if path
+          @path = path[-1] == ?/ ? path : path + '/'
+        else
+          @path = ''
+        end
+      end
+
+      # See UnzipTask#include
+      def include(*files) #:doc:
+        @include ||= []
+        @include |= files
+        self
+      end
+
+      # See UnzipTask#exclude
+      def exclude(*files) #:doc:
+        @exclude ||= []
+        @exclude |= files
+        self
+      end
+
+      def map(entries)
+        includes = @include || ['**/*']
+        excludes = @exclude || []
+        entries.inject({}) do |map, entry|
+          if entry.name =~ /^#{@path}/
+            short = entry.name.sub(@path, '')
+            if includes.any? { |pat| File.fnmatch(pat, short, File::FNM_PATHNAME) } &&
+               !excludes.any? { |pat| File.fnmatch(pat, short, File::FNM_PATHNAME) }
+              map[short] = entry
+            end
+          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:
+  #    unzip(to_dir=>zip_file) => Zip
+  #
+  # Creates a task that will unzip a file into the target directory. The task name
+  # is the target directory, the prerequisite is the file to unzip.
+  #
+  # This method creates a file task to expand the zip file. It returns an Unzip object
+  # that specifies how the file will be extracted. You can include or exclude specific
+  # files from within the zip, and map to different paths.
+  #
+  # The Unzip object's to_s method return the path to the target directory, so you can
+  # use it as a prerequisite. By keeping the Unzip object separate from the file task,
+  # you overlay additional work on top of the file task.
+  #
+  # For example:
+  #   unzip('all'=>'test.zip')
+  #   unzip('src'=>'test.zip').include('README', 'LICENSE') 
+  #   unzip('libs'=>'test.zip').from_path('libs')
+  def unzip(args)
+    target, arg_names, zip_file = Buildr.application.resolve_args([args])
+    task = file(File.expand_path(target.to_s)=>zip_file)
+    Unzip.new(task=>zip_file).tap do |setup|
+      task.enhance { setup.extract }
+    end
+  end
+
+end



Mime
View raw message