Skip to content

Commit ac3dd9c

Browse files
committed
TemplateLoadService: route all view caching to a single class
1 parent 89bf429 commit ac3dd9c

File tree

10 files changed

+67
-64
lines changed

10 files changed

+67
-64
lines changed

ExtentReports/Reporter/ExtentHtmlReporter.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AventStack.ExtentReports.Core;
22
using AventStack.ExtentReports.Reporter.Configuration;
3+
using AventStack.ExtentReports.Reporter.TemplateEngine;
34
using AventStack.ExtentReports.Views.Html;
45

56
using RazorEngine.Templating;
@@ -86,22 +87,7 @@ private void AddTemplates()
8687
"Partials.Sidenav"
8788
};
8889

89-
foreach (string template in templates)
90-
{
91-
string resourceName = typeof(IHtmlMarker).Namespace + "." + template + ".cshtml";
92-
using (var resourceStream = typeof(IHtmlMarker).Assembly.GetManifestResourceStream(resourceName))
93-
{
94-
using (var reader = new StreamReader(resourceStream))
95-
{
96-
if (resourceStream != null)
97-
{
98-
var arr = template.Split('.');
99-
var name = arr.Length > 1 ? arr[arr.Length - 1] : arr[0];
100-
RazorEngineManager.Instance.Razor.AddTemplate(name, reader.ReadToEnd());
101-
}
102-
}
103-
}
104-
}
90+
TemplateLoadService.LoadTemplate<IHtmlMarker>(templates);
10591
}
10692
}
10793
}

ExtentReports/Reporter/ExtentLoggerReporter.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AventStack.ExtentReports.Core;
22
using AventStack.ExtentReports.Reporter.Configuration;
3+
using AventStack.ExtentReports.Reporter.TemplateEngine;
34
using AventStack.ExtentReports.Views.Commons;
45
using AventStack.ExtentReports.Views.Logger;
56

@@ -35,10 +36,16 @@ public override void Flush(ReportAggregates reportAggregates)
3536
File.WriteAllText(SavePath + "Index.html", source);
3637
source = RazorEngineManager.Instance.Razor.RunCompile("LoggerDashboard", typeof(ExtentLoggerReporter), this);
3738
File.WriteAllText(SavePath + "Dashboard.html", source);
38-
source = RazorEngineManager.Instance.Razor.RunCompile("LoggerTag", typeof(ExtentLoggerReporter), this);
39-
File.WriteAllText(SavePath + "Tag.html", source);
40-
source = RazorEngineManager.Instance.Razor.RunCompile("LoggerException", typeof(ExtentLoggerReporter), this);
41-
File.WriteAllText(SavePath + "Exception.html", source);
39+
if (CategoryContext.Context.Count > 0)
40+
{
41+
source = RazorEngineManager.Instance.Razor.RunCompile("LoggerTag", typeof(ExtentLoggerReporter), this);
42+
File.WriteAllText(SavePath + "Tag.html", source);
43+
}
44+
if (ExceptionInfoContext.Context.Count > 0)
45+
{
46+
source = RazorEngineManager.Instance.Razor.RunCompile("LoggerException", typeof(ExtentLoggerReporter), this);
47+
File.WriteAllText(SavePath + "Exception.html", source);
48+
}
4249
}
4350

4451
public override void Start()
@@ -47,7 +54,7 @@ public override void Start()
4754
AddTemplates();
4855
}
4956

50-
private void AddTemplates()
57+
private static void AddTemplates()
5158
{
5259
string[] templates = new string[]
5360
{
@@ -60,7 +67,7 @@ private void AddTemplates()
6067
"Partials.LoggerNavRight",
6168
"LoggerMacro"
6269
};
63-
AddTemplates<ILoggerMarker>(templates);
70+
TemplateLoadService.LoadTemplate<ILoggerMarker>(templates);
6471

6572
templates = new string[]
6673
{
@@ -74,27 +81,7 @@ private void AddTemplates()
7481
"CommonsRow",
7582
"CommonsTag"
7683
};
77-
AddTemplates<ICommonsMarker>(templates);
78-
}
79-
80-
private void AddTemplates<T>(string[] templates)
81-
{
82-
foreach (string template in templates)
83-
{
84-
var resourceName = typeof(T).Namespace + "." + template + ".cshtml";
85-
using (var resourceStream = typeof(T).Assembly.GetManifestResourceStream(resourceName))
86-
{
87-
using (var reader = new StreamReader(resourceStream))
88-
{
89-
if (resourceStream != null)
90-
{
91-
var arr = template.Split('.');
92-
var name = arr.Length > 1 ? arr[arr.Length - 1] : arr[0];
93-
RazorEngineManager.Instance.Razor.AddTemplate(name, reader.ReadToEnd());
94-
}
95-
}
96-
}
97-
}
84+
TemplateLoadService.LoadTemplate<ICommonsMarker>(templates);
9885
}
9986
}
10087
}

ExtentReports/Reporter/ExtentV3HtmlReporter.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using AventStack.ExtentReports.Core;
22
using AventStack.ExtentReports.Views.V3Html;
33
using AventStack.ExtentReports.Reporter.Configuration;
4+
using AventStack.ExtentReports.Reporter.TemplateEngine;
45

56
using RazorEngine.Templating;
67

@@ -30,6 +31,7 @@ public ExtentV3HtmlReporter(string filePath) : base(filePath)
3031

3132
public override void Flush(ReportAggregates reportAggregates)
3233
{
34+
Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
3335
base.Flush(reportAggregates);
3436
var source = RazorEngineManager.Instance.Razor.RunCompile("V3Index", typeof(ExtentV3HtmlReporter), this);
3537
File.WriteAllText(SavePath, source);
@@ -57,22 +59,7 @@ private void AddTemplates()
5759
"TestRunner.V3Logs"
5860
};
5961

60-
foreach (string template in templates)
61-
{
62-
string resourceName = typeof(IV3HtmlMarker).Namespace + "." + template + ".cshtml";
63-
using (var resourceStream = typeof(IV3HtmlMarker).Assembly.GetManifestResourceStream(resourceName))
64-
{
65-
using (var reader = new StreamReader(resourceStream))
66-
{
67-
if (resourceStream != null)
68-
{
69-
var arr = template.Split('.');
70-
var name = arr.Length > 1 ? arr[arr.Length - 1] : arr[0];
71-
RazorEngineManager.Instance.Razor.AddTemplate(name, reader.ReadToEnd());
72-
}
73-
}
74-
}
75-
}
62+
TemplateLoadService.LoadTemplate<IV3HtmlMarker>(templates);
7663
}
7764
}
7865
}

ExtentReports/Reporter/TemplateEngine/RazorEngineManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
using System;
77

8-
namespace AventStack.ExtentReports.Reporter
8+
namespace AventStack.ExtentReports.Reporter.TemplateEngine
99
{
1010
internal sealed class RazorEngineManager
1111
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using AventStack.ExtentReports.Views;
2+
3+
using RazorEngine.Templating;
4+
5+
using System.IO;
6+
7+
namespace AventStack.ExtentReports.Reporter.TemplateEngine
8+
{
9+
internal static class TemplateLoadService
10+
{
11+
public static void LoadTemplate<T>(string template) where T : IViewsMarker
12+
{
13+
LoadTemplate<T>(new[] { template });
14+
}
15+
16+
public static void LoadTemplate<T>(string[] templates) where T : IViewsMarker
17+
{
18+
foreach (string template in templates)
19+
{
20+
string resourceName = typeof(T).Namespace + "." + template + ".cshtml";
21+
using (var resourceStream = typeof(T).Assembly.GetManifestResourceStream(resourceName))
22+
{
23+
using (var reader = new StreamReader(resourceStream))
24+
25+
{
26+
if (resourceStream != null)
27+
{
28+
var arr = template.Split('.');
29+
var name = arr.Length > 1 ? arr[arr.Length - 1] : arr[0];
30+
RazorEngineManager.Instance.Razor.AddTemplate(name, reader.ReadToEnd());
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AventStack.ExtentReports.Views.Commons
22
{
3-
public interface ICommonsMarker
3+
internal interface ICommonsMarker : IViewsMarker
44
{
55
}
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AventStack.ExtentReports.Views.Html
22
{
3-
internal interface IHtmlMarker
3+
internal interface IHtmlMarker : IViewsMarker
44
{
55
}
66
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace AventStack.ExtentReports.Views
2+
{
3+
internal interface IViewsMarker
4+
{
5+
}
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AventStack.ExtentReports.Views.Logger
22
{
3-
internal interface ILoggerMarker
3+
internal interface ILoggerMarker : IViewsMarker
44
{
55
}
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AventStack.ExtentReports.Views.V3Html
22
{
3-
internal interface IV3HtmlMarker
3+
internal interface IV3HtmlMarker : IViewsMarker
44
{
55
}
66
}

0 commit comments

Comments
 (0)