Skip to content

Commit b744a73

Browse files
authored
Merge pull request #27 from solidify-project/feature/cleanoutput
Clean output folder before rendering new website
2 parents cc90288 + f7a1c15 commit b744a73

8 files changed

Lines changed: 140 additions & 0 deletions

File tree

src/SolidifyProject.Engine.Infrastructure/Interfaces/IContentWriterService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ namespace SolidifyProject.Engine.Infrastructure.Interfaces
55
public interface IContentWriterService<T> where T : class
66
{
77
Task SaveContentAsync(string id, T content);
8+
9+
Task CleanFolderAsync(string path);
810
}
911
}

src/SolidifyProject.Engine.Infrastructure/Services/OrchestrationService.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public sealed class OrchestrationService
2929

3030
public async Task RenderProjectAsync()
3131
{
32+
await CleanOutputAsync();
33+
3234
var tasksGroupContent = Task.Run(async () =>
3335
{
3436
var dataModel = await DataService.GetDataModelAsync();
@@ -87,5 +89,16 @@ private async Task CopyAssetByIdAsync(string id)
8789

8890
await LoggerService.WriteLogMessage($"{DateTime.Now.ToLongTimeString()}: [Asset:Finished] \"{id}\"");
8991
}
92+
93+
private async Task CleanOutputAsync()
94+
{
95+
await LoggerService.WriteLogMessage($"{DateTime.Now.ToLongTimeString()}: Starting cleaning output folder");
96+
97+
await PageModelWriterService.CleanFolderAsync(String.Empty);
98+
99+
await AssetsWriterService.CleanFolderAsync(String.Empty);
100+
101+
await LoggerService.WriteLogMessage($"{DateTime.Now.ToLongTimeString()}: Cleaning output folder finished");
102+
}
90103
}
91104
}

src/SolidifyProject.Engine.Services/ContentWriterService/_FileSystemContentWriterServiceBase.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ public void EnsureDirectoryExists(string path)
2424
Directory.CreateDirectory(directory);
2525
}
2626

27+
public Task CleanFolderAsync(string path)
28+
{
29+
DirectoryInfo di = new DirectoryInfo(Path.Combine(_root, path));
30+
if (di.Exists)
31+
{
32+
foreach (var file in di.GetFiles())
33+
{
34+
file.Delete();
35+
}
36+
37+
foreach (var directory in di.GetDirectories())
38+
{
39+
directory.Delete(true);
40+
}
41+
}
42+
43+
return Task.CompletedTask;
44+
}
45+
2746
public abstract Task SaveContentAsync(string id, T content);
2847
}
2948
}

src/Test/SolidifyProject.Engine.Test.Integration/Infrastructure/Services/OrchestrationServiceTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public class OrchestrationServiceTest
4242

4343
private IHtmlMinificationService HtmlMinificationService => new GoogleHtmlMinificationService();
4444

45+
[TearDown]
46+
public void TearDown()
47+
{
48+
_assetsWriterStorage.Clear();
49+
_pageModelWriterStorage.Clear();
50+
}
51+
4552
[Test]
4653
public async Task ProduceHtml()
4754
{
@@ -76,5 +83,60 @@ public async Task ProduceHtml()
7683

7784
Assert.AreEqual("<html><head><link href=\"/assets/style.css\" rel=\"stylesheet\" type=\"text/css\"><title>Static Site Generator - Home</title><link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/Assets/favicon.ico\"></head><body><div><ul><li><a href=\"https://facebook.com\">Facebook</a></li><li><a href=\"https://twitter.com\">Twitter</a></li></ul></div><h1>Welcome to Static Site Generator!</h1><h4>Authors</h4><h5>Founders</h5><ul><li>Anton Boyko (Microsoft Azure MVP)</li></ul><h5>Contributors</h5><p><strong>Powered by .NET Core 2.0</strong></p><div><i>All rights reserved</i></div></body></html>", page.ContentRaw);
7885
}
86+
87+
[Test]
88+
public async Task CleanUpBeforeRender()
89+
{
90+
Assert.AreEqual(0, _pageModelWriterStorage.Count);
91+
Assert.AreEqual(0, _assetsWriterStorage.Count);
92+
93+
_assetsWriterStorage.Add(new BinaryContentModel
94+
{
95+
Id = "fakedata"
96+
});
97+
98+
_assetsWriterStorage.Add(new BinaryContentModel
99+
{
100+
Id = "fakedata123"
101+
});
102+
103+
_pageModelWriterStorage.Add(new TextContentModel
104+
{
105+
Id = "fakedata"
106+
});
107+
108+
_pageModelWriterStorage.Add(new TextContentModel
109+
{
110+
Id = "fakedata123"
111+
});
112+
113+
var orchestrationService = new OrchestrationService();
114+
orchestrationService.LoggerService = LoggerService;
115+
orchestrationService.AssetsReaderService = AssetsReaderService;
116+
orchestrationService.AssetsWriterService = AssetsWriterService;
117+
orchestrationService.PageModelReaderService = PageModelReaderService;
118+
orchestrationService.PageModelWriterService = PageModelWriterService;
119+
orchestrationService.TemplateReaderService = TemplateReaderService;
120+
orchestrationService.TemplateService = TemplateService;
121+
orchestrationService.DataService = DataService;
122+
orchestrationService.MarkupService = MarkupService;
123+
orchestrationService.HtmlMinificationService = HtmlMinificationService;
124+
125+
await orchestrationService.RenderProjectAsync();
126+
127+
128+
Assert.AreEqual(2, _pageModelWriterStorage.Count);
129+
Assert.IsFalse(_pageModelWriterStorage.All(x => x.ContentRaw == null));
130+
131+
Assert.AreEqual(2, _assetsWriterStorage.Count);
132+
Assert.IsFalse(_assetsWriterStorage.All(x => x.ContentRaw == null));
133+
134+
var logoAsset = _assetsWriterStorage.SingleOrDefault(x => x.Id.Contains("img") && x.Id.Contains("logo.png"));
135+
Assert.IsNotNull(logoAsset);
136+
137+
var page = _pageModelWriterStorage.Single(x => x.Id.Equals("index.html"));
138+
139+
Assert.AreEqual("<html><head><link href=\"/assets/style.css\" rel=\"stylesheet\" type=\"text/css\"><title>Static Site Generator - Home</title></head><body><div><ul><li><a href=\"https://facebook.com\">Facebook</a></li><li><a href=\"https://twitter.com\">Twitter</a></li></ul></div><h1>Welcome to Static Site Generator!</h1><h4>Authors</h4><h5>Founders</h5><ul><li>Anton Boyko (Microsoft Azure MVP)</li></ul><h5>Contributors</h5><p><strong>Powered by .NET Core 2.0</strong></p><div><i>All rights reserved</i></div></body></html>", page.ContentRaw);
140+
}
79141
}
80142
}

src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Services/ContentWriterService/ContentWriteServiceFakeTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@ public override Task SaveContentAsyncTest(CustomDataModel newElement)
3232
{
3333
return base.SaveContentAsyncTest(newElement);
3434
}
35+
36+
[TestCaseSource(nameof(_saveContentAsyncTestCases))]
37+
public override Task CleanOutputAsyncTest(CustomDataModel newElement)
38+
{
39+
return base.CleanOutputAsyncTest(newElement);
40+
}
3541
}
3642
}

src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Services/ContentWriterService/FileSystemContentWriterServiceTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,11 @@ public override Task SaveContentAsyncTest(CustomDataModel newElement)
5252
{
5353
return base.SaveContentAsyncTest(newElement);
5454
}
55+
56+
[TestCaseSource(nameof(_saveContentAsyncTestCases))]
57+
public override Task CleanOutputAsyncTest(CustomDataModel newElement)
58+
{
59+
return base.CleanOutputAsyncTest(newElement);
60+
}
5561
}
5662
}

src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Services/ContentWriterService/_ContentWriterServiceTestBase.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,17 @@ public virtual async Task SaveContentAsyncTest(T newElement)
2626

2727
Assert.IsTrue(AreEqual(newElement, newElementActual), "Objects are not equal");
2828
}
29+
30+
[Test]
31+
public virtual async Task CleanOutputAsyncTest(T newElement)
32+
{
33+
await ContentWriterService.SaveContentAsync(newElement.Id, newElement);
34+
35+
await ContentWriterService.CleanFolderAsync("");
36+
37+
var newElementActual = await ContentReaderService.LoadContentByIdAsync(newElement.Id);
38+
39+
Assert.IsNull(newElementActual);
40+
}
2941
}
3042
}

src/Test/SolidifyProject.Engine.Test.Unit/_Fake/Infrastructure/Services/ContentWriterServiceFake.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
6+
using Markdig.Extensions.TaskLists;
57
using SolidifyProject.Engine.Infrastructure.Interfaces;
68
using SolidifyProject.Engine.Infrastructure.Models.Base;
79

@@ -33,5 +35,23 @@ public Task SaveContentAsync(string id, T content)
3335

3436
return Task.FromResult<object>(null);
3537
}
38+
39+
public Task CleanFolderAsync(string path)
40+
{
41+
if (!string.IsNullOrEmpty(path))
42+
{
43+
var elements = _storage
44+
.Where(x => x.Id.StartsWith($"{path}{Path.DirectorySeparatorChar}"));
45+
46+
foreach (var element in elements)
47+
{
48+
_storage.Remove(element);
49+
}
50+
}
51+
52+
_storage.Clear();
53+
54+
return Task.CompletedTask;
55+
}
3656
}
3757
}

0 commit comments

Comments
 (0)