csharp72c0d4fb
| 1 | using System.Diagnostics.CodeAnalysis; |
| 2 | using System.Reflection; |
| 3 | |
| 4 | namespace HybridCodebaseIndex.Core; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// Embedded copies of shipped files (see <c>EmbeddedResource</c> in csproj). |
| 8 | /// Resource name: <c>HybridCodebaseIndex.Core.</c> + relative path with <c>/</c> replaced by <c>.</c>. |
| 9 | /// </summary> |
| 10 | internal static class BundledContent |
| 11 | { |
| 12 | private static readonly Assembly s_assembly = typeof(BundledContent).Assembly; |
| 13 | private const string ResourcePrefix = "HybridCodebaseIndex.Core."; |
| 14 | |
| 15 | internal static bool TryReadEmbeddedText(string relativePath, [NotNullWhen(true)] out string? text) |
| 16 | { |
| 17 | text = null; |
| 18 | var normalized = NormalizeRelative(relativePath); |
| 19 | if (normalized.Length == 0) |
| 20 | return false; |
| 21 | |
| 22 | var name = ResourcePrefix + normalized.Replace('/', '.'); |
| 23 | using var stream = s_assembly.GetManifestResourceStream(name); |
| 24 | if (stream is null) |
| 25 | return false; |
| 26 | using var reader = new StreamReader(stream); |
| 27 | text = reader.ReadToEnd(); |
| 28 | return !string.IsNullOrWhiteSpace(text); |
| 29 | } |
| 30 | |
| 31 | private static string NormalizeRelative(string relativePath) => |
| 32 | relativePath.Replace('\\', '/').TrimStart('/').Trim(); |
| 33 | } |
| 34 | |
| 35 | |