You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by vb...@apache.org on 2008/03/26 01:09:17 UTC

svn commit: r641084 - in /incubator/buildr/trunk: lib/java/artifact_namespace.rb spec/artifact_namespace_spec.rb

Author: vborja
Date: Tue Mar 25 17:09:16 2008
New Revision: 641084

URL: http://svn.apache.org/viewvc?rev=641084&view=rev
Log:
More specs for ArtifactNamespace

Modified:
    incubator/buildr/trunk/lib/java/artifact_namespace.rb
    incubator/buildr/trunk/spec/artifact_namespace_spec.rb

Modified: incubator/buildr/trunk/lib/java/artifact_namespace.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/artifact_namespace.rb?rev=641084&r1=641083&r2=641084&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/java/artifact_namespace.rb (original)
+++ incubator/buildr/trunk/lib/java/artifact_namespace.rb Tue Mar 25 17:09:16 2008
@@ -13,7 +13,6 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-require 'core/environment'
 require 'java/artifact'
 
 module Buildr
@@ -61,7 +60,7 @@
   #         log4j:      log4j:log4j:jar:1.2.15
   #         groovy:     org.codehaus.groovy:groovy:jar:1.5.4
   #       
-  #       # open Buildr::XMLBeans namespace
+  #       # module/addon namespace
   #       Buildr::XMLBeans: 
   #         xmlbeans: 2.2
   #
@@ -175,6 +174,34 @@
       
       alias_method :for, :instance
     end
+
+    def initialize(name) #:nodoc:
+      @name = name
+      clear
+    end
+
+    attr_reader :name
+    include Enumerable
+    
+    # Return an array of artifacts defined for use
+    def to_a(include_parents = false)
+      seen = {}
+      registry = self
+      while registry
+        registry.instance_variable_get(:@using).each_pair do |key, spec|
+          spec = spec(key) unless Hash == spec
+          name = Artifact.to_spec(spec.merge(:version => '-'))
+          seen[name] = Buildr.artifact(spec) unless seen.key?(name)
+        end
+        registry = include_parents ? registry.parent : nil
+      end
+      seen.values
+    end
+
+    def each(&block)
+      to_a.each(&block)
+    end
+
     
     # Set the parent namespace
     def parent=(parent)
@@ -196,14 +223,7 @@
       end
       @parent.tap(&block)
     end
-
-    def initialize(name) #:nodoc:
-      @name = name
-      clear
-    end
-
-    attr_reader :name
-
+    
     # Clear internal requirements map
     def clear
       @using = {}
@@ -211,26 +231,7 @@
       @requires = {}
     end
     
-    # Return artifacts defined for use
-    def used(include_parents = false)
-      seen = {}
-      registry = self
-      while registry
-        registry.instance_variable_get(:@using).each_pair do |key, spec|
-          spec = spec(key) unless Hash == spec
-          name = Artifact.to_spec(spec.merge(:version => '-'))
-          seen[name] = Buildr.artifact(spec) unless seen.key?(name)
-        end
-        registry = include_parents ? registry.parent : nil
-      end
-      seen.values
-    end
 
-    def each(&block)
-      used(false).each(&block)
-    end
-    include Enumerable
-    
     # Return the named artifacts from this namespace hierarchy
     def only(*names)
       names = @requires.keys if names.empty?
@@ -262,7 +263,7 @@
         end
         spec
       elsif parent
-        parent.spec(name)
+        parent.spec(name) || parent.spec(@aliases[name])
       end
     end
 
@@ -301,18 +302,20 @@
     #
     # In adition to comparition operators, artifact requirements support logic operators
     # 
-    # * ( expr )       -- Parenthesis group expressions
-    # * not( expr )    -- Negation
-    # * expr and expr  -- Logical and
-    # * expr or  expr  -- Logical or
+    # * ( expr )     -- expression grouping
+    # * !( expr )    -- negate nested expr, parens are required
+    # * expr & expr  -- Logical and
+    # * expr |  expr -- Logical or
     #
     # Requirements defined on parent namespaces, are inherited by
     # their childs, this means that when a specific version is selected for use
     # on a sub-namespace, validation will be performed by checking the parent requirement.
     #
     #   artifacts('one') do 
-    #     need :bar => 'foo:bar:jar:1.0 or ~>1.0'
-    #     need 'foo:baz:jar:>1.2 & <1.3 & not(>=1.2.5 | <=1.2.6)'
+    #     need :bar => 'foo:bar:jar:1.0 | ~>1.0'
+    #     need 'foo:baz:jar:>1.2 & <1.3 & !(>=1.2.5 | <=1.2.6)'
+    #     # default logical operator is &, so this can be written as
+    #     need 'foo:baz:jar:>1.2 <1.3 !(>=1.2.5 | <=1.2.6)'
     #   end
     # 
     #   artifacts('one:two') do 
@@ -388,30 +391,19 @@
 
     # See examples for #need and #default methods.
     def use(*specs)
-      set = lambda do |k, v|
-        k = k.to_sym
-        unless @setting_defaults && (@using[k] || @using[@aliases[k]])
-          @using[k] = v
-        end
-      end
       specs.flatten.each do |spec|
         if (Hash === spec || Struct === spec) &&
            (spec.keys & ActsAsArtifact::ARTIFACT_ATTRIBUTES).empty?
           spec.each_pair do |k, v|
             if VersionRequirement.version?(v)
-              fail_unless_satisfied(requirement(k), {:version => v})
-              set.call(k, v)
+              set(k, v)
             else
               spec = Artifact.to_hash(v)
-              fail_unless_satisfied(requirement(k), spec)
-              set.call(k, spec)
+              set(k, spec)
             end
           end
         else
-          spec = Artifact.to_hash(spec)
-          unvers = Artifact.to_spec(spec.merge(:version => '-')).to_sym
-          fail_unless_satisfied(requirement(unvers), spec)
-          set.call(unvers, spec)
+          set(spec, Artifact.to_hash(spec))
         end
       end
       self
@@ -457,6 +449,23 @@
           raise "Artifact attributes mismatch, " + 
             "required #{Artifact.to_spec(req)}, got #{Artifact.to_spec(spec)}"
         end
+      end
+    end
+
+    def set(name, spec)
+      name = normalize_name(name)
+      needed = requirement(name)
+      candidate = VersionRequirement.version?(spec) ? {:version => spec } : spec
+      if @setting_defaults
+        current = spec(name)
+        satisfied = current && needed && needed[:version].satisfied_by?(current[:version])
+        unless satisfied || (!needed && current)
+          fail_unless_satisfied(needed, candidate)
+          @using[name] = spec
+        end
+      else
+        fail_unless_satisfied(needed, candidate)
+        @using[name] = spec
       end
     end
 

Modified: incubator/buildr/trunk/spec/artifact_namespace_spec.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/artifact_namespace_spec.rb?rev=641084&r1=641083&r2=641084&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/artifact_namespace_spec.rb (original)
+++ incubator/buildr/trunk/spec/artifact_namespace_spec.rb Tue Mar 25 17:09:16 2008
@@ -32,7 +32,7 @@
     artifacts(nil) { |ns| ns.name.should == ArtifactNamespace::ROOT }
   end
 
-  it 'should return an array responding to :namespace if no block given' do
+  it 'should return an array responding to #namespace if no block given' do
     ary = artifacts
     ary.should be_kind_of(Array)
     ary.should respond_to(:namespace)
@@ -78,50 +78,83 @@
   end
 
   it 'should register a requirement with the #need method' do
-    artifacts do |root|
-      root.need 'foo:bar:jar:>1.0'
-      root.should_not be_satisfied('foo:bar:jar:?')
-      root.need :bat => 'foo:bat:jar:>1.0'
-      root.should_not be_satisfied(:bat)
+    artifacts do |ns|
+      ns.need 'foo:bar:jar:>1.0'
+      ns.should_not be_satisfied('foo:bar:jar:?')
+      ns.need :bat => 'foo:bat:jar:>1.0'
+      ns.should_not be_satisfied(:bat)
     end
   end
 
   it 'should register an artifact with the #use method' do
-    artifacts do |root|
-      root.use :bat => 'bat:bar:jar:2.0'
-      root.spec(:bat).should_not be_nil
-      root.spec(:bat)[:version].should == '2.0'
-      root.spec(:bat)[:group].should == 'bat'
+    artifacts do |ns|
+      ns.use :bat => 'bat:bar:jar:2.0'
+      ns.spec(:bat).should_not be_nil
+      ns.spec(:bat)[:version].should == '2.0'
+      ns.spec(:bat)[:group].should == 'bat'
       artifacts(:bat).should_not be_empty
-      root.use 'bat:man:jar:3.0'
-      root.spec('bat:man:jar:?')[:version].should == '3.0'
+      ns.use 'bat:man:jar:3.0'
+      ns.spec('bat:man:jar:?')[:version].should == '3.0'
       artifacts('bat:man:jar:?').should_not be_empty
     end
   end
 
   it 'should set defaults witht the #default method' do 
-    artifacts do |root|
-      root.use :bar => 'foo:bar:jar:2.0'
-      root.spec(:bar).should_not be_nil
-      root.default :bar => 'foo:bar:jar:1.9'
-      root.spec(:bar)[:version].should == '2.0'
-      root.default :baz => 'foo:baz:jar:1.8'
-      root.spec(:baz)[:version].should == '1.8'
+    artifacts do
+      use :bar => 'foo:bar:jar:2.0'
+      spec(:bar)[:version].should == '2.0'
+      default :bar => 'foo:bar:jar:1.9'
+      spec(:bar)[:version].should == '2.0'
+      default :baz => 'foo:baz:jar:1.8'
+      spec(:baz)[:version].should == '1.8'
+      need :bat => 'foo:bat:jar:>1.0'
+      default :bat => 'foo:bat:jar:1.5'
+      spec(:bat)[:version].should == '1.5'
     end
   end
 
   it 'should complain if requirement is not met' do 
-    artifacts do |root|
-      root.need :foo => 'foo:bar:jar:>3.0'
-      lambda { root.use :foo => '2.0' }.should raise_error(Exception)
-      lambda { root.use :foo => 'foo:baz:jar:3.1' }.should raise_error(Exception)
-      root.use :foo => '3.2'
-      root.spec(:foo)[:version].should == '3.2'
-      root.use :bat => '2.0'
-      lambda { root.need :bat => 'foo:bat:jar:>2.0' }.should raise_error(Exception)
-      root.use :baz => 'foo:ban:jar:2.1'
-      lambda { root.need :baz => 'foo:baz:jar:>2.0' }.should raise_error(Exception)
+    artifacts do |ns|
+      ns.need :foo => 'foo:bar:jar:>3.0'
+      lambda { ns.use :foo => '2.0' }.should raise_error(Exception)
+      lambda { ns.use :foo => 'foo:baz:jar:3.1' }.should raise_error(Exception)
+      ns.use :foo => '3.2'
+      ns.spec(:foo)[:version].should == '3.2'
+      ns.use :bat => '2.0'
+      lambda { ns.need :bat => 'foo:bat:jar:>2.0' }.should raise_error(Exception)
+      ns.use :baz => 'foo:ban:jar:2.1'
+      lambda { ns.need :baz => 'foo:baz:jar:>2.0' }.should raise_error(Exception)
     end
+  end
+
+  it 'should be populated with ArtifactNamespace.load given a hash of hashes' do 
+    hash = {}
+    hash[nil] = Hash[:foo => 'foo:bar:jar:1.0']
+    hash['one'] = Hash[:foo => 'foo:bar:jar:2.0']
+    ArtifactNamespace.load(hash)
+    artifacts[nil].spec(:foo)[:version].should == '1.0'
+    artifacts['one'].spec(:foo)[:version].should == '2.0'
+  end
+
+  it 'should select compatible artifacts defined on parent namespaces' do
+    artifacts do
+      use :foo => 'foo:bar:jar:3.0'
+      use :baz => 'foo:baz:jar:1.0'
+      use 'foo:bat:jar:1.5.6.7'
+    end
+    module Some
+      Buildr.artifacts(self) do
+        need :foo => 'foo:bar:jar:>=1.0'
+        need :baz => 'foo:baz:jar:>=2.0'
+        need :bat => 'foo:bat:jar:>1.5 & <1.6'
+        default :foo => '2.0'
+        default :baz => '2.0'
+        default :bat => '1.5.5'
+      end
+    end
+    artifacts[Some].spec(:foo)[:version].should == '3.0'
+    artifacts[Some].spec(:baz)[:version].should == '2.0'
+    artifacts[Some].spec(:bat)[:version].should == '1.5.6.7'
   end
   
 end