forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpPage.cs
More file actions
182 lines (153 loc) · 7.08 KB
/
SharpPage.cs
File metadata and controls
182 lines (153 loc) · 7.08 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ServiceStack.IO;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public class SharpPage
{
public IVirtualFile File { get; }
public ReadOnlyMemory<char> FileContents { get; private set; }
public ReadOnlyMemory<char> BodyContents { get; private set; }
public Dictionary<string, object> Args { get; protected set; }
public SharpPage LayoutPage { get; set; }
public List<PageFragment> PageFragments { get; set; }
public DateTime LastModified { get; set; }
public DateTime LastModifiedCheck { get; private set; }
public bool HasInit { get; private set; }
public bool IsLayout { get; private set; }
public ScriptContext Context { get; }
public PageFormat Format { get; }
private readonly object semaphore = new object();
public bool IsTempFile => File.Directory.VirtualPath == ScriptConstants.TempFilePath;
public string VirtualPath => IsTempFile ? "{temp file}" : File.VirtualPath;
public SharpPage(ScriptContext context, IVirtualFile file, PageFormat format=null)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
File = file ?? throw new ArgumentNullException(nameof(file));
Format = format ?? Context.GetFormat(File.Extension);
if (Format == null)
throw new ArgumentException($"File with extension '{File.Extension}' is not a registered PageFormat in Context.PageFormats", nameof(file));
}
public virtual async Task<SharpPage> Init()
{
if (HasInit)
{
var skipCheck = !Context.DebugMode &&
(Context.CheckForModifiedPagesAfter != null
? DateTime.UtcNow - LastModifiedCheck < Context.CheckForModifiedPagesAfter.Value
: !Context.CheckForModifiedPages) &&
(Context.InvalidateCachesBefore == null || LastModifiedCheck > Context.InvalidateCachesBefore.Value);
if (skipCheck)
return this;
File.Refresh();
LastModifiedCheck = DateTime.UtcNow;
if (File.LastModified == LastModified)
return this;
}
return await Load();
}
public async Task<SharpPage> Load()
{
string contents;
using (var stream = File.OpenRead())
{
contents = await stream.ReadToEndAsync();
}
var lastModified = File.LastModified;
var fileContents = contents.AsMemory();
var pageVars = new Dictionary<string, object>();
var pos = 0;
var bodyContents = fileContents;
fileContents.AdvancePastWhitespace().TryReadLine(out ReadOnlyMemory<char> line, ref pos);
if (line.StartsWith(Format.ArgsPrefix))
{
while (fileContents.TryReadLine(out line, ref pos))
{
if (line.Trim().Length == 0)
continue;
if (line.StartsWith(Format.ArgsSuffix))
break;
var colonPos = line.IndexOf(':');
var spacePos = line.IndexOf(' ');
var bracePos = line.IndexOf('{');
var sep = colonPos >= 0 ? ':' : ' ';
if (bracePos > 0 && spacePos > 0 && colonPos > spacePos)
sep = ' ';
line.SplitOnFirst(sep, out var first, out var last);
pageVars[first.Trim().ToString()] = !last.IsEmpty ? last.Trim().ToString() : "";
}
//When page has variables body starts from first non whitespace after variables end
var argsSuffixPos = line.LastIndexOf(Format.ArgsSuffix);
if (argsSuffixPos >= 0)
{
//Start back from the end of the ArgsSuffix
pos -= line.Length - argsSuffixPos - Format.ArgsSuffix.Length;
}
bodyContents = fileContents.SafeSlice(pos).AdvancePastWhitespace();
}
var pageFragments = pageVars.TryGetValue("ignore", out object ignore)
&& ("page".Equals(ignore.ToString()) || "template".Equals(ignore.ToString()))
? new List<PageFragment> { new PageStringFragment(bodyContents) }
: SharpPageUtils.ParseTemplatePage(bodyContents);
foreach (var fragment in pageFragments)
{
if (fragment is PageVariableFragment var && var.Binding == ScriptConstants.Page)
{
IsLayout = true;
break;
}
}
lock (semaphore)
{
LastModified = lastModified;
LastModifiedCheck = DateTime.UtcNow;
FileContents = fileContents;
Args = pageVars;
BodyContents = bodyContents;
PageFragments = pageFragments;
HasInit = true;
LayoutPage = Format.ResolveLayout(this);
}
if (LayoutPage != null)
{
if (!LayoutPage.HasInit)
{
await LayoutPage.Load();
}
else
{
if (Context.DebugMode || Context.CheckForModifiedPagesAfter != null &&
DateTime.UtcNow - LayoutPage.LastModifiedCheck >= Context.CheckForModifiedPagesAfter.Value)
{
LayoutPage.File.Refresh();
LayoutPage.LastModifiedCheck = DateTime.UtcNow;
if (LayoutPage.File.LastModified != LayoutPage.LastModified)
await LayoutPage.Load();
}
}
}
return this;
}
}
public class SharpPartialPage : SharpPage
{
private static readonly MemoryVirtualFiles TempFiles = new MemoryVirtualFiles();
private static readonly InMemoryVirtualDirectory TempDir = new InMemoryVirtualDirectory(TempFiles, ScriptConstants.TempFilePath);
static IVirtualFile CreateFile(string name, string format) =>
new InMemoryVirtualFile(TempFiles, TempDir)
{
FilePath = name + "." + format,
TextContents = "",
};
public SharpPartialPage(ScriptContext context, string name, IEnumerable<PageFragment> body, string format, Dictionary<string,object> args=null)
: base(context, CreateFile(name, format), context.GetFormat(format))
{
PageFragments = body.ToList();
Args = args ?? new Dictionary<string, object>();
}
public override Task<SharpPage> Init() => ((SharpPage)this).InTask();
}
}