Author: vborja
Date: Tue Mar 25 17:12:05 2008
New Revision: 641089

URL: http://svn.apache.org/viewvc?rev=641089&view=rev
Log:
New syntax for ArtifactNamespace#need allowing to specify the default version,
Buildr::XMLBeans and Groovyc can serve as examples on how to specify and 
document dependencies.

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

Modified: incubator/buildr/trunk/lib/buildr/xmlbeans.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/xmlbeans.rb?rev=641089&r1=641088&r2=641089&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/xmlbeans.rb (original)
+++ incubator/buildr/trunk/lib/buildr/xmlbeans.rb Tue Mar 25 17:12:05 2008
@@ -21,20 +21,21 @@
 
   # Provides XMLBeans schema compiler. Require explicitly using <code>require 
"buildr/xmlbeans"</code>.
   #
-  # You may use ArtifactNamespace to select the artifact version used by this 
module:
-  #
   #   require 'buildr/xmlbeans'
-  #   artifacts[Buildr::XMLBeans].use :xmlbeans => '2.2.0'
   #   define 'some_proj' do 
-  #      compile_xml_beans _('xsd_dir')
+  #      compile_xml_beans _(:source, :main, :xsd) # the directory with *.xsd
   #   end
   module XMLBeans
 
-    Buildr.artifacts(self) do |ns|
-      ns.need :stax => 'stax:stax-api:jar:>=1',
-              :xmlbeans => 'org.apache.xmlbeans:xmlbeans:jar:>2'
-      ns.default :stax => '1.0.1', :xmlbeans => '2.3.0'
-    end
+    # You can use ArtifactNamespace to customize the versions of 
+    # <code>:xmlbeans</code> or <code>:stax</code> used by this module:
+    #
+    #   require 'buildr/xmlbeans'
+    #   artifacts[Buildr::XMLBeans].use :xmlbeans => '2.2.0'
+    REQUIREMENTS = {
+      'stax:stax-api:jar:>=1' => '1.0.1',
+      'org.apache.xmlbeans:xmlbeans:jar:>2' => '2.3.0'
+    }.tap { |reqs| Buildr.artifacts[self].need reqs }
     
     class << self
 

Modified: incubator/buildr/trunk/lib/java/artifact_namespace.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/artifact_namespace.rb?rev=641089&r1=641088&r2=641089&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/java/artifact_namespace.rb (original)
+++ incubator/buildr/trunk/lib/java/artifact_namespace.rb Tue Mar 25 17:12:05 
2008
@@ -19,7 +19,8 @@
 
   #
   # ArtifactNamespace allows users to control artifact versions to be
-  # used by their projects and Buildr modules/addons.
+  # used by their projects and Buildr modules/addons. See Buildr::XMLBeans
+  # as an example for modules/addon writers.
   # 
   # A namespace is a hierarchical dictionary that allows to specify
   # artifact version requirements (see ArtifactNamespace#need).
@@ -32,21 +33,21 @@
   # index the resulting array by a non-numeric argument (see AryMixin#[])
   #
   #    -- buildfile --
-  #    # open the root namespace, equivalent to artifacts[:root]
+  #    # open the current namespace, equivalent to artifacts[nil]
   #    artifacts do |ns|
-  #       # later referenced by name
-  #       ns.use :spring => 'org.springframework:spring:jar:2.5'
-  #       ns.use :log4j => 'log4j:log4j:jar:1.2.15'
+  #       # artifacts are referenced by id or spec
+  #       ns.use 'org.springframework:spring:jar:2.5'
+  #       ns.use :logging => 'log4j:log4j:jar:1.2.15'
   #    end
   #
   #    require 'buildr/xmlbeans'
   #    # specify the xmlbeans version to use:
   #    artifacts[Buildr::XMLBeans][:xmlbeans] = '2.2'
   #   
-  #    # Artifacts can be referenced using their name or spec
+  #    # Artifacts can be referenced using their id 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' }
+  #    define 'foo_proj' { compile.with :logging, :'asm:asm:jar:-', 
'some:other:jar:2.0' }
   #    # 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)
@@ -62,7 +63,7 @@
   #       # root namespace, null name
   #       ~:
   #         spring:     org.springframework:spring:jar:2.5
-  #         log4j:      log4j:log4j:jar:1.2.15
+  #         logging:    log4j:log4j:jar:1.2.15
   #         groovy:     org.codehaus.groovy:groovy:jar:1.5.4
   #       
   #       # module/addon namespace
@@ -78,9 +79,9 @@
   #   require 'buildr/xmlbeans' # will use xmlbeans-2.2
   #   require 'java/groovyc' # will find groovy 1.5.4 on global ns
   #   describe 'one' do 
-  #     compile.with :spring, :log4j   # spring-2.5, log4j-1.2.15
+  #     compile.with :spring, :logging   # spring-2.5, log4j-1.2.15
   #     describe 'oldie' do
-  #       compile.with :spring, :log4j # spring-1.0, log4j-1.2.15
+  #       compile.with :spring, :logging # spring-1.0, log4j-1.2.15
   #     end
   #   end
   # 
@@ -119,7 +120,7 @@
       #       # root namespace, null name
       #       ~:
       #         spring:     org.springframework:spring:jar:2.5
-      #         log4j:      log4j:log4j:jar:1.2.15
+      #         logging:    log4j:log4j:jar:1.2.15
       #         groovy:     org.codehaus.groovy:groovy:jar:1.5.4
       #       
       #       # open Buildr::XMLBeans namespace
@@ -241,7 +242,7 @@
     # Clear internal requirements map
     def clear
       @using = {}
-      @aliases = {}
+      @ids = {}
       @requires = {}
     end
     
@@ -256,23 +257,23 @@
     def spec(name)
       return nil unless name
       name = normalize_name(name)
-      using = @using[name] || @[EMAIL PROTECTED]
+      using = @using[name] || @[EMAIL PROTECTED]
       if Hash === using
         using.dup
       elsif using
-        spec = @requires[name] || @[EMAIL PROTECTED]
+        spec = @requires[name] || @[EMAIL PROTECTED]
         spec = spec ? spec.dup : {}
         spec[:version] = using.dup
         spec
       elsif parent
-        parent.spec(name) || parent.spec(@aliases[name])
+        parent.spec(name) || parent.spec(@ids[name])
       end
     end
 
     def requirement(name)
       return nil unless name
       name = normalize_name(name)
-      req = @requires[name] || @[EMAIL PROTECTED]
+      req = @requires[name] || @[EMAIL PROTECTED]
       if req
         req.dup
       elsif parent
@@ -283,21 +284,23 @@
     def delete(name)
       return self unless name
       name = normalize_name(name)
-      [name, @aliases[name]].each do |n|
-        @requires.delete(n); @using.delete(n); @aliases.delete(n)
+      [name, @ids[name]].each do |n|
+        @requires.delete(n); @using.delete(n); @ids.delete(n)
       end
       self
     end
 
-    # :call-seq: 
+    # :call-seq:
     #   artifacts do
-    #     need *specs
-    #     need name => spec
+    #     need *requirements
+    #     need name => requirement
+    #     need requirement => default_version
     #   end
     # 
-    # Establish an artifact dependency on the current namespace.
-    # A dependency is simply an artifact spec whose version part
-    # contains comparision operators.
+    # Establish an artifact +requirement+ on the current namespace.
+    # A +requirement+ is simply an artifact spec whose version part
+    # contains comparision operators. If no +name+ is specified, 
+    # the artifact :id attribute is used as name.
     #
     # Supported comparison operators are =, !=, >, <, >=, <= and ~>.
     # The compatible comparison (~>) matches from the specified version up one 
version.
@@ -336,22 +339,36 @@
         named = {}
         if (Hash === spec || Struct === spec) &&
            (spec.keys & ActsAsArtifact::ARTIFACT_ATTRIBUTES).empty?
-          spec.each_pair { |k, v| named[k] = Artifact.to_hash(v) }
+          spec.each_pair do |name, art|
+            if name =~ /([^:]+:){2,4}/
+              default_version = art
+              art = Artifact.to_hash(name)
+              name = art[:id]
+            elsif Array === art
+              art, default_version = Artifact.to_hash(art.first), art.last
+            else
+              art, default_version = Artifact.to_hash(art), nil
+            end
+            named[name] = [art, default_version]
+          end
         else
-          named[nil] = Artifact.to_hash(spec)
+          spec = Artifact.to_hash(spec)
+          named[spec[:id]] = [spec, nil]
         end
-        named.each_pair do |name, spec|
+        named.each_pair do |name, spec_version|
+          spec, default_version = *spec_version
           unvers = normalize_name(spec)
           spec[:version] = VersionRequirement.create(spec[:version])
           @requires[unvers] = spec
           if name
             name = name.to_sym
-            using = @using[name] || @[EMAIL PROTECTED]
+            using = @using[name] || @[EMAIL PROTECTED]
             using = { :version  => using } if using.kind_of?(String) && 
VersionRequirement.version?(using)
             fail_unless_satisfied(spec, using)
-            @aliases[name] = unvers
-            @aliases[unvers] = name
+            @ids[name] = unvers
+            @ids[unvers] = name
           end
+          setting_defaults { set(unvers, default_version) } if default_version
         end
       end
       self
@@ -385,12 +402,7 @@
     #   Foo::Addon.baz    # used baz-4.0
     # 
     def default(*specs)
-      @setting_defaults = true
-      begin
-        use(*specs)
-      ensure
-        @setting_defaults = false
-      end
+      setting_defaults { use(*specs) }
     end
 
     # See examples for #need and #default methods.
@@ -466,6 +478,16 @@
       end
     end
 
+    def setting_defaults
+      @setting_defaults = true
+      begin
+        result = yield
+      ensure
+        @setting_defaults = false
+      end
+      result
+    end
+
     def set(name, spec)
       name = normalize_name(name)
       needed = requirement(name)
@@ -475,10 +497,12 @@
         satisfied = current && needed && 
needed[:version].satisfied_by?(current[:version])
         unless satisfied || (!needed && current)
           fail_unless_satisfied(needed, candidate)
+          @ids[spec[:id].to_sym], @ids[name] = name, spec[:id].to_sym if 
@ids[name].nil? && spec[:id]
           @using[name] = spec
         end
       else
         fail_unless_satisfied(needed, candidate)
+        @ids[spec[:id].to_sym], @ids[name] = name, spec[:id].to_sym if 
@ids[name].nil? && spec[:id]
         @using[name] = spec
       end
     end

Modified: incubator/buildr/trunk/lib/java/groovyc.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/java/groovyc.rb?rev=641089&r1=641088&r2=641089&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/java/groovyc.rb (original)
+++ incubator/buildr/trunk/lib/java/groovyc.rb Tue Mar 25 17:12:05 2008
@@ -47,19 +47,18 @@
     # * :source            -- Source code compatibility.
     # * :target            -- Bytecode compatibility.
     # * :javac             -- Hash of options passed to the ant javac task
+    #
+    # ArtifactNamespace for this module is 
+    # 
     class Groovyc < Base
       
-      Buildr.artifacts(self) do
-        need :groovy => "org.codehaus.groovy:groovy:jar:>=1.5.3",
-             :commons_cli => 'commons-cli:commons-cli:jar:>=1.0',
-             :asm => 'asm:asm:jar:>=2.2',
-             :antlr => 'antlr:antlr:jar:>=2.7.7'
-        default :groovy => '1.5.3',
-                :commons_cli => '1.0',
-                :asm => '2.2.3',
-                :antlr => '2.7.7'
-      end
-        
+      REQUIREMENTS = {
+        'org.codehaus.groovy:groovy:jar:>=1.5.3' => '1.5.3',
+        'commons-cli:commons-cli:jar:>=1.0' => '1.0',
+        'asm:asm:jar:>=2.2' => '2.2.3',
+        'antlr:antlr:jar:>=2.7.7' => '2.7.7'
+      }.tap { |reqs| Buildr.artifacts[self].need reqs }
+      
       ANT_TASK = 'org.codehaus.groovy.ant.Groovyc'
       GROOVYC_OPTIONS = [:encoding, :verbose, :fork, :memoryInitialSize, 
:memoryMaximumSize, :listfiles, :stacktrace]
       JAVAC_OPTIONS = [:optimise, :warnings, :debug, :deprecation, :source, 
:target, :javac]
@@ -67,7 +66,7 @@
 
       class << self
         def dependencies #:nodoc:
-          Buildr.artifacts(Buildr.artifacts[self].used)
+          Buildr.artifacts[self].values
         end
 
         def applies_to?(project, task) #:nodoc:

Modified: incubator/buildr/trunk/spec/artifact_namespace_spec.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/artifact_namespace_spec.rb?rev=641089&r1=641088&r2=641089&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/artifact_namespace_spec.rb (original)
+++ incubator/buildr/trunk/spec/artifact_namespace_spec.rb Tue Mar 25 17:12:05 
2008
@@ -92,12 +92,28 @@
     end
   end
 
+  it 'should take the artifact id attribute as name' do
+     artifacts do |ns|
+        ns.need 'foo:bar:jar:>1.0'
+        ns.default :bar => '1.1'
+        ns.spec('foo:bar:jar:-')[:version].should == '1.1'
+        ns.use 'some:thing:jar:2.0'
+        ns.spec(:thing)[:version].should == '2.0'
+        ns.spec('some:thing:jar:-')[:version].should == '2.0'
+     end
+  end
+
   it 'should register a requirement with the #need method' do
     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
+    artifacts('foo') do |ns|
+      ns.need 'foo:baz:jar:>1.0' => '2.0'
+      ns.spec('foo:baz:jar:-').values_at(:id, :version).should == ['baz', 
'2.0']
+      ns.spec(:baz).values_at(:id, :version).should == ['baz', '2.0']
     end
   end
 


Reply via email to