-
Notifications
You must be signed in to change notification settings - Fork 97
/
DefaultControlBuilderFactory.cs
271 lines (246 loc) · 12.2 KB
/
DefaultControlBuilderFactory.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using DotVVM.Framework.Compilation.ViewCompiler;
using DotVVM.Framework.Configuration;
using DotVVM.Framework.Hosting;
using DotVVM.Framework.Utils;
using Microsoft.Extensions.DependencyInjection;
namespace DotVVM.Framework.Compilation
{
/// <summary>
/// Provides control builder objects for markup files.
/// </summary>
public class DefaultControlBuilderFactory : IControlBuilderFactory
{
private readonly DotvvmConfiguration configuration;
private readonly bool allowReload;
private readonly IMarkupFileLoader markupFileLoader;
private readonly CompiledAssemblyCache compiledAssemblyCache;
public Func<IViewCompiler> ViewCompilerFactory { get; private set; }
private ConcurrentDictionary<string, Lazy<(ControlBuilderDescriptor, Lazy<IControlBuilder>)>> controlBuilders = new();
public DefaultControlBuilderFactory(DotvvmConfiguration configuration, IMarkupFileLoader markupFileLoader, CompiledAssemblyCache compiledAssemblyCache)
{
this.configuration = configuration;
this.allowReload = configuration.Runtime.ReloadMarkupFiles.Enabled ?? configuration.Debug;
// WORKAROUND: there is a circular dependency
// TODO: get rid of that
this.ViewCompilerFactory = () => configuration.ServiceProvider.GetRequiredService<IViewCompiler>();
this.markupFileLoader = markupFileLoader;
this.compiledAssemblyCache = compiledAssemblyCache;
if (configuration.CompiledViewsAssemblies != null)
foreach (var assembly in configuration.CompiledViewsAssemblies)
{
LoadCompiledViewsAssembly(assembly);
}
}
/// <summary>
/// Gets the control builder.
/// </summary>
public (ControlBuilderDescriptor descriptor, Lazy<IControlBuilder> builder) GetControlBuilder(string virtualPath)
{
var (markupFile, markupChanged) = GetMarkupFile(virtualPath);
if (!markupChanged)
{
if (controlBuilders.TryGetValue(virtualPath, out var builder))
{
return builder.Value;
}
// because of race conditions, the builder doesn't need to be created
// we let it fallback onto creating the builder again
}
var lazy = new Lazy<(ControlBuilderDescriptor, Lazy<IControlBuilder>)>(() =>
CreateControlBuilder(markupFile));
if (allowReload)
{
controlBuilders[virtualPath] = lazy;
return lazy.Value;
}
else
{
return controlBuilders.GetOrAdd(virtualPath, lazy).Value;
}
}
readonly ConcurrentDictionary<string, MarkupFile> markupFiles = new();
private (MarkupFile file, bool changed) GetMarkupFile(string virtualPath)
{
if (markupFiles.TryGetValue(virtualPath, out var cachedFile))
{
if (!allowReload)
return (cachedFile, false);
var newFile = markupFileLoader.GetMarkup(configuration, virtualPath);
if (newFile is null || cachedFile.Equals(newFile))
return (cachedFile, false);
else
{
markupFiles[virtualPath] = newFile;
return (newFile, true);
}
}
var markupFile = markupFileLoader.GetMarkup(configuration, virtualPath) ?? throw new DotvvmCompilationException($"File '{virtualPath}' was not found. This exception is possibly caused because of incorrect route registration.");
markupFiles.TryAdd(virtualPath, markupFile);
return (markupFile, true);
}
/// <summary>
/// Creates the control builder.
/// </summary>
private (ControlBuilderDescriptor, Lazy<IControlBuilder>) CreateControlBuilder(MarkupFile file)
{
var compilationService = configuration.ServiceProvider.GetService<IDotvvmViewCompilationService>();
void editCompilationException(DotvvmCompilationException ex)
{
if (ex.FileName is null || ex.FileName == file.FullPath || ex.FileName == file.FileName)
{
ex.SetFile(file.FullPath, file);
}
else if (ex.MarkupFile is null)
{
// try to load the markup file of this error
try
{
var exceptionFile = GetMarkupFile(ex.FileName);
ex.SetFile(exceptionFile.file.FullPath, exceptionFile.file);
}
catch { }
}
}
try
{
var sw = ValueStopwatch.StartNew();
var (descriptor, factory) = ViewCompilerFactory().CompileView(file.ReadContent(), file.FileName);
var phase1Ticks = sw.ElapsedTicks;
var lazyBuilder = new Lazy<IControlBuilder>(() => {
try
{
sw.Restart();
var result = factory();
// register the internal resource after the page is successfully compiled,
// otherwise we could be hiding compile error behind more cryptic resource registration errors
if (descriptor.ViewModuleReference != null)
{
var (import, init) = descriptor.ViewModuleReference.BuildResources(configuration.Resources);
configuration.Resources.RegisterViewModuleResources(import, init);
}
Interlocked.Increment(ref DotvvmMetrics.BareCounters.ViewsCompiledOk);
compilationService.RegisterCompiledView(file.FileName, descriptor, null);
return result;
}
catch (DotvvmCompilationException ex)
{
Interlocked.Increment(ref DotvvmMetrics.BareCounters.ViewsCompiledFailed);
editCompilationException(ex);
compilationService.RegisterCompiledView(file.FileName, descriptor, ex);
throw;
}
catch (Exception ex)
{
compilationService.RegisterCompiledView(file.FileName, descriptor, ex);
throw;
}
finally
{
Interlocked.Add(ref DotvvmMetrics.BareCounters.ViewsCompilationTime, phase1Ticks + sw.ElapsedTicks);
}
});
// initialize the Lazy asynchronously to speed up initialization and get reasonably accurate ViewsCompilationTime metric
Task.Run(() => {
try {
_ = lazyBuilder.Value;
} catch { }
});
return (descriptor, lazyBuilder);
}
catch (DotvvmCompilationException ex)
{
editCompilationException(ex);
compilationService.RegisterCompiledView(file.FileName, null, ex);
throw;
}
catch (Exception ex)
{
compilationService.RegisterCompiledView(file.FileName, null, ex);
throw;
}
}
/// <summary> Removes an old entry from the cache, forcing a recompilation. If loading of this view is already running </summary>
public void InvalidateCache(string virtualPath)
{
this.markupFiles.TryRemove(virtualPath, out _);
this.controlBuilders.TryRemove(virtualPath, out _);
}
public void LoadCompiledViewsAssembly(string filePath)
{
var assembly = TryFindAssembly(filePath);
if (assembly != null)
{
LoadCompiledViewsAssembly(assembly);
var bindings = Path.Combine(Path.GetDirectoryName(assembly.GetCodeBasePath())!, "CompiledViewsBindings.dll");
if (File.Exists(bindings)) AssemblyLoader.LoadFile(bindings);
}
}
public Assembly? TryFindAssembly(string fileName)
{
if (File.Exists(fileName)) return AssemblyLoader.LoadFile(fileName);
if (Path.IsPathRooted(fileName)) return null;
var cleanName = Path.GetFileNameWithoutExtension(Path.GetFileName(fileName));
var assemblies = compiledAssemblyCache.GetAllAssemblies();
foreach (var assembly in assemblies)
{
// get already loaded assembly
if (assembly.GetName().Name == cleanName)
{
var codeBase = assembly.GetCodeBasePath();
if (codeBase!.EndsWith(fileName, StringComparison.OrdinalIgnoreCase)) return assembly;
}
}
foreach (var assemblyDirectory in new[] { Path.GetDirectoryName(typeof(DefaultControlBuilderFactory).Assembly.GetCodeBasePath()), configuration.ApplicationPhysicalPath })
{
if (!string.IsNullOrEmpty(assemblyDirectory))
{
var possibleFileName = Path.Combine(assemblyDirectory, fileName);
if (File.Exists(possibleFileName)) return AssemblyLoader.LoadFile(possibleFileName);
}
}
return null;
}
public void LoadCompiledViewsAssembly(Assembly assembly)
{
var initMethods = assembly.GetTypes()
.Where(t => t.Name == "SerializedObjects")
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Static))
.Where(m => m.Name == "Init")
.ToArray();
foreach (var initMethod in initMethods)
{
var args = initMethod.GetParameters().Select(p => configuration.ServiceProvider.GetRequiredService(p.ParameterType)).ToArray();
initMethod.Invoke(null, args);
}
var builders = assembly.GetTypes().Select(t => new {
type = t,
attribute = t.GetCustomAttribute<LoadControlBuilderAttribute>()
}).Where(t => t.attribute != null);
foreach (var builder in builders)
{
RegisterControlBuilder(builder.attribute!.FilePath, (IControlBuilder)Activator.CreateInstance(builder.type).NotNull());
}
}
public void RegisterControlBuilder(string file, IControlBuilder builder)
{
controlBuilders.TryAdd(file, new Lazy<(ControlBuilderDescriptor, Lazy<IControlBuilder>)>(() => (builder.Descriptor, new Lazy<IControlBuilder>(() => builder))));
}
private static readonly HashSet<string> csharpKeywords = new HashSet<string>(new[]
{
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else",
"enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock",
"long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort",
"using", "virtual", "void", "volatile", "while", "add", "alias", "ascending", "async", "await", "descending", "dynamic", "from", "get", "global", "group", "into",
"join", "let", "orderby", "partial", "remove", "select", "set", "value", "var", "where", "where", "yield"
});
}
}