You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by to...@apache.org on 2010/03/26 08:21:50 UTC

svn commit: r927714 - in /buildr/trunk: lib/buildr/core/filter.rb spec/core/common_spec.rb

Author: toulmean
Date: Fri Mar 26 07:21:50 2010
New Revision: 927714

URL: http://svn.apache.org/viewvc?rev=927714&view=rev
Log:
fix for BUILDR-407: Exclude and include patterns should support lambdas or procs

Modified:
    buildr/trunk/lib/buildr/core/filter.rb
    buildr/trunk/spec/core/common_spec.rb

Modified: buildr/trunk/lib/buildr/core/filter.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/core/filter.rb?rev=927714&r1=927713&r2=927714&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/core/filter.rb (original)
+++ buildr/trunk/lib/buildr/core/filter.rb Fri Mar 26 07:21:50 2010
@@ -199,6 +199,27 @@ module Buildr
       target.to_s
     end
 
+  protected
+  
+    # :call-seq:
+    #   pattern_match(file, pattern) => boolean
+    # 
+    # This method returns true if the file name matches the pattern.
+    # The pattern may be a String, a Regexp or a Proc.
+    #
+    def pattern_match(file, pattern)
+      case
+      when pattern.is_a?(Regexp)
+        return file.match(pattern)
+      when pattern.is_a?(String)
+        return File.fnmatch(pattern, file)
+      when pattern.is_a?(Proc)
+        return pattern.call(file)
+      else
+        raise "Cannot interpret pattern #{pattern}"
+      end
+    end
+    
   private
     def copy_map
       sources.each { |source| raise "Source directory #{source} doesn't exist" unless File.exist?(source.to_s) }
@@ -207,8 +228,8 @@ module Buildr
       sources.flatten.map(&:to_s).inject({}) do |map, source|
         files = Util.recursive_with_dot_files(source).
           map { |file| Util.relative_path(file, source) }.
-          select { |file| @include.empty? || @include.any? { |pattern| pattern.is_a?(Regexp) ? file.match(pattern) : File.fnmatch(pattern, file) } }.
-          reject { |file| @exclude.any? { |pattern| pattern.is_a?(Regexp) ? file.match(pattern) : File.fnmatch(pattern, file) } }
+          select { |file| @include.empty? || @include.any? { |pattern| pattern_match(file, pattern) } }.
+          reject { |file| @exclude.any? { |pattern| pattern_match(file, pattern) } }
         files.each do |file|
           src, dest = File.expand_path(file, source), File.expand_path(file, target.to_s)
           map[file] = src if !File.exist?(dest) || File.stat(src).mtime >= File.stat(dest).mtime

Modified: buildr/trunk/spec/core/common_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/core/common_spec.rb?rev=927714&r1=927713&r2=927714&view=diff
==============================================================================
--- buildr/trunk/spec/core/common_spec.rb (original)
+++ buildr/trunk/spec/core/common_spec.rb Fri Mar 26 07:21:50 2010
@@ -313,6 +313,11 @@ describe Buildr::Filter do
     @filter.from('src').into('target').include(/file[2|3]/).run
     Dir['target/*'].sort.should eql(['target/file2', 'target/file3'])
   end
+  
+  it 'should respond to :include with a Proc and use these inclusion patterns' do
+    @filter.from('src').into('target').include(lambda {|file| file[-1, 1].to_i%2 == 0}).run
+    Dir['target/*'].sort.should eql(['target/file2', 'target/file4'])
+  end
 
   it 'should respond to :exclude and return self' do
     @filter.exclude('file').should be(@filter)
@@ -323,10 +328,15 @@ describe Buildr::Filter do
     Dir['target/*'].sort.should eql(['target/file1', 'target/file4'])
   end
   
-  it 'should respond to :exclude  with regular expressions and use these exclusion patterns' do
+  it 'should respond to :exclude with regular expressions and use these exclusion patterns' do
     @filter.from('src').into('target').exclude(/file[2|3]/).run
     Dir['target/*'].sort.should eql(['target/file1', 'target/file4'])
   end
+  
+  it 'should respond to :exclude with a Proc and use these exclusion patterns' do
+    @filter.from('src').into('target').exclude(lambda {|file| file[-1, 1].to_i%2 == 0}).run
+    Dir['target/*'].sort.should eql(['target/file1', 'target/file3'])
+  end
 
   it 'should copy files over' do
     @filter.from('src').into('target').run