I will followup on the other mails soon.

This patch will make deltree a MatchingTask which means you can
specify

<deltree dir="src">
<include name="**/*.class" />
</deltree>

and it should delete all the .class files in src (and all empty
directories as well).

Note that you now might have to explicitly disable default excludes
to get the old behavior of deltree (maybe this should be the
default?).

No documentation patches for now as the part about directory based
tasks needs a brush up.

Stefan

Index: src/main/org/apache/tools/ant/taskdefs/Deltree.java
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Deltree.java,v
retrieving revision 1.2
diff -u -r1.2 Deltree.java
--- src/main/org/apache/tools/ant/taskdefs/Deltree.java	2000/02/24 01:34:45	1.2
+++ src/main/org/apache/tools/ant/taskdefs/Deltree.java	2000/05/25 14:34:39
@@ -58,12 +58,13 @@
 import java.io.*;
 
 /**
+ * Deletes a whole directory tree.
  *
- *
  * @author [EMAIL PROTECTED]
+ * @author Stefan Bodewig <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
  */
 
-public class Deltree extends Task {
+public class Deltree extends MatchingTask {
 
     private File dir;
 
@@ -74,24 +75,31 @@
     public void execute() throws BuildException {
 	project.log("Deleting: " + dir.getAbsolutePath());
 
-	if (dir.exists()) {
-	    if (!dir.isDirectory()) {
-		dir.delete();
-		return;
-		// String msg = "Given dir: " + dir.getAbsolutePath() +
-		// " is not a dir";
-		// throw new BuildException(msg);
-	    }
-            try {
-                removeDir(dir);
-            } catch (IOException ioe) {
-                String msg = "Unable to delete " + dir.getAbsolutePath();
-                throw new BuildException(msg);
-            }
+        if (dir == null) {
+            throw new BuildException("dir attribute must be set!");
+        }
+
+	if (dir.exists() && !dir.isDirectory()) {
+            throw new BuildException(dir.getAbsolutePath()+" is not a directory");
         }
+
+        DirectoryScanner ds = super.getDirectoryScanner(dir);
+        String[] files = ds.getIncludedFiles();
+
+        for (int i=0; i<files.length; i++) {
+            (new File(dir, files[i])).delete();
+        }
+
+        try {
+            removeDir(dir);
+        } catch (IOException ioe) {
+            String msg = "Unable to delete " + dir.getAbsolutePath();
+            throw new BuildException(msg, ioe);
+        }
     }
     
     private void removeDir(File dir) throws IOException {
+        boolean wasEmpty = true;
 
         // check to make sure that the given dir isn't a symlink
         // the comparison of absolute path and canonical path
@@ -107,11 +115,13 @@
 	    if (f.isDirectory()) {
 		removeDir(f);
 	    } else {
-		f.delete();
-	    }
+                wasEmpty = false;
+            }
 	}
 	    //        }
-        dir.delete();
+        if (wasEmpty) {
+            dir.delete();
+        }
     }
 }
 

Reply via email to