| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text.RegularExpressions; |
| | | 5 | | using DirectSight.Common; |
| | | 6 | | |
| | | 7 | | namespace DirectSight.Parser.FileReading; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// File reader for reading files from local disk. |
| | | 11 | | /// </summary> |
| | | 12 | | internal class LocalFileReader : IFileReader |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Regex to analyze if a path is a deterministic path. |
| | | 16 | | /// </summary> |
| | 1 | 17 | | private static readonly Regex DeterministicPathRegex = new Regex("\\/_\\d?\\/", RegexOptions.Compiled); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The source directories for typical environments like Github Actions. |
| | | 21 | | /// </summary> |
| | | 22 | | private static readonly IReadOnlyList<string> DeterministicSourceDirectories; |
| | | 23 | | |
| | | 24 | | static LocalFileReader() |
| | 1 | 25 | | { |
| | 1 | 26 | | var directories = new List<string>(); |
| | | 27 | | |
| | | 28 | | // Github Actions |
| | 1 | 29 | | if ("true".Equals(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"), StringComparison.OrdinalIgnoreCase) |
| | 1 | 30 | | && Environment.GetEnvironmentVariable("GITHUB_WORKSPACE") != null) |
| | 1 | 31 | | { |
| | 1 | 32 | | directories.Add(Environment.GetEnvironmentVariable("GITHUB_WORKSPACE")); |
| | 1 | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | DeterministicSourceDirectories = directories; |
| | 1 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="LocalFileReader" /> class. |
| | | 40 | | /// </summary> |
| | 32 | 41 | | public LocalFileReader() |
| | 32 | 42 | | { |
| | 32 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Loads the file with the given path. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="path">The path.</param> |
| | | 49 | | /// <param name="error">Error message if file reading failed, otherwise <code>null</code>.</param> |
| | | 50 | | /// <returns><code>null</code> if an error occurs, otherwise the lines of the file.</returns> |
| | | 51 | | public string[] LoadFile(string path, out string error) |
| | 66 | 52 | | { |
| | 66 | 53 | | string mappedPath = MapPath(path); |
| | | 54 | | |
| | | 55 | | try |
| | 66 | 56 | | { |
| | 66 | 57 | | if (!File.Exists(mappedPath)) |
| | 2 | 58 | | { |
| | 2 | 59 | | error = string.Format("File '{0}' does not exist (any more).", path); |
| | 2 | 60 | | return null; |
| | | 61 | | } |
| | | 62 | | |
| | 64 | 63 | | string[] lines = ReadAllLinesPreserveTrailingEmpty(mappedPath); |
| | | 64 | | |
| | 64 | 65 | | error = null; |
| | 64 | 66 | | return lines; |
| | | 67 | | } |
| | 0 | 68 | | catch (Exception ex) |
| | 0 | 69 | | { |
| | 0 | 70 | | error = string.Format("Error during reading file '{0}': {1}", path, ex.GetExceptionMessageForDisplay()); |
| | 0 | 71 | | return null; |
| | | 72 | | } |
| | 66 | 73 | | } |
| | | 74 | | |
| | | 75 | | private static string MapPath(string path) |
| | 66 | 76 | | { |
| | 66 | 77 | | if (File.Exists(path)) |
| | 64 | 78 | | { |
| | 64 | 79 | | return path; |
| | | 80 | | } |
| | | 81 | | |
| | 2 | 82 | | if (path.StartsWith("/_") && DeterministicPathRegex.IsMatch(path)) |
| | 0 | 83 | | { |
| | 0 | 84 | | path = path[(path.IndexOf('/', 2) + 1)..]; |
| | | 85 | | |
| | 0 | 86 | | if (File.Exists(path)) |
| | 0 | 87 | | { |
| | 0 | 88 | | return path; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return MapPath(path, DeterministicSourceDirectories); |
| | | 92 | | } |
| | | 93 | | |
| | 2 | 94 | | return path; |
| | 66 | 95 | | } |
| | | 96 | | |
| | | 97 | | private static string MapPath(string path, IEnumerable<string> directories) |
| | 0 | 98 | | { |
| | | 99 | | /* |
| | | 100 | | * Search in source dirctories |
| | | 101 | | * |
| | | 102 | | * E.g. with source directory 'C:\agent\1\work\s' the following locations will be searched: |
| | | 103 | | * C:\agent\1\work\s\_\some\directory\file.cs |
| | | 104 | | * C:\agent\1\work\s\some\directory\file.cs |
| | | 105 | | * C:\agent\1\work\s\directory\file.cs |
| | | 106 | | * C:\agent\1\work\s\file.cs |
| | | 107 | | */ |
| | 0 | 108 | | string[] parts = path.Split('/', '\\'); |
| | | 109 | | |
| | 0 | 110 | | foreach (var sourceDirectory in directories) |
| | 0 | 111 | | { |
| | 0 | 112 | | for (int i = 0; i < parts.Length; i++) |
| | 0 | 113 | | { |
| | 0 | 114 | | string combinedPath = sourceDirectory; |
| | | 115 | | |
| | 0 | 116 | | for (int j = i; j < parts.Length; j++) |
| | 0 | 117 | | { |
| | 0 | 118 | | combinedPath = Path.Combine(combinedPath, parts[j]); |
| | 0 | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | if (File.Exists(combinedPath)) |
| | 0 | 122 | | { |
| | 0 | 123 | | return combinedPath; |
| | | 124 | | } |
| | 0 | 125 | | } |
| | 0 | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | return path; |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Reads all lines of a file, preserving a trailing empty line if present. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="path">The path of the file.</param> |
| | | 135 | | /// <returns>The lines of the file.</returns> |
| | | 136 | | private static string[] ReadAllLinesPreserveTrailingEmpty(string path) |
| | 64 | 137 | | { |
| | 64 | 138 | | var lines = new List<string>(); |
| | | 139 | | |
| | 64 | 140 | | var encoding = FileHelper.GetEncoding(path); |
| | 64 | 141 | | using (var reader = new StreamReader(path, encoding)) |
| | 64 | 142 | | { |
| | | 143 | | string line; |
| | 3776 | 144 | | while ((line = reader.ReadLine()) != null) |
| | 3712 | 145 | | { |
| | 3712 | 146 | | lines.Add(line); |
| | 3712 | 147 | | } |
| | 64 | 148 | | } |
| | | 149 | | |
| | 64 | 150 | | return [.. lines]; |
| | 64 | 151 | | } |
| | | 152 | | } |