// Deletes an empty folder from disk and the project private static void DeleteDirectory(IMSBuildNuGetProjectSystem projectSystem, string path) { var fullPath = Path.Combine(projectSystem.ProjectFullPath, path); if (!Directory.Exists(fullPath)) { return; } // Only delete this folder if it is empty and we didn't specify that we want to recurse if (GetFiles(projectSystem, path, "*.*", recursive: false).Any() || GetDirectories(projectSystem, path).Any()) { projectSystem.NuGetProjectContext.Log(MessageLevel.Warning, Strings.Warning_DirectoryNotEmpty, path); return; } projectSystem.RegisterProcessedFiles(new[] { path }); projectSystem.DeleteDirectory(path, recursive: false); // Workaround for update-package TFS issue. If we're bound to TFS, do not try and delete directories. var sourceControlManager = SourceControlUtility.GetSourceControlManager(projectSystem.NuGetProjectContext); if (sourceControlManager != null) { // Source control bound, do not delete return; } // For potential project systems that do not remove items from disk, we delete the folder directly // There is no actual scenario where we know this is broken without the code below, but since the // code was always there, we are leaving it behind for now. if (!Directory.Exists(fullPath)) { Directory.Delete(fullPath, recursive: false); // The directory is not guaranteed to be gone since there could be // other open handles. Wait, up to half a second, until the directory is gone. for (var i = 0; Directory.Exists(fullPath) && i < 5; ++i) { Thread.Sleep(100); } projectSystem.RegisterProcessedFiles(new[] { path }); projectSystem.NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_RemovedFolder, fullPath); } }
public static void DeleteDirectory(IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem, string path, bool recursive) { var fullPath = Path.Combine(msBuildNuGetProjectSystem.ProjectFullPath, path); if (!Directory.Exists(fullPath)) { return; } // Only delete this folder if it is empty and we didn't specify that we want to recurse if (!recursive && (GetFiles(msBuildNuGetProjectSystem, path, "*.*", recursive).Any() || GetDirectories(msBuildNuGetProjectSystem, path).Any())) { msBuildNuGetProjectSystem.NuGetProjectContext.Log(MessageLevel.Warning, Strings.Warning_DirectoryNotEmpty, path); return; } msBuildNuGetProjectSystem.DeleteDirectory(path, recursive); // Workaround for update-package TFS issue. If we're bound to TFS, do not try and delete directories. var sourceControlManager = SourceControlUtility.GetSourceControlManager(msBuildNuGetProjectSystem.NuGetProjectContext); if (sourceControlManager != null) { // Source control bound, do not delete return; } try { Directory.Delete(fullPath, recursive); // The directory is not guaranteed to be gone since there could be // other open handles. Wait, up to half a second, until the directory is gone. for (int i = 0; Directory.Exists(fullPath) && i < 5; ++i) { System.Threading.Thread.Sleep(100); } msBuildNuGetProjectSystem.RemoveFile(path); msBuildNuGetProjectSystem.NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_RemovedFolder, fullPath); } catch (DirectoryNotFoundException) { } }
public static void DeleteDirectory(IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem, string path, bool recursive) { var fullPath = Path.Combine(msBuildNuGetProjectSystem.ProjectFullPath, path); if (!Directory.Exists(fullPath)) { return; } // Only delete this folder if it is empty and we didn't specify that we want to recurse if (!recursive && (GetFiles(msBuildNuGetProjectSystem, path, "*.*", recursive).Any() || GetDirectories(msBuildNuGetProjectSystem, path).Any())) { msBuildNuGetProjectSystem.NuGetProjectContext.Log(MessageLevel.Warning, Strings.Warning_DirectoryNotEmpty, path); return; } msBuildNuGetProjectSystem.DeleteDirectory(path, recursive); // Workaround for update-package TFS issue. If we're bound to TFS, do not try and delete directories. var sourceControlManager = SourceControlUtility.GetSourceControlManager(msBuildNuGetProjectSystem.NuGetProjectContext); if(sourceControlManager != null) { // Source control bound, do not delete return; } try { Directory.Delete(fullPath, recursive); // The directory is not guaranteed to be gone since there could be // other open handles. Wait, up to half a second, until the directory is gone. for (int i = 0; Directory.Exists(fullPath) && i < 5; ++i) { System.Threading.Thread.Sleep(100); } msBuildNuGetProjectSystem.RemoveFile(path); msBuildNuGetProjectSystem.NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_RemovedFolder, fullPath); } catch (DirectoryNotFoundException) { } }