public override IEnumerable <Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos() { if (directory.Exists) { IEnumerable <IFileSystemInfo> fileSystemInfos; try { fileSystemInfos = directory.EnumerateFileSystemInfos("*", System.IO.SearchOption.TopDirectoryOnly); } catch (System.IO.DirectoryNotFoundException) { yield break; } foreach (IFileSystemInfo fileSystemInfo in fileSystemInfos) { if (fileSystemInfo is IDirectoryInfo directoryInfo) { yield return(new DirectoryInfoBaseAbstractionAdapter(directoryInfo)); } else { yield return(new FileInfoBaseAbstractionAdapter((IFileInfo)fileSystemInfo)); } } } }
public static IFileSystemInfo FromIndentedString(IFileSystem fileSystem, string rootPath, string indentedString) { string[] lines = indentedString.Split('\r', '\n'); Stack <int> folderIndents = new Stack <int>(); folderIndents.Push(-1); IDirectoryInfo root = Create(fileSystem, rootPath); IDirectoryInfo directory = root; foreach (string line in lines.Where(l => !string.IsNullOrWhiteSpace(l))) { int indent = GetIndent(line); string name = line.Trim(); bool isFolder = IsFolder(name); if (isFolder) { // Find a folder for current item while (indent <= folderIndents.Peek()) { folderIndents.Pop(); directory = directory.Parent; } string path = Path.Combine(directory.FullName, name.Substring(1, name.Length - 2)); IDirectoryInfo child = Create(fileSystem, path); AddToDirectory(directory, child); folderIndents.Push(indent); directory = child; } else { // Find a folder for current item while (indent <= folderIndents.Peek()) { folderIndents.Pop(); directory = directory.Parent; } string path = Path.Combine(directory.FullName, name); IFileSystemInfo child = FileInfoStubFactory.Create(fileSystem, path); AddToDirectory(directory, child); } } var children = (List <IFileSystemInfo>)root.EnumerateFileSystemInfos(); if (children.Count != 1) { return(root); } var result = children.First(); children.Remove(result); return(result); }
private static void AddToDirectory(IDirectoryInfo directory, IFileSystemInfo child) { var children = (List <IFileSystemInfo>)directory.EnumerateFileSystemInfos(); int index = children.BinarySearch(child, Comparer <IFileSystemInfo> .Create((x, y) => string.Compare(x.FullName, y.FullName, StringComparison.OrdinalIgnoreCase))); if (index >= 0) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Item with name {0} already exists in folder {1}", child.FullName, directory.FullName)); } if (child.TraverseBreadthFirst(f => (f as IDirectoryInfo)?.EnumerateFileSystemInfos()).Contains(directory)) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Circular dependency. Item {0} is a parent of {1} already", child.FullName, directory.FullName)); } var directoryChild = child as IDirectoryInfo; if (directoryChild != null) { var oldParent = (List <IFileSystemInfo>)directoryChild.Parent?.EnumerateFileSystemInfos(); oldParent?.Remove(directoryChild); directoryChild.Parent.Returns(directory); } else { var fileChild = (IFileInfo)child; var oldParent = (List <IFileSystemInfo>)fileChild.Directory?.EnumerateFileSystemInfos(); oldParent?.Remove(fileChild); fileChild.Directory.Returns(directory); } children.Insert(~index, child); }
public static bool Empty(this IDirectoryInfo directoryInfo) => !directoryInfo.EnumerateFileSystemInfos().Any();
public IEnumerable <IFileSystemInfo> EnumerateFileSystemInfos() { return(FileSystem.Path.GetFromFileSystemInfos(_info.EnumerateFileSystemInfos())); }
private static void AddToDirectory(IDirectoryInfo directory, IFileSystemInfo child) { var children = (List<IFileSystemInfo>)directory.EnumerateFileSystemInfos(); int index = children.BinarySearch(child, Comparer<IFileSystemInfo>.Create((x, y) => string.Compare(x.FullName, y.FullName, StringComparison.OrdinalIgnoreCase))); if (index >= 0) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Item with name {0} already exists in folder {1}", child.FullName, directory.FullName)); } if (child.TraverseBreadthFirst(f => (f as IDirectoryInfo)?.EnumerateFileSystemInfos()).Contains(directory)) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Circular dependency. Item {0} is a parent of {1} already", child.FullName, directory.FullName)); } var directoryChild = child as IDirectoryInfo; if (directoryChild != null) { var oldParent = (List<IFileSystemInfo>)directoryChild.Parent?.EnumerateFileSystemInfos(); oldParent?.Remove(directoryChild); directoryChild.Parent.Returns(directory); } else { var fileChild = (IFileInfo)child; var oldParent = (List<IFileSystemInfo>)fileChild.Directory?.EnumerateFileSystemInfos(); oldParent?.Remove(fileChild); fileChild.Directory.Returns(directory); } children.Insert(~index, child); }