[jruby~main:d732285c] Ant::BlockTarget: target block execution delayed until target is also exe

  • From: nicksieger@kenai.com
  • To: commits@jruby.kenai.com
  • Subject: [jruby~main:d732285c] Ant::BlockTarget: target block execution delayed until target is also exe
  • Date: Fri, 5 Mar 2010 22:09:46 +0000

Project:    jruby
Repository: main
Revision:   d732285c8b674ec2e86519b35622d2ae0b1c9f97
Author:     nicksieger
Date:       2010-03-05 22:04:48 UTC
Link:       

Log Message:
------------
Ant::BlockTarget: target block execution delayed until target is also executed


Revisions:
----------
d732285c8b674ec2e86519b35622d2ae0b1c9f97


Modified Paths:
---------------
lib/ruby/site_ruby/shared/ant/element.rb
lib/ruby/site_ruby/shared/ant/target.rb
spec/java_integration/ant/target_spec.rb
spec/java_integration/ant/task_spec.rb
spec/java_integration/ant_spec_helper.rb


Diffs:
------
diff --git a/lib/ruby/site_ruby/shared/ant/element.rb 
b/lib/ruby/site_ruby/shared/ant/element.rb
index ceb25ce..e483237 100644
--- a/lib/ruby/site_ruby/shared/ant/element.rb
+++ b/lib/ruby/site_ruby/shared/ant/element.rb
@@ -33,7 +33,7 @@ class Ant
     end
 
     def call(parent, args={}, &code)
-      element = create parent
+      element = create_element
       assign_attributes element, args
       define_nested_elements element if @clazz
       code.arity==1 ? code[element] : element.instance_eval(&code) if 
block_given?
@@ -53,7 +53,7 @@ class Ant
     end
 
     private
-    def create(parent) # See ProjectHelper2.ElementHelper
+    def create_element # See ProjectHelper2.ElementHandler
       UnknownElement.new(@name).tap do |e|
         e.ant = @ant
         e.project = @ant.project
diff --git a/lib/ruby/site_ruby/shared/ant/target.rb 
b/lib/ruby/site_ruby/shared/ant/target.rb
index 456bfd3..b8791de 100644
--- a/lib/ruby/site_ruby/shared/ant/target.rb
+++ b/lib/ruby/site_ruby/shared/ant/target.rb
@@ -38,7 +38,24 @@ class Ant
       set_project ant.project
       hash = extract_options(options)
       hash.each_pair {|k,v| send("set_#{k}", v) }
-      define_target(ant, &block) if block
+      @ant, @block = ant, block
+    end
+
+    def execute
+      # Have to dupe this logic b/c Ant doesn't provide a way to
+      # override inner part of execute
+      if_cond, unless_cond = if_condition, unless_condition
+      if if_cond && unless_cond
+        define_target.execute
+      elsif !if_cond
+        project.log(self, "Skipped because property '#{if_cond}' not set.", 
Project::MSG_VERBOSE)
+      else
+        project.log(self, "Skipped because property '#{unless_cond}' set.", 
Project::MSG_VERBOSE)
+      end
+    end
+
+    def defined_tasks
+      define_target.tasks
     end
 
     private
@@ -49,11 +66,30 @@ class Ant
       hash
     end
 
-    def define_target(ant, &block)
-      ant.current_target = self
-      ant.instance_eval(&block)
-    ensure
-      ant.current_target = nil
+    def if_condition
+      cond = get_if
+      return true unless cond
+      val = project.replace_properties(cond)
+      project.get_property(val) && val
+    end
+
+    def unless_condition
+      cond = get_unless
+      return true unless cond
+      val = project.replace_properties(cond)
+      project.get_property(val).nil? && val
+    end
+
+    def define_target
+      Target.new.tap do |t|
+        t.name = ""
+        begin
+          @ant.current_target = t
+          @ant.instance_eval(&@block)
+        ensure
+          @ant.current_target = nil
+        end
+      end
     end
   end
 
diff --git a/spec/java_integration/ant/target_spec.rb 
b/spec/java_integration/ant/target_spec.rb
index c2aa3d6..787488b 100644
--- a/spec/java_integration/ant/target_spec.rb
+++ b/spec/java_integration/ant/target_spec.rb
@@ -21,4 +21,31 @@ describe Ant, "targets", :type => :ant do
     end
     ant.project.targets.keys.to_a.should include("a", "b")
   end
+
+  it "should heed :if and :unless conditions" do
+    message = ""
+    ant = Ant.new :output_level => 0 do
+      property :name => "foo", :value => "defined"
+      target :will_never_execute, :if => "not.defined" do
+        message << "will_never_execute?"
+      end
+
+      target :also_will_never_execute, :unless => "foo" do
+        message << "also_will_never_execute"
+      end
+
+      target :may_execute, :if => "bar" do
+        message << "executed"
+      end
+    end
+
+    ant.execute_target(:will_never_execute)
+    ant.execute_target(:also_will_never_execute)
+    ant.execute_target(:may_execute)
+    message.should == ""
+
+    ant.property :name => "bar", :value => "defined"
+    ant.execute_target(:may_execute)
+    message.should_not == ""
+  end
 end
diff --git a/spec/java_integration/ant/task_spec.rb 
b/spec/java_integration/ant/task_spec.rb
index e09d65e..fabc1f2 100644
--- a/spec/java_integration/ant/task_spec.rb
+++ b/spec/java_integration/ant/task_spec.rb
@@ -4,6 +4,7 @@ describe Ant, "tasks:", :type => :ant do
   before :all do
     # The single example ant project these specs will validate
     @output = output = "ant-file#{rand}.txt"
+    @message = message = ""
     @ant = Ant.new :output_level => 0 do
       property :name => "jar", :value => "spec-test.jar"
       property :name => "dir", :value => "build"
@@ -33,6 +34,10 @@ describe Ant, "tasks:", :type => :ant do
       target :greet do
         greet :msg => "Ant"
       end
+
+      target :rubygreet do
+        message << "Hello Ruby!"
+      end
     end
   end
 
@@ -40,13 +45,15 @@ describe Ant, "tasks:", :type => :ant do
     File.unlink(@output) if File.exist?(@output)
   end
 
+  before :each do
+    @message.replace("")
+  end
+
   describe "jar" do
     subject do
       @ant.project.targets["jar"]
     end
 
-    it { should have_valid_tasks }
-
     it { should have_structure([{:_name => "jar", :destfile => 
"spec-test.jar", :compress => "true", :index => "true",
                                  :_children => [ { :_name => "fileset", :dir 
=> "build" }] }]) }
 
@@ -60,8 +67,6 @@ describe Ant, "tasks:", :type => :ant do
       @ant.project.targets["jarjar"]
     end
 
-    it { should have_valid_tasks }
-
     it { should have_structure([{:_name => "jarjar", :destfile => 
"spec-test.jar", :compress => "true",
                                  :_children => [ { :_name => "fileset", :dir 
=> "build" },
                                                  { :_name => "zipfileset", 
:src => "./lib/jruby.jar" }] }]) }
@@ -77,4 +82,12 @@ describe Ant, "tasks:", :type => :ant do
       File.read(@output).should == "Hello Ant"
     end
   end
+
+  describe "rubygreet" do
+    it "should execute the code block when the target is executed" do
+      @message.should == ""
+      @ant.execute_target(:rubygreet)
+      @message.should == "Hello Ruby!"
+    end
+  end
 end
diff --git a/spec/java_integration/ant_spec_helper.rb 
b/spec/java_integration/ant_spec_helper.rb
index eace9fa..c8eb555 100644
--- a/spec/java_integration/ant_spec_helper.rb
+++ b/spec/java_integration/ant_spec_helper.rb
@@ -8,31 +8,6 @@ class Ant
         Ant.send(:instance_variable_set, "@ant", nil)
       end
 
-      # Validates the tasks in a target look sane. Currently just
-      # checks owning_target.
-      class ValidTasksMatcher
-        def description
-          "have valid tasks"
-        end
-
-        def matches?(target)
-          @target = target
-          result = true
-          @target.tasks.each do |t|
-            result &&= (t.owning_target == @target)
-          end
-          result
-        end
-
-        def failure_message_for_should
-          "expected #{@target.inspect} to have valid tasks"
-        end
-
-        def failure_message_for_should_not
-          "expected #{@target.inspect} to not have valid tasks"
-        end
-      end
-
       # Allows matching task structure with a nested hash as follows.
       #
       # { :_name => "jar", :destfile => "spec-test.jar", :compress => 
"true", :index => "true",
@@ -59,7 +34,7 @@ class Ant
         def matches?(actual)
           @actual = actual
           result = true
-          tasks = actual.tasks
+          tasks = actual.defined_tasks
           if Hash === @expected && tasks.length != 1 || tasks.length != 
@expected.length
             @message = "task list length different"
             return false
@@ -123,10 +98,6 @@ class Ant
         end
       end
 
-      def have_valid_tasks
-        ValidTasksMatcher.new
-      end
-
       def have_structure(hash)
         TaskStructureMatcher.new(hash)
       end




[jruby~main:d732285c] Ant::BlockTarget: target block execution delayed until target is also exe

nicksieger 03/05/2010
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close