-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileSystemSourceHandler.cs
153 lines (118 loc) · 4.79 KB
/
FileSystemSourceHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using Bloggen.Net.Config;
using Microsoft.Extensions.Options;
namespace Bloggen.Net.Source
{
public class FileSystemSourceHandler : ISourceHandler
{
public string AssetsPath => this.assetsPath;
public string TemplateAssetsPath => this.templateAssetsPath;
private const string TEMPLATES_DIRECTORY = "templates";
private const string TEMPLATE_NAME = "index.hbs";
private const string LAYOUTS_DIRECTORY = "layouts";
private static readonly string[] LAYOUTS = {"page", "post", "tag", "list"};
private const string PARTIALS_DIRECTORY = "partials";
private const string POSTS_DIRECTORY = "posts";
private const string PAGES_DIRECTORY = "pages";
private readonly IFileSystem fileSystem;
private readonly string templatePath;
private readonly string postsPath;
private readonly string pagesPath;
private readonly string assetsPath;
private readonly string templateAssetsPath;
public FileSystemSourceHandler(
IFileSystem fileSystem,
CommandLineOptions commandLineOptions,
IOptions<SiteConfig> siteConfig)
{
this.fileSystem = fileSystem;
this.templatePath = this.fileSystem.Path.Combine(
commandLineOptions.SourceDirectory,
TEMPLATES_DIRECTORY,
siteConfig.Value.Template
);
this.postsPath = this.fileSystem.Path.Combine(
commandLineOptions.SourceDirectory,
POSTS_DIRECTORY
);
this.pagesPath = this.fileSystem.Path.Combine(
commandLineOptions.SourceDirectory,
PAGES_DIRECTORY
);
this.assetsPath = this.fileSystem.Path.Combine(
commandLineOptions.SourceDirectory,
"assets"
);
this.templateAssetsPath = this.fileSystem.Path.Combine(
this.templatePath,
"assets"
);
}
public Stream GetTemplate()
{
return this.fileSystem.FileStream.Create(
this.fileSystem.Path.Combine(this.templatePath, TEMPLATE_NAME),
FileMode.Open);
}
public IEnumerable<(string partialName, Stream stream)> GetLayouts()
{
var layoutsPath = this.fileSystem.Path.Combine(this.templatePath, LAYOUTS_DIRECTORY);
var files = LAYOUTS.Select(l => this.fileSystem.Path.Combine(layoutsPath, $"{l}.hbs"));
foreach (var f in files)
{
if (!this.fileSystem.File.Exists(f))
{
throw new FileNotFoundException("Layout file not found", fileName: f);
}
}
return LAYOUTS.Select(l =>
(l, this.fileSystem.FileStream.Create(
this.fileSystem.Path.Combine(layoutsPath, $"{l}.hbs"), FileMode.Open)));
}
public IEnumerable<(string partialName, Stream stream)> GetPartials()
{
var path = this.fileSystem.Path.Combine(this.templatePath, PARTIALS_DIRECTORY);
if (this.fileSystem.Directory.Exists(path))
{
return this.EnumerateFiles(
this.fileSystem.Path.Combine(this.templatePath, PARTIALS_DIRECTORY));
}
else
{
return Enumerable.Empty<(string partialName, Stream stream)>();
}
}
public IEnumerable<(string fileName, Stream stream)> GetPosts()
{
return this.EnumerateFiles(this.postsPath);
}
public IEnumerable<(string fileName, Stream stream)> GetPages()
{
return this.EnumerateFiles(this.pagesPath);
}
public string GetPost(string fileName)
{
return this.GetFile(fileName, this.postsPath);
}
public string GetPage(string fileName)
{
return this.GetFile(fileName, this.pagesPath);
}
private IEnumerable<(string name, Stream stream)> EnumerateFiles(string path)
{
return this.fileSystem.Directory.EnumerateFiles(path)
.Select(p =>
(this.fileSystem.Path.GetFileNameWithoutExtension(p),
this.fileSystem.FileStream.Create(p, FileMode.Open)));
}
private string GetFile(string fileName, string directory)
{
var filePath = this.fileSystem.Directory.GetFiles(directory).First(f =>
this.fileSystem.Path.GetFileNameWithoutExtension(f) == fileName);
return this.fileSystem.File.ReadAllText(filePath);
}
}
}