Browse Source

Fixed problem with quiet flag preventing

deletes.

Reported by:		Martin van den Bemt

Fixed double delete message with verbose
flag.

Reported by:		Jason Rosenburg

Added ability to remove empty directories
when using the implicit or nested filesets.
Off by default for now, as that is the
current behaviour.  If there is enough
agreement, we probably want to turn this
on by default.

Requested by:		Lots of people. :-)


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268503 13f79535-47bb-0310-9956-ffa450edef68
master
glennm 24 years ago
parent
commit
746290bbea
2 changed files with 65 additions and 11 deletions
  1. +16
    -2
      docs/index.html
  2. +49
    -9
      src/main/org/apache/tools/ant/taskdefs/Delete.java

+ 16
- 2
docs/index.html View File

@@ -2075,7 +2075,8 @@ repository pointed to by the cvsRoot attribute, and stores the files in "${
<h3>Description</h3>
<p>Deletes either a single file, all files in a specified directory and its
sub-directories, or a set of files specified by one or more <a href="#fileset">FileSet</a>s.
When specifying a set of files, empty directories are <i>not</i> removed.</p>
When specifying a set of files, empty directories are <i>not</i> removed by default.
To remove empty directories, use the <em>includeEmptyDirs</em> atribute.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@@ -2109,6 +2110,12 @@ When specifying a set of files, empty directories are <i>not</i> removed.</p>
Default is &quot;false&quot; meaning things are &quot;noisy&quot;.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">includeEmptyDirs</td>
<td valign="top">Set to &quot;true&quot; to delete empty directores when
using filesets. Default is &quot;false&quot;.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">includes</td>
<td valign="top"><i>Deprecated.</i> Comma separated list of patterns of files that must be
@@ -2145,13 +2152,20 @@ When specifying a set of files, empty directories are <i>not</i> removed.</p>
<pre> &lt;delete file=&quot;/lib/ant.jar&quot;/&gt;</pre>
<p>deletes the file <code>/lib/ant.jar</code>.</p>
<pre> &lt;delete dir=&quot;lib&quot;/&gt;</pre>
<p>deletes all files in the <code>/lib</code> directory.</p>
<p>deletes the <code>lib</code> directory, including all files
and subdirectories of <code>lib</code>.</p>
<pre> &lt;delete&gt;
&lt;fileset dir=&quot;.&quot; includes=&quot;**/*.bak&quot;/&gt;
&lt;/delete&gt;
</pre>
<p>deletes all files with the extension &quot;<code>.bak</code>&quot from the current directory
and any sub-directories.</p>
<pre> &lt;delete includeEmptyDirs=&quot;true&quot; &gt;
&lt;fileset dir=&quot;build&quot; /&gt;
&lt;/delete&gt;
</pre>
<p>deletes all files and subdirectories of <code>build</code>, but not
<code>build</code> itself.<p>
<hr>
<h2><a name="deltree">Deltree</a></h2>
<h3><i>Deprecated</i></h3>


+ 49
- 9
src/main/org/apache/tools/ant/taskdefs/Delete.java View File

@@ -79,6 +79,7 @@ public class Delete extends MatchingTask {
protected File dir = null;
protected Vector filesets = new Vector();
protected boolean usedMatchingTask = false;
protected boolean includeEmpty = false; // by default, remove matching empty dirs

private int verbosity = Project.MSG_VERBOSE;
private boolean quiet = false;
@@ -128,6 +129,13 @@ public class Delete extends MatchingTask {
}

/**
* Used to delete empty directories.
*/
public void setIncludeEmptyDirs(boolean includeEmpty) {
this.includeEmpty = includeEmpty;
}

/**
* Adds a set of files (nested fileset attribute).
*/
public void addFileset(FileSet set) {
@@ -234,7 +242,7 @@ public class Delete extends MatchingTask {
} else {
log("Deleting: " + file.getAbsolutePath());
if (!quiet && !file.delete()) {
if (!file.delete() && !quiet) {
throw new BuildException("Unable to delete file " + file.getAbsolutePath());
}
}
@@ -245,7 +253,15 @@ public class Delete extends MatchingTask {

// delete the directory
if (dir != null && dir.exists() && dir.isDirectory() && !usedMatchingTask) {
log("Deleting directory " + dir.getAbsolutePath());
/*
If verbosity is MSG_VERBOSE, that mean we are doing regular logging
(backwards as that sounds). In that case, we want to print one
message about deleting the top of the directory tree. Otherwise,
the removeDir method will handle messages for _all_ directories.
*/
if (verbosity == Project.MSG_VERBOSE) {
log("Deleting directory " + dir.getAbsolutePath());
}
removeDir(dir);
}

@@ -254,14 +270,16 @@ public class Delete extends MatchingTask {
FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(project);
String[] files = ds.getIncludedFiles();
removeFiles(fs.getDir(project), files);
String[] dirs = ds.getIncludedDirectories();
removeFiles(fs.getDir(project), files, dirs);
}

// delete the files from the default fileset
if (usedMatchingTask && dir != null) {
DirectoryScanner ds = super.getDirectoryScanner(dir);
String [] files = ds.getIncludedFiles();
removeFiles(dir, files);
String[] files = ds.getIncludedFiles();
String[] dirs = ds.getIncludedDirectories();
removeFiles(dir, files, dirs);
}
}

@@ -279,28 +297,50 @@ public class Delete extends MatchingTask {
removeDir(f);
} else {
log("Deleting " + f.getAbsolutePath(), verbosity);
if (!quiet && !f.delete()) {
if (!f.delete() && !quiet) {
throw new BuildException("Unable to delete file " + f.getAbsolutePath());
}
}
}
log("Deleting directory " + d.getAbsolutePath(), verbosity);
if (!quiet && !d.delete()) {
if (!d.delete() && !quiet) {
throw new BuildException("Unable to delete directory " + dir.getAbsolutePath());
}
}

protected void removeFiles(File d, String[] files) {
protected void removeFiles(File d, String[] files, String[] dirs) {
if (files.length > 0) {
log("Deleting " + files.length + " files from " + d.getAbsolutePath());
for (int j=0; j<files.length; j++) {
File f = new File(d, files[j]);
log("Deleting " + f.getAbsolutePath(), verbosity);
if (!quiet && !f.delete()) {
if (!f.delete() && !quiet) {
throw new BuildException("Unable to delete file " + f.getAbsolutePath());
}
}
}

if (dirs.length > 0 && includeEmpty) {
int dirCount = 0;
for (int j=0; j<dirs.length; j++) {
File dir = new File(d, dirs[j]);
String[] dirFiles = dir.list();
if (dirFiles == null || dirFiles.length == 0) {
log("Deleting " + dir.getAbsolutePath(), verbosity);
if (!dir.delete() && !quiet) {
throw new BuildException("Unable to delete directory " + dir.getAbsolutePath());
} else {
dirCount++;
}
}
}

if (dirCount > 0) {
log("Deleted " + dirCount + " director" +
(dirCount==1 ? "y" : "ies") +
" from " + d.getAbsolutePath());
}
}
}
}


Loading…
Cancel
Save