1 // NAnt - A .NET build tool
2 // Copyright (C) 2001 Gerry Shaw
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // Gerry Shaw (gerry_shaw@yahoo.com)
21 // TODO: move this into the task documentation (once we figure out how tasks
22 // should be documented - xml??
25 verbose: Show name of each deleted file ("true"/"false"). Default is "false"
28 quiet: If the file does not exist, do not display a diagnostic message or
29 modify the exit status to reflect an error (unless Ant has been invoked with
30 the -verbose or -debug switches). This means that if a file or directory cannot
31 be deleted, then no error is reported. This setting emulates the -f option to
32 the Unix "rm" command. ("true"/"false"). Default is "false" meaning things are
33 "noisy". Setting this to true, implies setting failonerror to false.
35 failonerror: This flag (which is only of relevance if 'quiet' is false),
36 controls whether an error -such as a failure to delete a file- stops the build
37 task, or is merely reported to the screen. The default is "true"
41 namespace SourceForge
.NAnt
{
47 public class DeleteTask
: Task
{
49 [TaskAttribute("file")]
52 [TaskAttribute("dir")]
55 [TaskAttribute("verbose")]
57 string _verbose
= Boolean
.FalseString
;
59 [TaskAttribute("failonerror")]
61 string _failOnError
= Boolean
.TrueString
;
63 /// <summary>If true then delete empty directories when using filesets.</summary>
64 [TaskAttribute("includeEmptyDirs")]
66 string _includeEmptyDirs
= Boolean
.FalseString
;
68 [TaskFileSet("fileset")]
69 FileSet _fileset
= new FileSet(false);
71 public string FileName { get { return _file; }
}
72 public string DirectoryName { get { return _dir; }
}
73 public bool FailOnError { get { return Convert.ToBoolean(_failOnError); }
}
74 public bool IncludeEmptyDirectories { get { return Convert.ToBoolean(_includeEmptyDirs); }
}
75 public FileSet DeleteFileSet { get { return _fileset; }
}
79 return (Project
.Verbose
|| Convert
.ToBoolean(_verbose
));
83 protected override void ExecuteTask() {
85 // limit task to deleting either a file or a directory or a file set
86 if (FileName
!= null && DirectoryName
!= null) {
87 throw new BuildException("Cannot specify 'file' and 'dir' in the same delete task", Location
);
90 // try to delete specified file
91 if (FileName
!= null) {
94 path
= Project
.GetFullPath(FileName
);
95 } catch (Exception e
) {
96 string msg
= String
.Format("Could not determine path from {0}", FileName
);
97 throw new BuildException(msg
, Location
, e
);
101 // try to delete specified directory
102 } else if (DirectoryName
!= null) {
105 path
= Project
.GetFullPath(DirectoryName
);
106 } catch (Exception e
) {
107 string msg
= String
.Format("Could not determine path from {0}", DirectoryName
);
108 throw new BuildException(msg
, Location
, e
);
110 DeleteDirectory(path
);
112 // delete files/directories in fileset
114 // only use the file set if file and dir attributes have NOT been set
115 foreach (string path
in DeleteFileSet
.FileNames
) {
119 if (IncludeEmptyDirectories
) {
120 foreach (string path
in DeleteFileSet
.DirectoryNames
) {
121 // only delete EMPTY directories (no files, no directories)
122 DirectoryInfo dirInfo
= new DirectoryInfo(path
);
124 if ((dirInfo
.GetFiles().Length
== 0) && (dirInfo
.GetDirectories().Length
== 0)) {
125 DeleteDirectory(path
);
132 void DeleteDirectory(string path
) {
134 if (Directory
.Exists(path
)) {
136 Log
.WriteLine(LogPrefix
+ "Deleting directory {0}", path
);
138 if (path
.Length
> 10) {
139 Directory
.Delete(path
, true);
141 // TODO: remove this once this task is fully tested and NAnt is at 1.0
142 Console
.WriteLine(LogPrefix
+ "Path {0} is too close to root to delete this early in development", path
);
145 throw new DirectoryNotFoundException();
147 } catch (Exception e
) {
149 string msg
= String
.Format("Cannot delete directory {0}", path
);
150 throw new BuildException(msg
, Location
, e
);
155 void DeleteFile(string path
) {
157 if (File
.Exists(path
)) {
159 Log
.WriteLine(LogPrefix
+ "Deleting file {0}", path
);
163 throw new FileNotFoundException();
165 } catch (Exception e
) {
167 string msg
= String
.Format("Cannot delete file {0}", path
);
168 throw new BuildException(msg
, Location
, e
);