You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by st...@apache.org on 2005/01/17 22:54:34 UTC

cvs commit: ant/docs/manual/CoreTasks conditions.html

stevel      2005/01/17 13:54:34

  Modified:    src/main/org/apache/tools/ant/types defaults.properties
               src/etc/testcases/types/selectors scriptselector.xml
               docs/manual/CoreTasks conditions.html
  Added:       src/etc/testcases/types scriptcondition.xml
               src/main/org/apache/tools/ant/types/optional
                        ScriptCondition.java
               src/testcases/org/apache/tools/ant/types/optional
                        ScriptConditionTest.java
  Log:
  Adding scripting support to conditions.
  
  Revision  Changes    Path
  1.1                  ant/src/etc/testcases/types/scriptcondition.xml
  
  Index: scriptcondition.xml
  ===================================================================
  <project name="testscriptcondition" >
  
  
    <macrodef name="t">
      <element name="test" implicit="yes" />
      <attribute name="message"/>
      <sequential>
        <fail message="query @{message} failed; result was false">
          <condition>
            <not>
              <test />
            </not>
          </condition>
        </fail>
      </sequential>
    </macrodef>  
  
    <macrodef name="f">
      <element name="test" implicit="yes" />
      <attribute name="message"/>
      <sequential>
        <fail message="test @{message} failed; result was true">
          <condition>
            <test/>
          </condition>
        </fail>
      </sequential>
    </macrodef>  
    
    <!-- this is here to test the macro is well coded -->
   <target name="testMacro">
      <t message="testMacro" >
          <istrue value="true"/>
      </t>
      <f message="testMacro2" >
          <istrue value="false"/>
      </f>
    </target>  
  
   <target name="testNolanguage">
      <t message="testNolanguage" >
        <scriptcondition >
          self.setValue(true);
        </scriptcondition>
      </t>
    </target>
  
    <target name="testClearByDefault">
      <f message="testClearByDefault" >
        <scriptcondition language="javascript">
        </scriptcondition>
      </f>
    </target>  
    
    <target name="testValueWorks">
      <t message="testValueWorks" >
        <scriptcondition language="javascript"
          value="true" />
      </t>
    </target>    
    
    <target name="testSetWorks">
      <t message="testSetWorks" >
        <scriptcondition language="javascript" value="false">
          self.setValue(true);
        </scriptcondition>
      </t>
    </target>
  
    <target name="testClearWorks">
      <f message="testClearWorks">
        <scriptcondition language="javascript" value="true">
          self.setValue(false);
        </scriptcondition>
      </f>
    </target>  
  </project>
  
  
  
  1.1                  ant/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java
  
  Index: ScriptCondition.java
  ===================================================================
  /*
   * Copyright  2005 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   *
   */
  package org.apache.tools.ant.types.optional;
  
  import org.apache.tools.ant.ProjectComponent;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.util.ScriptRunner;
  import org.apache.tools.ant.taskdefs.condition.Condition;
  
  import java.io.File;
  
  /**
   * A condition that lets you include script.
   * The condition component sets a bean "self", whose attribute "result"
   * must be set to true for the condition to succeed, false to fail.
   * The default is 'false'
   */
  public class ScriptCondition extends ProjectComponent implements Condition {
  
      /**
       * script runner
       */
      private ScriptRunner runner = new ScriptRunner();
  
      /**
       * result field
       */
      private boolean value = false;
  
      /**
       * Load the script from an external file ; optional.
       *
       * @param file the file containing the script source.
       */
      public void setSrc(File file) {
          runner.setSrc(file);
      }
  
      /**
       * The script text.
       *
       * @param text a component of the script text to be added.
       */
      public void addText(String text) {
          runner.addText(text);
      }
  
      /**
       * Defines the language (required).
       *
       * @param language the scripting language name for the script.
       */
      public void setLanguage(String language) {
          runner.setLanguage(language);
      }
  
  
      /**
       * Is this condition true?
       *
       * @return true if the condition is true
       *
       * @throws org.apache.tools.ant.BuildException
       *          if an error occurs
       */
      public boolean eval() throws BuildException {
          runner.bindToComponent(this);
          runner.executeScript("ant_condition");
          return getValue();
      }
  
      /**
       * get the current value of the conditon
       * @return true if the condition
       */
      public boolean getValue() {
          return value;
      }
  
      /**
       * set the value of the condition.
       * This is used by the script to pass the return value.
       * It can be used by an attribute, in which case it sets the default
       * value
       * @param value
       */
      public void setValue(boolean value) {
          this.value = value;
      }
  }
  
  
  
  1.1                  ant/src/testcases/org/apache/tools/ant/types/optional/ScriptConditionTest.java
  
  Index: ScriptConditionTest.java
  ===================================================================
  /*
   * Copyright  2005 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   *
   */
  package org.apache.tools.ant.types.optional;
  
  import org.apache.tools.ant.BuildFileTest;
  
  /**
   */
  public class ScriptConditionTest extends BuildFileTest {
      /**
       * Constructor for the BuildFileTest object
       *
       * @param name string to pass up to TestCase constructor
       */
      public ScriptConditionTest(String name) {
          super(name);
      }
  
      public void setUp() {
          configureProject("src/etc/testcases/types/scriptcondition.xml");
      }
  
      public void testNolanguage() {
          expectBuildExceptionContaining("testNolanguage",
                  "Absence of language attribute not detected",
                  "script language must be specified");
      }
  
      public void testMacro() {
          executeTarget("testMacro");
      }
      public void testClearByDefault() {
          executeTarget("testClearByDefault");
      }
      public void testValueWorks() {
          executeTarget("testValueWorks");
      }
  
      public void testSetWorks() {
          executeTarget("testSetWorks");
      }
  
      public void testClearWorks() {
          executeTarget("testClearWorks");
      }
  
  }
  
  
  
  1.35      +2 -0      ant/src/main/org/apache/tools/ant/types/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/defaults.properties,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- defaults.properties	16 Jan 2005 23:37:51 -0000	1.34
  +++ defaults.properties	17 Jan 2005 21:54:34 -0000	1.35
  @@ -40,3 +40,5 @@
   ispingable=org.apache.tools.ant.taskdefs.optional.condition.IsPingable
   mavenrepository=org.apache.tools.ant.taskdefs.repository.MavenRepository
   scriptselector=org.apache.tools.ant.types.optional.ScriptSelector
  +scriptcondition=org.apache.tools.ant.types.optional.ScriptCondition
  +
  
  
  
  1.2       +0 -2      ant/src/etc/testcases/types/selectors/scriptselector.xml
  
  Index: scriptselector.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/types/selectors/scriptselector.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- scriptselector.xml	16 Jan 2005 23:37:51 -0000	1.1
  +++ scriptselector.xml	17 Jan 2005 21:54:34 -0000	1.2
  @@ -46,8 +46,6 @@
           </scriptselector>
         </selector>
       </testselected>
  -    <scriptdef name="nolang">
  -    </scriptdef>
     </target>
   
     <target name="testSelectionSetByDefault">
  
  
  
  1.25      +56 -4     ant/docs/manual/CoreTasks/conditions.html
  
  Index: conditions.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTasks/conditions.html,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- conditions.html	1 Dec 2004 11:25:46 -0000	1.24
  +++ conditions.html	17 Jan 2005 21:54:34 -0000	1.25
  @@ -334,7 +334,7 @@
       existence of any signature
     </p>
     <p>
  -    This condition has been added in Apache Ant 1.7.
  +    This condition was added in Apache Ant 1.7.
     </p>
     <table border="1" cellpadding="2" cellspacing="0">
     <tr>
  @@ -361,7 +361,7 @@
       Test whether a file passes an embedded selector.
     </p>
     <p>
  -    This condition has been added in Apache Ant 1.7.
  +    This condition was added in Apache Ant 1.7.
     </p>
     <table border="1" cellpadding="2" cellspacing="0">
     <tr>
  @@ -398,7 +398,7 @@
   its implementation class can be loaded. Types include
   tasks, datatypes, scriptdefs, macrodefs and presetdefs.</p>
   
  -<p>This condition has been added in Apache Ant 1.7.</p>
  +<p>This condition was added in Apache Ant 1.7.</p>
   
   <table border="1" cellpadding="2" cellspacing="0">
     <tr>
  @@ -413,8 +413,60 @@
     </tr>
   </table>
   
  +<h4>scriptcondition</h4>
  +
  +<p>Evaluate a condition based on a script in any
  +<a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a>
  +supported language.</p>
  +<p>
  +See the <a href="../OptionalTasks/script.html">Script</a> task for
  +an explanation of scripts and dependencies.
  +</p>
  +
  +<p>This condition was added in Apache Ant 1.7.</p>
  +
  +<table border="1" cellpadding="2" cellspacing="0">
  +  <tr>
  +    <td valign="top"><b>Attribute</b></td>
  +    <td valign="top"><b>Description</b></td>
  +    <td align="center" valign="top"><b>Required</b></td>
  +  </tr>
  +  <tr>
  +    <td valign="top">language</td>
  +    <td valign="top">script language</td>
  +    <td valign="top" align="center">Yes</td>
  +  </tr>
  +  <tr>
  +    <td valign="top">value</td>
  +    <td valign="top">default boolean value</td>
  +    <td valign="top" align="center">No -default is "false"</td>
  +  </tr>  
  +  <tr>
  +    <td valign="top">src</td>
  +    <td valign="top">filename of script source</td>
  +    <td valign="top" align="center">No</td>
  +  </tr>  
  +</table>
  +<p>
  +The script supports script language inline, this script has access to the
  +same beans as the <code>&lt;script&gt;</code> task, and to the <code>
  +self</code> bean, which refers back to the condition itself. The
  +<code>value</code> property of this bean sets the return value:
  +</p>
  +<p>
  +Example:
  +</p>
  +<pre>
  +    &lt;scriptcondition language=&quot;javascript&quot;
  +            value=&quot;true&quot;&gt;
  +        self.setValue(false);
  +    &lt;/scriptcondition&gt;
  +</pre>
  +
  +Sets the default value of the condition to true, then in the script, 
  +sets the value to false. This condition always evaluates to "false"
   <hr>
  -<p align="center">Copyright &copy; 2001-2004 Apache Software
  +<p align="center">Copyright &copy; 2001-2005 Apache Software
   Foundation. All rights Reserved.</p>
   
   </body>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org