public static void Delete(ICakeContext context, DirectoryPath path, DeleteDirectorySettings settings) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (path == null) { throw new ArgumentNullException(nameof(path)); } if (path.IsRelative) { path = path.MakeAbsolute(context.Environment); } var directory = context.FileSystem.GetDirectory(path); if (!directory.Exists) { const string format = "The directory '{0}' does not exist."; throw new IOException(string.Format(CultureInfo.InvariantCulture, format, path.FullPath)); } var hasDirectories = directory.GetDirectories("*", SearchScope.Current).Any(); var hasFiles = directory.GetFiles("*", SearchScope.Current).Any(); if (!settings.Recursive && (hasDirectories || hasFiles)) { const string format = "Cannot delete directory '{0}' without recursion since it's not empty."; throw new IOException(string.Format(CultureInfo.InvariantCulture, format, path.FullPath)); } context.Log.Verbose("Deleting directory {0}", path); if (settings.Force) { context.Log.Verbose("Removing ReadOnly attributes on all files in directory {0}", path); var searchOption = settings.Recursive ? SearchScope.Recursive : SearchScope.Current; foreach (var file in directory.GetFiles("*", searchOption)) { file.Attributes &= ~FileAttributes.ReadOnly; } } directory.Delete(settings.Recursive); }
public static void DeleteDirectory(this ICakeContext context, DirectoryPath path, DeleteDirectorySettings settings) { DirectoryDeleter.Delete(context, path, settings); }
public static void DeleteDirectories(this ICakeContext context, IEnumerable <string> directories, DeleteDirectorySettings settings) { if (directories == null) { throw new ArgumentNullException(nameof(directories)); } var paths = directories.Select(p => new DirectoryPath(p)); foreach (var directory in paths) { DeleteDirectory(context, directory, settings); } }
public static void DeleteDirectories(this ICakeContext context, IEnumerable <DirectoryPath> directories, DeleteDirectorySettings settings) { if (directories == null) { throw new ArgumentNullException(nameof(directories)); } foreach (var directory in directories) { DeleteDirectory(context, directory, settings); } }
public static void EnsureDirectoryDoesNotExist(this ICakeContext context, DirectoryPath path, DeleteDirectorySettings settings) { if (context.DirectoryExists(path)) { context.DeleteDirectory(path, settings); } }