Patch is attached.

On Wed, 2003-10-08 at 10:48, Ian MacLean wrote:
> Sounds useful. Post away.
> 
> Ian
> Met @ Uber wrote:
> 
> >The patch I'm talking about is hardly important, its just a cleanup of
> >the logic and code within DeleteTask.cs.  I was trying to figure out why
> >directory and file deletion were displaying different (messages), so I
> >went in and took.  I cleaned up the logic a lot so that everything is
> >consistent, as far as I am concerned.  It should be more efficient as a
> >result.
> >
> >If you want it, let me know, else I'll suck it up ;-)
> >
> >~ Matthew
> >
> >
> >
> >-------------------------------------------------------
> >This sf.net email is sponsored by:ThinkGeek
> >Welcome to geek heaven.
> >http://thinkgeek.com/sf
> >_______________________________________________
> >nant-developers mailing list
> >[EMAIL PROTECTED]
> >https://lists.sourceforge.net/lists/listinfo/nant-developers
> >  
> >
> 
> 
> 
Index: DeleteTask.cs
===================================================================
RCS file: /cvsroot/nant/nant/src/NAnt.Core/Tasks/DeleteTask.cs,v
retrieving revision 1.17
diff -u -r1.17 DeleteTask.cs
--- DeleteTask.cs	23 Aug 2003 13:31:34 -0000	1.17
+++ DeleteTask.cs	8 Oct 2003 15:24:41 -0000
@@ -118,9 +118,11 @@
         #region Override implementation of Task
 
         protected override void ExecuteTask() {
-            // limit task to deleting either a file or a directory or a file set
+            // limit task to deleting either a file, directory, or file set
             if (FileName != null && DirectoryName != null) {
-                throw new BuildException("Cannot specify 'file' and 'dir' in the same delete task.", Location);
+                string msg = "Cannot specify 'file' and 'dir' in the same " +
+                             "delete task.";
+                throw new BuildException(msg, Location);
             }
 
             if (FileName != null) {
@@ -129,44 +131,49 @@
                 try {
                     path = Project.GetFullPath(FileName);
                 } catch (Exception e) {
-                    string msg = String.Format(CultureInfo.InvariantCulture, "Could not determine path from {0}.", FileName);
+                    string msg = 
+                        String.Format(CultureInfo.InvariantCulture, 
+                                      "Could not determine path from '{0}'.", 
+                                      FileName);
                     throw new BuildException(msg, Location, e);
                 }
-                DeleteFile(path, true);
-
+                DeleteFile(path, Verbose);
             } else if (DirectoryName != null) {
                 // try to delete specified directory
                 string path = null;
                 try {
                     path = Project.GetFullPath(DirectoryName);
                 } catch (Exception e) {
-                    string msg = String.Format(CultureInfo.InvariantCulture, "Could not determine path from {0}.", DirectoryName);
+                    string msg = 
+                        String.Format(CultureInfo.InvariantCulture, 
+                                      "Could not determine path from '{0}'.", 
+                                      DirectoryName);
                     throw new BuildException(msg, Location, e);
                 }
-                if (!Directory.Exists(path)) {
-                    string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete directory {0}. The directory does not exist.", path);
-                    throw new BuildException(msg, Location);
-                }
-                
-                Log(Level.Info, LogPrefix + "Deleting directory {0}.", path);
-                RecursiveDeleteDirectory(path);
+                RecursiveDeleteDirectory(path, Verbose);
             } else {
-                // delete files in fileset
+                // dBelete files in fileset
                 if (DeleteFileSet.DirectoryNames.Count == 0) {
-                    Log(Level.Info, LogPrefix + "Deleting {0} files.", DeleteFileSet.FileNames.Count);
+                    Log(Level.Info, 
+                        LogPrefix + "Deleting '{0}' files.", 
+                        DeleteFileSet.FileNames.Count);
                 } else if (DeleteFileSet.FileNames.Count == 0) {
-                    Log(Level.Info, LogPrefix + "Deleting {0} directories.", DeleteFileSet.DirectoryNames.Count);
+                    Log(Level.Info, 
+                        LogPrefix + "Deleting '{0}' directories.", 
+                        DeleteFileSet.DirectoryNames.Count);
                 } else {
-                    Log(Level.Info, LogPrefix + "Deleting {0} files and {1} directories.", DeleteFileSet.FileNames.Count, DeleteFileSet.DirectoryNames.Count);
+                    Log(Level.Info, 
+                        LogPrefix + "Deleting '{0}' files and '{1}' " + 
+                        "directories.",
+                        DeleteFileSet.FileNames.Count, 
+                        DeleteFileSet.DirectoryNames.Count);
                 }
 
                 foreach (string path in DeleteFileSet.FileNames) {
                     DeleteFile(path, Verbose);
                 }
                 foreach (string path in DeleteFileSet.DirectoryNames) {
-                    if (Directory.Exists(path)) {
-                        RecursiveDeleteDirectory(path);
-                    }
+                    RecursiveDeleteDirectory(path, Verbose);
                 }
             }
         }
@@ -175,37 +182,50 @@
 
         #region Private Instance Methods
 
-        private void RecursiveDeleteDirectory(string path) {
+        private void RecursiveDeleteDirectory(string path, bool verbose) {
             try {
                 // First, recursively delete all directories in the directory
-                string[] dirs = Directory.GetDirectories(path);
-                foreach (string dir in dirs)
-                    RecursiveDeleteDirectory(dir);
-
-                // Next, delete all files in the directory
-                string[] files = Directory.GetFiles(path);
-                foreach (string file in files) {
-                    try {
-                        File.SetAttributes(file, FileAttributes.Normal);
-                        Log(Level.Verbose, LogPrefix + "Deleting file {0}.", file);
-                        File.Delete(file);
-                    } catch (Exception e) {
-                        string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete file {0}.", file);
-                        if (FailOnError) {
-                            throw new BuildException(msg, Location, e);
-                        }
-                        Log(Level.Verbose, LogPrefix + msg);
+                if (Directory.Exists(path)) {
+		    string[] dirs = Directory.GetDirectories(path);
+		    foreach (string dir in dirs) {
+			RecursiveDeleteDirectory(dir, verbose);
+		    }
+
+		    // Next, delete all files in the directory
+		    string[] files = Directory.GetFiles(path);
+		    foreach (string file in files) {
+			try {
+			    Log(Level.Verbose, 
+				LogPrefix + "Deleting file '{0}'.", 
+				file);
+			    DeleteFile(file, true);
+			} catch (Exception e) {
+			    string msg = 
+				String.Format(CultureInfo.InvariantCulture, 
+					      "Cannot delete file '{0}'.", 
+					      file);
+			    Log(Level.Verbose, LogPrefix + msg);
+			    if (FailOnError) {
+				throw new BuildException(msg, Location, e);
+			    }
+			}
+		    }
+
+		    // Finally, delete the directory
+		    File.SetAttributes(path, FileAttributes.Normal);
+                    if (verbose) {
+                        Log(Level.Info, 
+                            LogPrefix + "Deleting directory '{0}'.", 
+                            path);
                     }
+		    Directory.Delete(path);
                 }
-
-                // Finally, delete the directory
-                File.SetAttributes(path, FileAttributes.Normal);
-                Log(Level.Verbose, LogPrefix + "Deleting directory {0}.", path);
-                Directory.Delete(path);
             } catch (BuildException e) {
                 throw e;
             } catch (Exception e) {
-                string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete directory {0}.", path);
+                string msg = String.Format(CultureInfo.InvariantCulture, 
+                                           "Cannot delete directory '{0}'.", 
+                                           path);
                 if (FailOnError) {
                     throw new BuildException(msg, Location, e);
                 }
@@ -218,17 +238,27 @@
                 FileInfo deleteInfo = new FileInfo(path);
                 if (deleteInfo.Exists) {
                     if (verbose) {
-                        Log(Level.Info, LogPrefix + "Deleting file {0}.", path);
+                        Log(Level.Info, 
+                            LogPrefix + "Deleting file '{0}'.", 
+                            path);
                     }
                     if (deleteInfo.Attributes != FileAttributes.Normal) {
-                        File.SetAttributes(deleteInfo.FullName, FileAttributes.Normal);
+                        File.SetAttributes(deleteInfo.FullName, 
+                                           FileAttributes.Normal);
                     }
                     File.Delete(path);
                 } else {
-                    throw new FileNotFoundException();
+                    string msg = 
+                        String.Format(CultureInfo.InvariantCulture, 
+                                      "Cannot delete file '{0}' because it " +
+                                      "does not exist.",
+                                      path);
+                    throw new BuildException(msg, Location);
                 }
             } catch (Exception e) {
-                string msg = String.Format(CultureInfo.InvariantCulture, "Cannot delete file {0}.", path);
+                string msg = String.Format(CultureInfo.InvariantCulture, 
+                                           "Cannot delete file '{0}'.", 
+                                           path);
                 if (FailOnError) {
                     throw new BuildException(msg, Location, e);
                 }

Reply via email to