-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExample.cs
More file actions
64 lines (53 loc) · 1.87 KB
/
Copy pathExample.cs
File metadata and controls
64 lines (53 loc) · 1.87 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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using SER.Code.ContextSystem.Interfaces;
using SER.Code.Helpers.ResultSystem;
using SER.Code.ScriptSystem;
namespace SER.Code.Examples;
public abstract class Example
{
public abstract string Name { get; }
public abstract string Content { get; }
private static Dictionary<string, string>? _cachedExamples;
[UsedImplicitly]
public static Dictionary<string, string> GetAllExamples()
{
if (_cachedExamples != null) return _cachedExamples;
var assembly = Assembly.GetExecutingAssembly();
var resourceNames = assembly.GetManifestResourceNames()
.Where(n => n.EndsWith(".ser"));
var examples = new Dictionary<string, string>();
foreach (var name in resourceNames)
{
using var stream = assembly.GetManifestResourceStream(name);
if (stream == null) continue;
using var reader = new StreamReader(stream);
string content = reader.ReadToEnd();
string[] parts = name.Split('.');
if (parts.Length < 2) continue;
string fileName = parts[parts.Length - 2];
examples[fileName] = content;
}
return _cachedExamples = examples;
}
public static string? GetExample(string name)
{
return GetAllExamples().TryGetValue(name, out var content) ? content : null;
}
[UsedImplicitly]
public static string Verify()
{
var examples = GetAllExamples();
foreach (var example in examples)
{
if (Script.CreateAnonymous(example.Key, example.Value).Compile().HasErrored(out var error))
{
return new Result(false, $"in example '{example.Key}'") + error;
}
}
return string.Empty;
}
}