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:57 UTC

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

Author: vborja
Date: Tue Mar 25 17:09:55 2008
New Revision: 641085

URL: http://svn.apache.org/viewvc?rev=641085&view=rev
Log:
Fix to version_requirements_spec, more artifact_namespace_specs

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

Modified: incubator/buildr/trunk/lib/java/artifact.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/artifact.rb?rev=641085&r1=641084&r2=641085&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/java/artifact.rb (original)
+++ incubator/buildr/trunk/lib/java/artifact.rb Tue Mar 25 17:09:55 2008
@@ -618,8 +618,9 @@
         set |= artifacts(spec.values)
       when Symbol
         name = spec
-        spec = ArtifactNamespace.instance.spec(name)
-        raise "No artifact found by name #{name.inspect}" unless spec
+        ns = ArtifactNamespace.instance
+        spec = ns.spec(name)
+        raise "No artifact found by name #{name.inspect} on #{ns.name} namespace" unless spec
         set |= [artifact(spec)]
       else
         fail "Invalid artifact specification in #{specs.inspect}"

Modified: incubator/buildr/trunk/lib/java/artifact_namespace.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/artifact_namespace.rb?rev=641085&r1=641084&r2=641085&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/java/artifact_namespace.rb (original)
+++ incubator/buildr/trunk/lib/java/artifact_namespace.rb Tue Mar 25 17:09:55 2008
@@ -31,7 +31,7 @@
   # to the Buildr.artifacts method (see ArtifactNamespace.instance):
   #
   #    -- buildfile --
-  #    # open the root namespace, equivalent to +artifacts.namespace(nil)+
+  #    # open the root namespace, equivalent to artifacts[nil]
   #    artifacts do |ns|
   #       # later referenced by name
   #       ns.use :spring => 'org.springframework:spring:jar:2.5'
@@ -42,10 +42,14 @@
   #    # specify the xmlbeans version to use:
   #    artifacts[Buildr::XMLBeans][:xmlbeans] = '2.2'
   #   
-  #    # select some artifacts by their ruby symbols or spec
+  #    # Artifacts can be referenced using their name or spec
+  #    define 'moo_proj' { compile.with artifacts[self][:spring] }
+  #    # Buildr.artifact can take ruby symbols searching them on the current namespace
   #    define 'foo_proj' { compile.with :log4j, :'asm:asm:jar:-', 'some:other:jar:2.0' }
-  #    # or get all used artifacts for the project namespace
-  #    define 'bar_proj' { compile.with artifacts[self].used }
+  #    # or get all used artifacts for the current project
+  #    define 'bar_proj' { compile.with artifacts[project].values }
+  #    # or get all used artifacts in namespace (including parents)
+  #    define 'full_proj' { compile.with artifacts[project].values(true) }
   # 
   # The ArtifactNamespace.load method can be used to populate your
   # namespaces from a hash of hashes, like your profile yaml in the
@@ -87,22 +91,22 @@
       
       # :call-seq:
       #   artifacts[numeric] -> artifact
-      #   artifacts[scope] -> namespace
+      #   artifacts[name] -> namespace
       #
-      # Extends the regular Array#[] so that non numeric
-      # indices are scope names, returning the corresponding
-      # namespace
+      # Extends the regular Array#[] so for non-numeric
+      # indices it returns the corresponding ArtifactNamespace
+      #
+      #   ary = artifacts('some:art:jar:1.0')
+      #   ary[0] -> Artifact
+      #   ary[nil] -> root ArtifactNamespace
+      #   ary['some:name:space'] -> 'some:name:space' ArtifactNamespace
       def [](idx)
-        if Numeric === idx
-          super
-        else
-          namespace(idx)
-        end
+        Numeric === idx ? super : namespace(idx)
       end
       
       # :call-seq:
       #   artifacts.namespace -> namespace
-      #   artifacts.namespace(scope) -> namespace
+      #   artifacts.namespace(name) -> namespace
       def namespace(*a, &b)
         ArtifactNamespace.instance(*a, &b)
       end
@@ -135,12 +139,8 @@
       #
       #   -- buildfile --
       #   ArtifactNamespace.load(Buildr.profile['artifacts'])
-      def load(namespace_hash)
-        if Hash === namespace_hash
-          namespace_hash.each_pair do |name, uses|
-            instance(name).use(uses)
-          end
-        end
+      def load(namespaces = {})
+        namespaces.each_pair { |name, uses| instance(name).use(uses) }
       end
       
       # Forget all previously declared namespaces.
@@ -150,9 +150,9 @@
 
       # :call-seq:
       #   Buildr.artifacts { |ns| ... } -> namespace
-      #   Buildr.artifacts(scope) { |ns| ... } -> namespace
+      #   Buildr.artifacts(name) { |ns| ... } -> namespace
       # 
-      # Obtain the namespace for the given +scope+ or for the currently
+      # Obtain the namespace for the given +name+ or for the currently
       # running project. If a block is given, the namespace is yielded to it.
       def instance(name = nil, &block)
         case name
@@ -161,8 +161,11 @@
         when nil then
           task = Thread.current[:rake_chain]
           task = task.instance_variable_get(:@value) if task
-          name = task ? task.scope : Rake.application.current_scope
-          name = name.join(':')
+          name = case task
+                 when Project then task.name
+                 when Rake::Task then task.scope.join(':')
+                 when nil then Rake.application.current_scope.join(':')
+                 end
         end
         name = name.to_s.split(/:{2,}/).join(':')
         name = ROOT if name.to_s.blank?
@@ -184,13 +187,13 @@
     include Enumerable
     
     # Return an array of artifacts defined for use
-    def to_a(include_parents = false)
+    def values(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 => '-'))
+          name = normalize_name(spec)
           seen[name] = Buildr.artifact(spec) unless seen.key?(name)
         end
         registry = include_parents ? registry.parent : nil
@@ -198,10 +201,14 @@
       seen.values
     end
 
-    def each(&block)
-      to_a.each(&block)
+    # Return the named artifacts from this namespace hierarchy
+    def values_at(*names)
+      names.map { |name| Buildr.artifact(spec(name)) }
     end
 
+    def each(&block)
+      values.each(&block)
+    end
     
     # Set the parent namespace
     def parent=(parent)
@@ -232,13 +239,6 @@
     end
     
 
-    # Return the named artifacts from this namespace hierarchy
-    def only(*names)
-      names = @requires.keys if names.empty?
-      ary = names.map { |name| Buildr.artifact(spec(name)) }
-      ary.size == 1 ? ary.first : ary
-    end
-
     # Test if named requirement has been satisfied
     def satisfied?(name)
       req, spec = requirement(name), spec(name)
@@ -303,9 +303,9 @@
     # In adition to comparition operators, artifact requirements support logic operators
     # 
     # * ( expr )     -- expression grouping
-    # * !( expr )    -- negate nested expr, parens are required
-    # * expr & expr  -- Logical and
-    # * expr |  expr -- Logical or
+    # * !( expr )    -- Negate nested expr, parens are required
+    # * expr & expr  -- Logical and, default operator. ">1 <2" is equivalent to ">1 & <2"
+    # * expr | expr  -- Logical or, lower precedence than &
     #
     # Requirements defined on parent namespaces, are inherited by
     # their childs, this means that when a specific version is selected for use
@@ -356,13 +356,13 @@
     # Specify default version if no previous one has been selected.
     # This method is useful mainly for plugin/addon writers, allowing
     # their users to override the artifact version to be used.
-    # Plugin/Addon writers need to document the +scope+ used by their
+    # Plugin/Addon writers need to document the +namespace+ used by their
     # addon, which can be simply an string or a module name.
     # 
     # Suppose we are writing the Foo::Addon module
     #
     #   module Foo::Addon
-    #     artifacts(self) do # scope is the module name => "Foo::Addon"
+    #     artifacts(self) do # namespace is the module name => "Foo::Addon"
     #       need :bar => 'foo:bar:jar:>2.0', # suppose bar is used at load time
     #            :baz => 'foo:baz:jar:>3.0'  # used when Foo::Addon.baz called
     #       default :bar => '2.5', :baz => '3.5'
@@ -410,10 +410,20 @@
     end
     
     alias_method :<<, :use
-    alias_method :[], :only
     
     # :call-seq:
-    #   artifacts.namespace('foo:bar')[:commons_net] = '1.0'
+    #   artifacts['name:space'][:an_art] -> Artifact
+    #   artifacts['name:space']['an:spec:jar:-'] -> Artifact
+    #   artifacts['name:space'][:an_art, 'an:spec:jar:-', 'more_art'] -> [Artifact]
+    #
+    # Alias for #values_at
+    def [](name, *rest)
+      values = values_at(name, *rest)
+      rest.empty? ? values.first : values
+    end
+    
+    # :call-seq:
+    #   artifacts['name:space'][:artifact_name] = '1.0'
     # 
     # Selects an artifact version for usage, with hash like syntax.
     # 
@@ -471,6 +481,8 @@
 
   end # ArtifactNamespace
 
+  #
+  # See ArtifactNamespace#need
   class VersionRequirement
     
     CMP_PROCS = Gem::Requirement::OPS.dup
@@ -480,14 +492,20 @@
     VER_CHARS = '\w\.'
     
     class << self
+      # is +str+ a version string?
       def version?(str)
         /^\s*[#{VER_CHARS}]+\s*$/ === str
       end
       
+      # is +str+ a version requirement?
       def requirement?(str)
         /[#{BOOL_CHARS}#{CMP_CHARS}\(\)]/ === str
       end
       
+      # :call-seq:
+      #    VersionRequirement.create(" >1 <2 !(1.5) ") -> requirement
+      #
+      # parse the +str+ requirement 
       def create(str)
         instance_eval normalize(str)
       rescue StandardError => e
@@ -537,14 +555,20 @@
       end
     end
 
-    def initialize(op, *requirements)
+    def initialize(op, *requirements) #:nodoc:
       @op, @requirements = op, requirements
     end
 
-    def has_alternatives?
+    # Is this object a composed requirement?
+    #   VersionRequirement.create('1').composed? -> false
+    #   VersionRequirement.create('1 | 2').composed? -> true
+    #   VersionRequirement.create('1 & 2').composed? -> true
+    def composed?
       requirements.size > 1
     end
 
+    # Return the last requirement on this object having a 
+    # = operator.
     def default
       default = nil
       requirements.reverse.find do |r|
@@ -559,6 +583,7 @@
       default
     end
 
+    # Test if this requirement can be satisfied by +version+
     def satisfied_by?(version)
       return false unless version
       unless version.kind_of?(Gem::Version)
@@ -577,14 +602,19 @@
       negative ? !result : result
     end
 
+    # Either modify the current requirement (if it's already an or operation)
+    # or create a new requirement
     def |(other)
       operation(:|, other)
     end
 
+    # Either modify the current requirement (if it's already an and operation)
+    # or create a new requirement
     def &(other)
       operation(:&, other)
     end
-
+    
+    # return the parsed expression
     def to_s
       str = requirements.map(&:to_s).join(" " + @op.to_s + " ").to_s
       str = "( " + str + " )" if negative || requirements.size > 1
@@ -629,7 +659,7 @@
       end
       result = nil
       methods = search_methods
-      if requirement.has_alternatives?
+      if requirement.composed?
         until result || methods.empty?
           method = methods.shift
           type = method.keys.first

Modified: incubator/buildr/trunk/spec/artifact_namespace_spec.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/artifact_namespace_spec.rb?rev=641085&r1=641084&r2=641085&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/artifact_namespace_spec.rb (original)
+++ incubator/buildr/trunk/spec/artifact_namespace_spec.rb Tue Mar 25 17:09:55 2008
@@ -16,19 +16,19 @@
 
 require File.join(File.dirname(__FILE__), 'spec_helpers')
 
-describe Buildr::ArtifactNamespace, 'obtained from Buildr#artifacts' do 
+describe Buildr, '#artifacts' do 
   before :each do 
     ArtifactNamespace.clear
   end
   
-  it 'should tap root namespace if called outside a project definition' do
+  it 'should tap root namespace if given a block and called outside a project definition' do
     expected = be_kind_of(ArtifactNamespace)
     artifacts { |ns| ns.name.should == ArtifactNamespace::ROOT }
     artifacts { |ns| ns.should expected }.should expected
     artifacts { self.should expected }
   end
 
-  it 'should tap root namespace when given nil' do
+  it 'should tap root namespace when given a block and nil argument' do
     artifacts(nil) { |ns| ns.name.should == ArtifactNamespace::ROOT }
   end
 
@@ -39,8 +39,32 @@
     ary.namespace.should be_kind_of(ArtifactNamespace)
     ary.namespace.name.should === ArtifactNamespace::ROOT
   end
+  
+  it 'should return an array whose non-numeric indices are namespaces' do
+    ary = artifacts("foo:bar:jar:1.0")
+    ary.should be_kind_of(Array)
+    ary[0].should be_kind_of(Artifact)
+    ary[nil].should be_kind_of(ArtifactNamespace)
+    ary[nil].name.should === ArtifactNamespace::ROOT
+    ary['some:addon'].should be_kind_of(ArtifactNamespace)
+    ary['some:addon'].name.should === 'some:addon'.intern
+  end
 
-  it 'should return the namespace for the current project' do
+  it 'should take symbols, searching for them on the current namespace' do 
+    artifacts[nil][:bar] = 'foo:bar:jar:1.0'
+    artifacts[nil].use 'foo:moo:jar:2.0'
+    artifacts[nil][:bat] = 'foo:bar:jar:0.9'
+    define 'foo' do
+      artifacts[project][:baz] = 'foo:baz:jar:1.0'
+      compile.with :bar, :baz, :'foo:moo:jar:-', 'some:other:jar:1.0'
+      classpath = compile.classpath.map(&:to_spec)
+      classpath.should include('foo:baz:jar:1.0', 'foo:bar:jar:1.0', 
+                               'foo:moo:jar:2.0', 'some:other:jar:1.0')
+      classpath.should_not include('foo:bar:jar:0.9')
+    end
+  end
+
+  it 'should return the namespace for the current project if given a block' do
     define 'foo' do
       artifacts { |ns| ns.name.should == name.intern }
       define 'bar' do 
@@ -49,7 +73,7 @@
     end
   end
 
-  it 'should take the first argument as the scope when given a block' do 
+  it 'should take the first argument as the namespace when given a block' do 
     artifacts('moo') { |ns| ns.name.should == :moo }
     artifacts(:mooo) { |ns| ns.name.should == :mooo }
     a = Module.new { def self.name; "Some::Module::A"; end }
@@ -156,5 +180,5 @@
     artifacts[Some].spec(:baz)[:version].should == '2.0'
     artifacts[Some].spec(:bat)[:version].should == '1.5.6.7'
   end
-  
+
 end

Modified: incubator/buildr/trunk/spec/version_requirement_spec.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/version_requirement_spec.rb?rev=641085&r1=641084&r2=641085&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/version_requirement_spec.rb (original)
+++ incubator/buildr/trunk/spec/version_requirement_spec.rb Tue Mar 25 17:09:55 2008
@@ -52,8 +52,8 @@
     should_satisfy '>1.0', %w(1.0.1), %w(1 1.0 0.1)
     should_satisfy '>=1.0', %w(1.0.1 1 1.0), %w(0.9)
 
-    should_satisfy '<1.0', %w(0.9, 0.9.9), %w(1 1.0 1.1 2)
-    should_satisfy '<=1.0', %w(0.9, 0.9.9 1 1.0), %w(1.1 2)
+    should_satisfy '<1.0', %w(0.9 0.9.9), %w(1 1.0 1.1 2)
+    should_satisfy '<=1.0', %w(0.9 0.9.9 1 1.0), %w(1.1 2)
     
     should_satisfy '~> 1.2.3', %w(1.2.3 1.2.3.4 1.2.4), %w(1.2.1 0.9 1.4 2)
   end