1

i'm building a free and a pro package with Ant.
In the pro version, there aren't some files, since i can skip them using a custom PhpDoc tag.

Sadly, using this system i have some empty dirs with only a index.html and i'd wish to delete them, so i can distribuite a "clean" package.

Directories I need to delete follow this path:

views
  `- {directoryName1}
      `- tmpl
         `- index.html
  `- {directoryName2}
   .... and son on ....

Only the directoryName changes everytime, excepts that everything remains the same.
I tried with this ant task, but it didn't work:

<delete includeemptydirs="true">
    <fileset dir="${buildDir}/free">
       <exclude name="**/*" />
       <include name="**/tmpl/index.html"/>
    </fileset>
</delete>

any help?

tampe125
  • 8,231
  • 7
  • 30
  • 45
  • maybe this question can help: http://stackoverflow.com/questions/2232943/how-to-exclude-a-directory-from-ant-fileset-based-on-directories-contents – oers Mar 05 '12 at 14:24
  • it's interesting, but my index.html file is in every directory. i need to delete those directories which contains ONLY a file with index.html – tampe125 Mar 05 '12 at 14:34

1 Answers1

2

You need a combination of resourcecount and condition checks. I use ant-contrib , but you have to include additional jars.

<project name="blah" basedir="." default="conditionally.clean">
    <taskdef resource="net/sf/antcontrib/antlib.xml"/>
    <macrodef name="m.clean">
        <attribute name="dir.todelete"/>
        <sequential>
            <local name="count.files"/>
            <resourcecount property="count.files">
                <fileset dir="@{dir.todelete}">
                    <exclude name="index.html"/>
                </fileset>
            </resourcecount>

            <if>
                <equals arg1="${count.files}" arg2="0"/>
                <then>
                    <echo message="will delete dir @{dir.todelete}"  />
                </then>
                <else>
                       <echo message="will not delete dir @{dir.todelete}"  />
                </else>
            </if>

        </sequential>

    </macrodef>

    <target name="conditionally.clean">
        <m.clean dir.todelete="etc/demo2"/>
        <m.clean dir.todelete="etc/demo1"/>
    </target>

</project>

You can construct a "foreach" (requires ant-contrib) to get all directories and call above macro...

(I guess a custom task or a embedded script may be much cleaner than the build.xml ... )

Jayan
  • 18,003
  • 15
  • 89
  • 143