public static void DeleteFile(string fullPath, INuGetProjectContext nuGetProjectContext) { if (!File.Exists(fullPath)) { return; } try { MakeWriteable(fullPath); var sourceControlManager = SourceControlUtility.GetSourceControlManager(nuGetProjectContext); if (sourceControlManager != null) { sourceControlManager.PendDeleteFiles(new List <string>() { fullPath }, String.Empty, nuGetProjectContext); } File.Delete(fullPath); string folderPath = Path.GetDirectoryName(fullPath); if (!String.IsNullOrEmpty(folderPath)) { nuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_RemovedFileFromFolder, Path.GetFileName(fullPath), folderPath); } else { nuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_RemovedFile, Path.GetFileName(fullPath)); } } catch (FileNotFoundException) { } }
// HACK: TODO: This is kinda bad that there is a PendAddFiles method here while delete files performs necessary pending and deletion // Need to update package extraction in Packaging to use a filesystem abstraction or' // just return files to be added in a clean form for projectmanagement to do the addition public static void PendAddFiles(IEnumerable <string> addedPackageFiles, string packagesDir, INuGetProjectContext nuGetProjectContext) { var sourceControlManager = SourceControlUtility.GetSourceControlManager(nuGetProjectContext); if (sourceControlManager != null) { sourceControlManager.PendAddFiles(addedPackageFiles, packagesDir, nuGetProjectContext); } }
public static Stream CreateFile(string fullPath, INuGetProjectContext nuGetProjectContext) { if (String.IsNullOrEmpty(fullPath) || String.IsNullOrEmpty(Path.GetFileName(fullPath))) { throw new ArgumentException("fullPath"); } // MakeWriteable(fullPath); SourceControlManager will do that var sourceControlManager = SourceControlUtility.GetSourceControlManager(nuGetProjectContext); if (sourceControlManager != null) { return(sourceControlManager.CreateFile(fullPath, nuGetProjectContext)); } return(CreateFile(fullPath)); }
// 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 DeleteFiles(IEnumerable <ZipFilePair> packageFiles, string packagesDir, INuGetProjectContext nuGetProjectContext) { var filesToDelete = new List <string>(); foreach (var packageFile in packageFiles.Where(e => e.IsInstalled())) { if (ContentEquals(packageFile.FileFullPath, packageFile.PackageEntry.Open)) { MakeWritable(packageFile.FileFullPath); filesToDelete.Add(packageFile.FileFullPath); } else { nuGetProjectContext.Log(MessageLevel.Warning, Strings.Warning_FileModified, packageFile.FileFullPath); } } var sourceControlManager = SourceControlUtility.GetSourceControlManager(nuGetProjectContext); if (sourceControlManager != null && sourceControlManager.IsPackagesFolderBoundToSourceControl()) { sourceControlManager.PendDeleteFiles(filesToDelete, packagesDir, nuGetProjectContext); foreach (var fileToDelete in filesToDelete) { File.Delete(fileToDelete); } } else { // When it is not SourceControl, it is a different scenario altogether // First get all directories that contain files var directoryLookup = filesToDelete.ToLookup(p => Path.GetDirectoryName(p)); // Get all directories that this package may have added var directories = from grouping in directoryLookup from directory in GetDirectories(grouping.Key, altDirectorySeparator: false) orderby directory.Length descending select directory; // Remove files from every directory foreach (var directory in directories) { var directoryFiles = directoryLookup.Contains(directory) ? directoryLookup[directory] : Enumerable.Empty <string>(); var dirPath = Path.Combine(packagesDir, directory); if (!Directory.Exists(dirPath)) { continue; } foreach (var file in directoryFiles) { var path = Path.Combine(packagesDir, file); File.Delete(path); } // If the directory is empty then delete it if (!GetFiles(packagesDir, dirPath, "*.*").Any() && !GetDirectories(packagesDir, dirPath).Any()) { DeleteDirectorySafe(Path.Combine(packagesDir, dirPath), recursive: false, nuGetProjectContext: nuGetProjectContext); } } } }