| | 1 | | namespace Songhay; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Static members related to <see cref="System.Reflection"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class ProgramAssemblyUtility |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Returns a <see cref="string"/> |
| | 10 | | /// about the executing assembly. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="targetAssembly"> |
| | 13 | | /// The executing <see cref="Assembly"/>. |
| | 14 | | /// </param> |
| | 15 | | /// <returns>Returns <see cref="string"/></returns> |
| 1 | 16 | | public static string? GetAssemblyInfo(Assembly targetAssembly) => GetAssemblyInfo(targetAssembly, false); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Returns a <see cref="string"/> |
| | 20 | | /// about the executing assembly. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="targetAssembly"> |
| | 23 | | /// The executing <see cref="Assembly"/>. |
| | 24 | | /// </param> |
| | 25 | | /// <param name="useConsoleChars"> |
| | 26 | | /// When <c>true</c> selected “special” characters are formatted for the Windows Console. |
| | 27 | | /// </param> |
| | 28 | | /// <returns>Returns <see cref="string"/></returns> |
| | 29 | | public static string? GetAssemblyInfo(Assembly? targetAssembly, bool useConsoleChars) |
| 1 | 30 | | { |
| 1 | 31 | | var sb = new StringBuilder(); |
| | 32 | |
|
| 1 | 33 | | var info = new ProgramAssemblyInfo(targetAssembly); |
| | 34 | |
|
| 1 | 35 | | sb.Append($"{info.AssemblyTitle} {info.AssemblyVersion}{Environment.NewLine}"); |
| 1 | 36 | | sb.Append(info.AssemblyDescription); |
| 1 | 37 | | sb.Append(Environment.NewLine); |
| 1 | 38 | | sb.Append(info.AssemblyCopyright); |
| 1 | 39 | | sb.Append(Environment.NewLine); |
| | 40 | |
|
| 1 | 41 | | return useConsoleChars ? ProgramUtility.GetConsoleCharacters(sb.ToString()) : sb.ToString(); |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets the directory name from assembly. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="assembly">The assembly.</param> |
| | 48 | | public static string? GetPathFromAssembly(Assembly? assembly) |
| 55 | 49 | | { |
| 55 | 50 | | ArgumentNullException.ThrowIfNull(assembly); |
| | 51 | |
|
| 55 | 52 | | var root = Path.GetDirectoryName(assembly.Location); |
| | 53 | |
|
| 55 | 54 | | return root; |
| 55 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Gets the path from assembly. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="assembly">The assembly.</param> |
| | 61 | | /// <param name="fileSegment">The file segment.</param> |
| | 62 | | public static string GetPathFromAssembly(Assembly? assembly, string? fileSegment) |
| 25 | 63 | | { |
| 25 | 64 | | fileSegment.ThrowWhenNullOrWhiteSpace(); |
| | 65 | |
|
| 25 | 66 | | fileSegment = ProgramFileUtility.TrimLeadingDirectorySeparatorChars(fileSegment); |
| | 67 | |
|
| 25 | 68 | | if (Path.IsPathRooted(fileSegment)) throw new FormatException("The expected relative path is not here."); |
| | 69 | |
|
| 25 | 70 | | fileSegment = ProgramFileUtility.NormalizePath(fileSegment); |
| | 71 | |
|
| 25 | 72 | | var root = GetPathFromAssembly(assembly); |
| 25 | 73 | | var levels = ProgramFileUtility.CountParentDirectoryChars(fileSegment); |
| 50 | 74 | | if (levels > 0) root = ProgramFileUtility.GetParentDirectory(root, levels); |
| | 75 | |
|
| 25 | 76 | | var path = ProgramFileUtility.GetCombinedPath(root, fileSegment); |
| | 77 | |
|
| 25 | 78 | | return path; |
| 25 | 79 | | } |
| | 80 | | } |