| | 1 | | using System.Reflection; |
| | 2 | |
|
| | 3 | | namespace DotNetApiDiff.Reporting; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Helper class to load embedded HTML templates and assets |
| | 7 | | /// </summary> |
| | 8 | | public static class EmbeddedTemplateLoader |
| | 9 | | { |
| 1 | 10 | | private static readonly Assembly _assembly = Assembly.GetExecutingAssembly(); |
| 1 | 11 | | private static readonly Dictionary<string, string> _templateCache = new(); |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Loads a template by name from embedded resources |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="templateName">Name of the template file (without extension)</param> |
| | 17 | | /// <returns>Template content as string</returns> |
| | 18 | | public static string LoadTemplate(string templateName) |
| 55 | 19 | | { |
| 55 | 20 | | if (_templateCache.TryGetValue(templateName, out var cached)) |
| 46 | 21 | | { |
| 46 | 22 | | return cached; |
| | 23 | | } |
| | 24 | |
|
| 9 | 25 | | var resourceName = $"DotNetApiDiff.Reporting.HtmlTemplates.{templateName}"; |
| 9 | 26 | | var content = LoadEmbeddedResource(resourceName); |
| 9 | 27 | | _templateCache[templateName] = content; |
| 9 | 28 | | return content; |
| 55 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Loads CSS styles from embedded resources |
| | 33 | | /// </summary> |
| | 34 | | /// <returns>CSS content as string</returns> |
| | 35 | | public static string LoadStyles() |
| 6 | 36 | | { |
| 6 | 37 | | return LoadTemplate("styles.css"); |
| 6 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Loads JavaScript code from embedded resources |
| | 42 | | /// </summary> |
| | 43 | | /// <returns>JavaScript content as string</returns> |
| | 44 | | public static string LoadScripts() |
| 6 | 45 | | { |
| 6 | 46 | | return LoadTemplate("scripts.js"); |
| 6 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Gets all available template names |
| | 51 | | /// </summary> |
| | 52 | | /// <returns>List of template names</returns> |
| | 53 | | public static List<string> GetAvailableTemplates() |
| 0 | 54 | | { |
| 0 | 55 | | var templateNames = new List<string>(); |
| 0 | 56 | | var resourceNames = _assembly.GetManifestResourceNames(); |
| | 57 | |
|
| 0 | 58 | | foreach (var resourceName in resourceNames) |
| 0 | 59 | | { |
| 0 | 60 | | if (resourceName.StartsWith("DotNetApiDiff.Reporting.HtmlTemplates.")) |
| 0 | 61 | | { |
| 0 | 62 | | var templateName = resourceName.Substring("DotNetApiDiff.Reporting.HtmlTemplates.".Length); |
| 0 | 63 | | templateNames.Add(templateName); |
| 0 | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | return templateNames; |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | private static string LoadEmbeddedResource(string resourceName) |
| 9 | 71 | | { |
| 9 | 72 | | using var stream = _assembly.GetManifestResourceStream(resourceName); |
| 9 | 73 | | if (stream == null) |
| 0 | 74 | | { |
| 0 | 75 | | throw new InvalidOperationException($"Could not find embedded resource: {resourceName}"); |
| | 76 | | } |
| | 77 | |
|
| 9 | 78 | | using var reader = new StreamReader(stream); |
| 9 | 79 | | return reader.ReadToEnd(); |
| 9 | 80 | | } |
| | 81 | | } |