| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary>Extensions of <see cref="DirectoryInfo"/>.</summary> |
| | 4 | | public static class DirectoryInfoExtensions |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Finds the specified target <see cref="DirectoryInfo"/> |
| | 8 | | /// under the specified root <see cref="DirectoryInfo"/>. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="directoryInfo">The specified root <see cref="DirectoryInfo"/>.</param> |
| | 11 | | /// <param name="expectedDirectoryName">The specified target <see cref="DirectoryInfo.Name"/>.</param> |
| | 12 | | public static DirectoryInfo? FindDirectory(this DirectoryInfo? directoryInfo, string? expectedDirectoryName) |
| 0 | 13 | | { |
| 0 | 14 | | expectedDirectoryName.ThrowWhenNullOrWhiteSpace(); |
| | 15 | |
|
| 0 | 16 | | if (directoryInfo == null) |
| 0 | 17 | | throw new DirectoryNotFoundException("The expected root directory is not here."); |
| | 18 | |
|
| 0 | 19 | | if (!directoryInfo.Exists) |
| 0 | 20 | | throw new DirectoryNotFoundException("The expected root directory does not exist."); |
| | 21 | |
|
| 0 | 22 | | return directoryInfo.GetDirectories(expectedDirectoryName).FirstOrDefault(); |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Finds the specified <see cref="FileInfo"/> |
| | 27 | | /// under the specified <see cref="DirectoryInfo"/>. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param> |
| | 30 | | /// <param name="expectedFileName">The specified <see cref="FileInfo.Name"/>.</param> |
| | 31 | | public static FileInfo? FindFile(this DirectoryInfo? directoryInfo, string? expectedFileName) |
| 0 | 32 | | { |
| 0 | 33 | | expectedFileName.ThrowWhenNullOrWhiteSpace(); |
| | 34 | |
|
| 0 | 35 | | if (directoryInfo == null) |
| 0 | 36 | | throw new DirectoryNotFoundException("The expected directory is not here."); |
| | 37 | |
|
| 0 | 38 | | if (!directoryInfo.Exists) |
| 0 | 39 | | throw new DirectoryNotFoundException("The expected directory does not exist."); |
| | 40 | |
|
| 0 | 41 | | return directoryInfo.GetFiles(expectedFileName).FirstOrDefault(); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary>Gets the parent directory.</summary> |
| | 45 | | /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param> |
| | 46 | | /// <param name="levels">The levels.</param> |
| | 47 | | /// <returns>Returns a <see cref="string"/> representing the directory.</returns> |
| | 48 | | public static string? GetParentDirectory(this DirectoryInfo? directoryInfo, int levels) => |
| 24 | 49 | | directoryInfo.GetParentDirectoryInfo(levels)?.FullName; |
| | 50 | |
|
| | 51 | | /// <summary>Gets the parent <see cref="DirectoryInfo"/>.</summary> |
| | 52 | | /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param> |
| | 53 | | /// <param name="levels">The levels.</param> |
| | 54 | | public static DirectoryInfo? GetParentDirectoryInfo(this DirectoryInfo? directoryInfo, int levels) |
| 84 | 55 | | { |
| 84 | 56 | | ArgumentNullException.ThrowIfNull(directoryInfo); |
| | 57 | |
|
| 84 | 58 | | levels = Math.Abs(levels); |
| 85 | 59 | | if (levels == 0) return directoryInfo; |
| | 60 | |
|
| 83 | 61 | | var parentDirectoryInfo = directoryInfo.Parent; |
| 83 | 62 | | if (parentDirectoryInfo == null) return directoryInfo; |
| | 63 | |
|
| 83 | 64 | | --levels; |
| | 65 | |
|
| 83 | 66 | | return levels >= 1 ? parentDirectoryInfo.GetParentDirectoryInfo(levels) : parentDirectoryInfo; |
| 84 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Combines path and root based |
| | 71 | | /// on the current value of <see cref="Path.DirectorySeparatorChar"/> |
| | 72 | | /// of the current OS or passes through a drive-letter rooted path.</summary> |
| | 73 | | /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param> |
| | 74 | | /// <param name="path">The path.</param> |
| | 75 | | /// <remarks> |
| | 76 | | /// For detail, see https://github.com/BryanWilhite/SonghayCore/issues/14 |
| | 77 | | /// and <see cref="ProgramFileUtility.GetCombinedPath(string, string)" />. |
| | 78 | | /// </remarks> |
| | 79 | | public static string ToCombinedPath(this DirectoryInfo? directoryInfo, string? path) |
| 28 | 80 | | { |
| 28 | 81 | | ArgumentNullException.ThrowIfNull(directoryInfo); |
| | 82 | |
|
| 28 | 83 | | return ProgramFileUtility.GetCombinedPath(directoryInfo.FullName, path); |
| 28 | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Verifies the specified <see cref="DirectoryInfo"/> |
| | 88 | | /// with conventional error handling. |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param> |
| | 91 | | /// <param name="expectedDirectoryName">The expected directory name.</param> |
| | 92 | | public static void VerifyDirectory(this DirectoryInfo? directoryInfo, string? expectedDirectoryName) |
| 0 | 93 | | { |
| 0 | 94 | | if (directoryInfo == null) |
| 0 | 95 | | throw new DirectoryNotFoundException("The expected directory is not here."); |
| | 96 | |
|
| 0 | 97 | | if (!directoryInfo.Exists) |
| 0 | 98 | | throw new DirectoryNotFoundException("The expected directory does not exist."); |
| | 99 | |
|
| 0 | 100 | | if (!directoryInfo.Name.EqualsInvariant(expectedDirectoryName)) |
| 0 | 101 | | throw new DirectoryNotFoundException( |
| 0 | 102 | | $"The expected directory is not here. [actual: {expectedDirectoryName ?? "[name]"}"); |
| 0 | 103 | | } |
| | 104 | | } |