ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <DDevie...@lgc.com>
Subject RE: <echo> with if/unless - a request
Date Fri, 07 Jun 2002 15:37:31 GMT
Better yet, factor the conditions in a base class, and derive from it. That
way, you can add the conditions to any task you want that derives from Task
(basically simple tasks). All there is do in the Task to change is rename
execute in runExec(), since I declare execute() final to do the conditional
checks. This is very old code (compiled against ANT 3.x), but the principle
remains (and its wasn't throroughly tested either ;-).

I heard ANT 2 will take are of these kind of the conditions in the Framework
instead, and thus tasks don't have to deal with it anymore... Yeahhh. --DD

PS: And even if you already derive from something else, you can compose it,
with some twisting (as I did in my Property task allowing overrides).

-----Original Message-----
From: Diane Holt [mailto:holtdl@yahoo.com] 
Sent: Friday, June 07, 2002 10:24 AM
To: Ant Users List
Subject: Re: <echo> with if/unless - a request

--- Juergen Damke <DAMKE@de.ibm.com> wrote:
> Would it be great effort to add if/unless attributes to <echo
> message=""/> similar to <fail message=""/>   ???

You could look at the diff for Exit.java where if/unless was added:

 
http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/task
defs/Exit.java.diff?r1=1.6&r2=1.7&diff_format=h

and go from there.

Diane

--------------------
package com.lgc.jax.tools;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Location;
import org.apache.tools.ant.BuildException;

public abstract class AbstractConditionalBaseTask extends Task {

  private String _osCondition;
  private String _ifCondition;
  private String _whenCondition;
  private String _unlessCondition;

  /** template method called by {@link #execute} if all conditions are met
*/
  protected void runExec() throws BuildException {}

  protected void debug(String message) {
    project.log(this,message,Project.MSG_DEBUG);
  }
  protected void verbose(String message) {
    project.log(this,message,Project.MSG_VERBOSE);
  }

  // Must not call it execute since reflection assumes there's
  // only one method called execute. IllegalAccessException otherwise!
  void runExecute(Project p, Target t, Location l, String name)
    throws BuildException {

    this.project  = p;
    this.target   = t;
    this.location = l;

    if (this.taskName==null)
      this.taskName = name;

    execute();
  }

  public final void execute() throws BuildException {
    boolean osCondition     = testOsCondition();
    boolean ifCondition     = testIfCondition();
    boolean whenCondition   = testWhenCondition();
    boolean unlessCondition = testUnlessCondition();

    if (ifCondition && whenCondition && unlessCondition) {
      runExec();
    }
    else {
      if (!osCondition) {
        String os = System.getProperty("os.name");
        String condition = (_osCondition==null)?"":_osCondition;
        verbose("Skipped: '"+condition+"' not found in
${os.name}=`"+os+"'!");
      } 
      if (!ifCondition) {
        String condition = (_ifCondition==null)?"":_ifCondition;
        verbose("Skipped: property '"+condition+"' not set!");
      } 
      if (!unlessCondition) {
        String condition = (_unlessCondition==null)?"":_unlessCondition;
        verbose("Skipped: property '"+condition+"' set!");
      }
      if (!whenCondition) {
        String condition = (_whenCondition==null)?"":_whenCondition;
        verbose("Skipped: test '"+condition+"' not true!");
      }
    }
  }

  public void setOs(String os) {
    _osCondition = os;
  }

  public void setIf(String property) {
    _ifCondition = property;
  }

  public void setWhen(String condition) {
    _whenCondition = condition;
  }

  public void setUnless(String property) {
    _unlessCondition = property;
  }

  private boolean testOsCondition() {
    if (_osCondition==null)
      return true;
    return _osCondition.indexOf(System.getProperty("os.name"))>=0;
  }

  private boolean testIfCondition() {
    if (_ifCondition==null)
      return true;
    return project.getProperty(_ifCondition)!=null;
  }

  private boolean testWhenCondition() {
    return true;
  }

  private boolean testUnlessCondition() {
    if (_unlessCondition==null)
      return true;
    return project.getProperty(_unlessCondition)==null;
  }

} // END class AbstractConditionalBaseTask


-----------------

package com.lgc.jax.tools;

<snip/>

/**
 * Add conditionals (os, if, unless, when) to standard property task.
 *
 * @author <a href="mailto:ddevienne@lgc.com">Dominique Devienne</a>
 */
public class PropertyTask extends Property {

  protected AbstractConditionalBaseTask _conditions;

  public PropertyTask() {
    _conditions = new AbstractConditionalBaseTask() {
      protected void runExec() throws BuildException {
        // I'm not which execute it would call, the one from
        // Property (our base class), or the one from MyConditionalTask's
        // base class (MyTask and thus Task)! Makes it explicit..
        //super.execute();
        superExec();
      }
    };
  }

<snip/>

  private void superExec() throws BuildException {
    super.execute();
  }

  public void execute() throws BuildException {
    // Must copy the state of this task to the impl pseudo class
    // that takes care of the conditionals.
    _conditions.runExecute(project,target,location,taskName);
  }

  public void setOs(String os) {
    _conditions.setOs(os);
  }

  public void setIf(String property) {
    _conditions.setIf(property);
  }

  public void setWhen(String condition) {
    _conditions.setWhen(condition);
  }

  public void setUnless(String property) {
    _conditions.setUnless(property);
  }

} // END class PropertyTask

--
To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


Mime
View raw message