public void CreateDirectory(string path) { if (!DirectoryExists(path)) { var change = FileSystemChanges.FirstOrDefault(f => f.FullPath.Equals(path, PathComparisonType) && f.FileSystemChangeType == FileSystemChangeType.RemoveDirectory); if (change != null) { // If the directory was deleted, just remove the change. // The directory could have been deleted to remove its children then added back. FileSystemChangeTracker.RemoveChange(change); } else { FileSystemChangeInformation addDirectoryInformation = new FileSystemChangeInformation() { FullPath = path, FileSystemChangeType = FileSystemChangeType.AddDirectory }; FileSystemChangeTracker.AddChange(addDirectoryInformation); } var deletedParents = FileSystemChanges.Where(f => path.StartsWith(f.FullPath) && f.FileSystemChangeType == FileSystemChangeType.RemoveDirectory); foreach (var deletedParent in deletedParents) { FileSystemChangeTracker.RemoveChange(deletedParent); } } }
public void RemoveDirectory(string path, bool recursive) { if (!DirectoryExists(path)) { throw new IOException(string.Format(MessageStrings.PathNotFound, path)); } var change = FileSystemChanges.FirstOrDefault(f => f.FullPath.Equals(path, PathComparisonType)); var subDirectoryChanges = FileSystemChanges.Where(f => f.FullPath.StartsWith(path, PathComparisonType)); if (!recursive) { if (change != null && change.FileSystemChangeType == FileSystemChangeType.AddDirectory) { if (subDirectoryChanges.Any()) { throw new IOException(string.Format(MessageStrings.DirectoryNotEmpty, path)); } FileSystemChangeTracker.RemoveChange(change); } else { if (EnumerateFiles(path, "*", SearchOption.AllDirectories).Any() || EnumerateDirectories(path, "*", SearchOption.AllDirectories).Any()) { throw new IOException(string.Format(MessageStrings.DirectoryNotEmpty, path)); } } } else { if (change != null && change.FileSystemChangeType == FileSystemChangeType.AddDirectory) { // All changes here then should be just additions. FileSystemChangeTracker.RemoveChanges(subDirectoryChanges); } else { var files = EnumerateFiles(path, "*", SearchOption.AllDirectories); foreach (var file in files) { DeleteFile(file); } var subDirs = EnumerateDirectories(path, "*", SearchOption.AllDirectories); foreach (var subDir in subDirs) { RemoveDirectory(subDir, false); } } } }
/// <summary> /// Adds a piece of metadata to communicate back to the scaffolding server. /// This currently uses the FileSystemChangeInformation message. /// At some point an additional message type should be added specifically for metadata exchange. /// </summary> /// <param name="metadata">The data to send back. It will be put in the FullPath property of the message.</param> public void AddMetadataMessage(string metadata) { FileSystemChangeInformation metadataInfo = new FileSystemChangeInformation() { FullPath = metadata, FileSystemChangeType = FileSystemChangeType.AddFile, FileContents = string.Empty }; FileSystemChangeTracker.AddChange(metadataInfo); }
public void WriteAllText(string path, string contents) { if (!DirectoryExists(Path.GetDirectoryName(path))) { throw new IOException(string.Format(MessageStrings.PathNotFound, path)); } var fileWriteInformation = new FileSystemChangeInformation() { FullPath = path, FileSystemChangeType = FileSystemChangeType.AddFile, FileContents = contents }; if (FileExists(path)) { fileWriteInformation.FileSystemChangeType = FileSystemChangeType.EditFile; } FileSystemChangeTracker.AddChange(fileWriteInformation); }
public void DeleteFile(string path) { if (!FileExists(path)) { throw new IOException(string.Format(MessageStrings.PathNotFound, path)); } var change = FileSystemChanges.FirstOrDefault(f => f.FullPath.Equals(path, PathComparisonType) && f.FileSystemChangeType == FileSystemChangeType.AddFile); if (change != null) { FileSystemChangeTracker.RemoveChange(change); } else { FileSystemChangeTracker.AddChange(new FileSystemChangeInformation() { FullPath = path, FileSystemChangeType = FileSystemChangeType.DeleteFile }); } }