You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by bo...@apache.org on 2011/11/11 18:12:29 UTC

svn commit: r1200964 - in /buildr/trunk: CHANGELOG lib/buildr/java/pom.rb lib/buildr/packaging/artifact.rb spec/packaging/artifact_spec.rb

Author: boisvert
Date: Fri Nov 11 17:12:28 2011
New Revision: 1200964

URL: http://svn.apache.org/viewvc?rev=1200964&view=rev
Log:
Buildr.transitive() now accepts hash with :scopes, :optional and :scopes_transitive parameters

Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/lib/buildr/java/pom.rb
    buildr/trunk/lib/buildr/packaging/artifact.rb
    buildr/trunk/spec/packaging/artifact_spec.rb

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=1200964&r1=1200963&r2=1200964&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Fri Nov 11 17:12:28 2011
@@ -1,4 +1,6 @@
 1.4.7 (Pending)
+* Added:  Buildr.transitive() now accepts hash with :scopes, :optional and
+          :scopes_transitive parameters
 * Added:  Improved scala file change detection
           (to avoid recompiling  unnecessarily)
 * Change: Scala 2.9.1 is now default

Modified: buildr/trunk/lib/buildr/java/pom.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/java/pom.rb?rev=1200964&r1=1200963&r2=1200964&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/java/pom.rb (original)
+++ buildr/trunk/lib/buildr/java/pom.rb Fri Nov 11 17:12:28 2011
@@ -75,27 +75,36 @@ module Buildr
 
     # :call-seq:
     #   dependencies(scopes?) => artifacts
+    #   dependencies(:scopes = [:runtime, :test, ...], :optional = true) => artifacts
     #
     # Returns list of required dependencies as specified by the POM. You can specify which scopes
     # to use (e.g. "compile", "runtime"); use +nil+ for dependencies with unspecified scope.
-    # The default scopes are +nil+, "compile" and "runtime" (aka SCOPES_WE_USE).
-    def dependencies(scopes = SCOPES_WE_USE)
-      #try to cache dependencies also
+    # The default scopes are +nil+, "compile" and "runtime" (aka SCOPES_WE_USE) and no optional dependencies.
+    # Specifying optional = true will return all optional dependencies matching the given scopes.
+    def dependencies(options = {})
+      # backward compatibility
+      options = { :scopes => options } if Array === options
+
+      # support symbols, but don't fidget with nil
+      options[:scopes] = (options[:scopes] || SCOPES_WE_USE).map { |s| s.to_s if s }
+
+      # try to cache dependencies also
       @depends_for_scopes ||= {}
-      unless depends = @depends_for_scopes[scopes]
+      unless depends = @depends_for_scopes[options]
         declared = project["dependencies"].first["dependency"] rescue nil
-        depends = (declared || []).reject { |dep| value_of(dep["optional"]) =~ /true/ }.
-          map { |dep|
+        depends = (declared || [])
+        depends = depends.reject { |dep| value_of(dep["optional"]) =~ /true/ } unless options[:optional]
+        depends = depends.map { |dep|
             spec = pom_to_hash(dep, properties)
             apply = managed(spec)
             spec = apply.merge(spec) if apply
 
-            #calculate transitive dependencies
-            if scopes.include?(spec[:scope])
+            # calculate transitive dependencies
+            if options[:scopes].include?(spec[:scope])
               spec.delete(:scope)
 
               exclusions = dep["exclusions"]["exclusion"] rescue nil
-              transitive_deps = POM.load(spec).dependencies(SCOPES_TRANSITIVE)
+              transitive_deps = POM.load(spec).dependencies(options[:scopes_transitive] || SCOPES_TRANSITIVE)
               transitive_deps = transitive_deps.reject{|dep|
                 exclusions.find {|ex| dep.index("#{dep['groupdId'].first}:#{dep['artifactId'].first}:") == 0}
               } if exclusions
@@ -103,8 +112,7 @@ module Buildr
               [Artifact.to_spec(spec)] + transitive_deps
             end
           }.flatten.compact #.uniq_by{|spec| art = spec.split(':'); "#{art[0]}:#{art[1]}"}
-
-        @depends_for_scopes[scopes] = depends
+        @depends_for_scopes[options] = depends
       end
       depends
     end

Modified: buildr/trunk/lib/buildr/packaging/artifact.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/packaging/artifact.rb?rev=1200964&r1=1200963&r2=1200964&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/packaging/artifact.rb (original)
+++ buildr/trunk/lib/buildr/packaging/artifact.rb Fri Nov 11 17:12:28 2011
@@ -811,23 +811,29 @@ module Buildr
     end
   end
 
-  def transitive(*specs)
-    specs.flatten.inject([]) do |set, spec|
+  def transitive(*args)
+    options = Hash === args.last ? args.pop : {}
+    dep_opts = {
+      :scopes   => options[:scopes] || [nil, "compile", "runtime", "provided"],
+      :optional => options[:optional]
+    }
+    specs = args.flatten
+    specs.inject([]) do |set, spec|
       case spec
       when /([^:]+:){2,4}/ # A spec as opposed to a file name.
         artifact = artifact(spec)
         set |= [artifact] unless artifact.type == :pom
-        set |= POM.load(artifact.pom).dependencies.map { |spec| artifact(spec) }
+        set |= POM.load(artifact.pom).dependencies(dep_opts).map { |spec| artifact(spec) }
       when Hash
-        set |= [transitive(spec)]
+        set |= [transitive(spec, options)]
       when String # Must always expand path.
-        set |= transitive(file(File.expand_path(spec)))
+        set |= transitive(file(File.expand_path(spec)), options)
       when Project
-        set |= transitive(spec.packages)
+        set |= transitive(spec.packages, options)
       when Rake::Task
-        set |= spec.respond_to?(:to_spec) ? transitive(spec.to_spec) : [spec]
+        set |= spec.respond_to?(:to_spec) ? transitive(spec.to_spec, options) : [spec]
       when Struct
-        set |= transitive(spec.values)
+        set |= transitive(spec.values, options)
       else
         fail "Invalid artifact specification in: #{specs.to_s}"
       end

Modified: buildr/trunk/spec/packaging/artifact_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/packaging/artifact_spec.rb?rev=1200964&r1=1200963&r2=1200964&view=diff
==============================================================================
--- buildr/trunk/spec/packaging/artifact_spec.rb (original)
+++ buildr/trunk/spec/packaging/artifact_spec.rb Fri Nov 11 17:12:28 2011
@@ -1040,6 +1040,13 @@ describe Buildr, '#transitive' do
       <version>8.4</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <artifactId>jlib-optional</artifactId>
+      <groupId>jlib</groupId>
+      <version>1.4</version>
+      <scope>runtime</scope>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 </project>
 XML
@@ -1118,6 +1125,16 @@ XML
   it 'should bring artifact and transitive depenencies' do
     transitive(@transitive).should eql(artifacts(@transitive, @complex, @simple - [@provided]))
   end
+
+  it 'should filter dependencies based on :scopes argument' do
+    specs = [@complex, 'saxon:saxon-dom:jar:8.4']
+    transitive(@complex, :scopes => [:runtime]).should eql(specs.map { |spec| artifact(spec) })
+  end
+
+  it 'should filter dependencies based on :optional argument' do
+    specs = [@complex, 'saxon:saxon-dom:jar:8.4', 'jlib:jlib-optional:jar:1.4']
+    transitive(@complex, :scopes => [:runtime], :optional => true).should eql(specs.map { |spec| artifact(spec) })
+  end
 end
 
 def modified?(old_mtime, spec)