You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by "Stover, Michael" <Mi...@usa.xerox.com> on 2000/09/04 18:21:47 UTC

Some bug fixes and minor additions

Hi, this is my first post to Ant, but I've been working on developing a GUI
front-end to Ant so that it can be used as installer software (ala
InstallShield).  In doing this, I've run across a few bugs, and some things
I've added for convenience.

1.  In the task Available.java, the work is done in the init() method.  This
didn't work out well because my build file changes things as it runs, and I
expected available to determine whether a resource was available at that
point in the run, so I changed init() to execute().

2.  When using the "if" attribute for a target, it could only ask if
something exists, and execute if it did.  However, I wanted to ask if
something didn't exist, and execute the task only then.  At first, I tried
some very clumsy, awkward stuff in the build file, but then I got tired of
debugging that, so I added the ability to use the NOT operator within the if
value.  So, I can write <target name="install_apache" depends="init"
if="!apache_installed">, and it will install apache only if it's not already
installed (or, to be precise, only if the property, "apache_installed", does
NOT exist).

Here's the code for this change:
In Target.java:

public void execute() throws BuildException {
	/* Modified the conditional */
        if (("".equals(this.condition)) ||
(project.getProperty(this.condition) != null) ||
                (this.condition.startsWith("!") &&
project.getProperty(this.condition.substring(1)) == null) ) {
	/* No other changes */

            Enumeration enum = tasks.elements();
            while (enum.hasMoreElements()) {
                Task task = (Task) enum.nextElement();
                task.execute();
            }
        } else {
            project.log("Skipped because property '" + this.condition + "'
not set.", this.name, Project.MSG_VERBOSE);
        }
    } // End execute()

Possibly, it might be preferrable to have an "unless" attribute for the
target tag instead of this "parsing" of the if conditional, but this was so
simple, and it serves my needs completely, I'm not sure it'd be worth it.
However, if some see a need for more complex conditionals, or if there would
be temptation to grow the parsing function, then something like "unless"
might be a better approach.

3. Lastly, I fixed a bug with using filter to replace.  If you are replacing
tags like @year@ within a line that has other "@" characters, then I don't
think the current version works.  Here's my version of the
replace(String,Hashtable) method within Project.java:

	/**
     * Does replacement on the given string using the given token table.
     *
     * @returns the string with the token replaced.
     */
    private String replace(String s, Hashtable tokens) {
        int index = s.indexOf(TOKEN_START);
        StringTokenizer tokenizer = new StringTokenizer(s,TOKEN_START,true);
        StringBuffer b = new StringBuffer();
        boolean replaced = false;
        int numTokens = tokenizer.countTokens();
        int count = 0;
        while(tokenizer.hasMoreTokens())
        {
          count++;
          String temp = tokenizer.nextToken();
          if(tokens.containsKey(temp) && count < numTokens)
          {
            b.setLength(b.length()-TOKEN_START.length());
            b.append((String)tokens.get(temp));
            replaced = true;
          }
          else
          {
            if(!replaced)
              b.append(temp);
            replaced = false;
          }
        }
        return b.toString();
    }

The entire method is different, so this replaces it all.

Apologies if this is based on old code - I've got the latest release, but
not the latest from cvs.

-Mike Stover

Re: Some bug fixes and minor additions

Posted by Stefan Bodewig <bo...@bost.de>.
>>>>> "SM" == Stover, Michael <Mi...@usa.xerox.com> writes:

 SM> 1.  In the task Available.java, the work is done in the init()
 SM> method.

Yes, I was planning to change that once ${} expansion happens at
runtime. I've never seen anybody using the properties set by available
in ${} constructs but you could do that (and your proposed change
would break this functionality).

Besides I want to commit a patch Glenn McAllister has submitted weeks
ago to assign arbitrary values to the property in <available> instead
of "true" - which makes the pattern described above a little bit more
useful.

 SM> 2.  When using the "if" attribute for a target, it could only ask
 SM> if something exists, and execute if it did.

There is an "unless" attribute to target - and it has been there for
quite some time. Your patch is against code that even predates Ant
1.1, not to speak of current CVS.

Stefan

Re: Some bug fixes and minor additions

Posted by Peter Donald <do...@mad.scientist.com>.
At 12:21  4/9/00 -0400, you wrote:
>Hi, this is my first post to Ant, but I've been working on developing a GUI
>front-end to Ant so that it can be used as installer software (ala
>InstallShield).  In doing this, I've run across a few bugs, and some things
>I've added for convenience.

kewl ! Is it going to be opensource ???

>2.  When using the "if" attribute for a target, it could only ask if
>something exists, and execute if it did.  However, I wanted to ask if
>something didn't exist, and execute the task only then.  At first, I tried
>some very clumsy, awkward stuff in the build file, but then I got tired of
>debugging that, so I added the ability to use the NOT operator within the if
>value.  So, I can write <target name="install_apache" depends="init"
>if="!apache_installed">, and it will install apache only if it's not already
>installed (or, to be precise, only if the property, "apache_installed", does
>NOT exist).


we already have an unless attribute that does just that. Download latest
CVS and try it out :P

Cheers,

Pete

*------------------------------------------------------*
| "Nearly all men can stand adversity, but if you want |
| to test a man's character, give him power."          |
|       -Abraham Lincoln                               |
*------------------------------------------------------*