Check if directory exists in Phing and prompt to continue?
Asked Answered
B

1

9

I'm trying to check if a directory or file exists in Phing but I cannot even get the basics to work.

For example:

<project name="test" default="help" basedir="./">

<target name="clean" description="Deletes directory">

    <available file="/testy" type="dir" property="dir.Exists" />

        <if>
            <isset property="dir.Exists"/>
                <then>
                    <echo>Yep</echo>
                </then> 
        </if>

</target>

<phingcall target="clean" />

</project>

I get a weird error:

Error reading project file [wrapped: \build.xml:22:18: Error initializing nested  
element <echo> [wrapped: phing.tasks.system.IfTask doesn't support the 'echo' 
creator/adder.]]

Ultimately I wanted to add a conditional yes/no to proceed if a directory exists.

ps. The error has nothing to do with "nested element echo" as far as I can tell because if I remove the echo it still sends the same error, in fact I think this is a default syntax related error message or something.

Booklover answered 20/8, 2012 at 3:42 Comment(0)
T
11

If all you need to do is delete the directory then just call delete on it. Phing will automatically check if it exists for you so you don't need to do the check:

<target name="clean">
    <delete
      dir="${project.basedir}/${source.directory}" quiet='true'
    />
</target>

The important delete attributes here are: Phing Delete Attributes

The following works fine in my system:

<target name='test'>
  <if>
    <available file='results' type='dir' />
    <then>
      <echo>Yep</echo>
    </then>
  </if>
</target>

So I'm thinking the way you are using the AvailableTask is incorrect.

Tibbetts answered 23/8, 2012 at 19:7 Comment(3)
It's not what I'm trying though, it was just an example. The real use prompts the user with a yes/no to decide to delete or not, like rm -i , it seems like <delete> does not have this operator.Booklover
Notice that available task requires a property attribute (mandatory) - phing.info/docs/guide/trunk/AvailableTask.htmlFissile
@TomasPrado property attribute is not mandatory when available is used as conditionWray

© 2022 - 2024 — McMap. All rights reserved.