< Summary

Information
Class: DotNetApiDiff.Reporting.EmbeddedTemplateLoader
Assembly: DotNetApiDiff
File(s): /home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Reporting/EmbeddedTemplateLoader.cs
Line coverage
60%
Covered lines: 23
Uncovered lines: 15
Coverable lines: 38
Total lines: 81
Line coverage: 60.5%
Branch coverage
37%
Covered branches: 3
Total branches: 8
Branch coverage: 37.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
LoadTemplate(...)100%22100%
LoadStyles()100%11100%
LoadScripts()100%11100%
GetAvailableTemplates()0%2040%
LoadEmbeddedResource(...)50%2275%

File(s)

/home/runner/work/dotnet-api-diff/dotnet-api-diff/src/DotNetApiDiff/Reporting/EmbeddedTemplateLoader.cs

#LineLine coverage
 1using System.Reflection;
 2
 3namespace DotNetApiDiff.Reporting;
 4
 5/// <summary>
 6/// Helper class to load embedded HTML templates and assets
 7/// </summary>
 8public static class EmbeddedTemplateLoader
 9{
 110    private static readonly Assembly _assembly = Assembly.GetExecutingAssembly();
 111    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)
 5519    {
 5520        if (_templateCache.TryGetValue(templateName, out var cached))
 4621        {
 4622            return cached;
 23        }
 24
 925        var resourceName = $"DotNetApiDiff.Reporting.HtmlTemplates.{templateName}";
 926        var content = LoadEmbeddedResource(resourceName);
 927        _templateCache[templateName] = content;
 928        return content;
 5529    }
 30
 31    /// <summary>
 32    /// Loads CSS styles from embedded resources
 33    /// </summary>
 34    /// <returns>CSS content as string</returns>
 35    public static string LoadStyles()
 636    {
 637        return LoadTemplate("styles.css");
 638    }
 39
 40    /// <summary>
 41    /// Loads JavaScript code from embedded resources
 42    /// </summary>
 43    /// <returns>JavaScript content as string</returns>
 44    public static string LoadScripts()
 645    {
 646        return LoadTemplate("scripts.js");
 647    }
 48
 49    /// <summary>
 50    /// Gets all available template names
 51    /// </summary>
 52    /// <returns>List of template names</returns>
 53    public static List<string> GetAvailableTemplates()
 054    {
 055        var templateNames = new List<string>();
 056        var resourceNames = _assembly.GetManifestResourceNames();
 57
 058        foreach (var resourceName in resourceNames)
 059        {
 060            if (resourceName.StartsWith("DotNetApiDiff.Reporting.HtmlTemplates."))
 061            {
 062                var templateName = resourceName.Substring("DotNetApiDiff.Reporting.HtmlTemplates.".Length);
 063                templateNames.Add(templateName);
 064            }
 065        }
 66
 067        return templateNames;
 068    }
 69
 70    private static string LoadEmbeddedResource(string resourceName)
 971    {
 972        using var stream = _assembly.GetManifestResourceStream(resourceName);
 973        if (stream == null)
 074        {
 075            throw new InvalidOperationException($"Could not find embedded resource: {resourceName}");
 76        }
 77
 978        using var reader = new StreamReader(stream);
 979        return reader.ReadToEnd();
 980    }
 81}