-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathCodeSnippetService.cs
133 lines (123 loc) · 4.81 KB
/
CodeSnippetService.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
using Microsoft.Extensions.Options;
using System.Globalization;
namespace BootstrapBlazor.Server.Services;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="factory"></param>
/// <param name="cacheManager"></param>
/// <param name="options"></param>
/// <param name="configuration"></param>
/// <param name="localizerOptions"></param>
class CodeSnippetService(
IHttpClientFactory factory,
ICacheManager cacheManager,
IOptions<WebsiteOptions> options,
IConfiguration configuration,
IOptions<JsonLocalizationOptions> localizerOptions)
{
/// <summary>
/// 获得示例源码方法
/// </summary>
/// <returns></returns>
public async Task<string> GetCodeAsync(string codeFile)
{
string? content;
try
{
// codeFile = ajax.razor.cs
var segs = codeFile.Split('.');
var key = segs[0];
var typeName = options.Value.SourceCodes.TryGetValue(key.ToLowerInvariant(), out var value) ? value : string.Empty;
if (!string.IsNullOrEmpty(typeName))
{
var fileName = codeFile.Replace(key, typeName);
content = await GetFileContentAsync(fileName);
// 源码修正
var type = typeName.Replace('\\', '.');
Utility.GetJsonStringByTypeName(localizerOptions.Value, typeof(CodeSnippetService).Assembly, $"BootstrapBlazor.Server.Components.Samples.{type}").ToList()
.ForEach(l => content = ReplacePayload(content, l));
content = ReplaceSymbols(content);
content = RemoveBlockStatement(content, "@inject IStringLocalizer<");
}
else
{
content = "Error: Please config docs.json";
}
}
catch (Exception ex) { content = $"Error: {ex.Message}"; }
return content;
}
/// <summary>
/// 获得指定文件源码
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public async Task<string> GetFileContentAsync(string fileName)
{
string? payload;
if (OperatingSystem.IsBrowser())
{
var client = factory.CreateClient();
client.Timeout = TimeSpan.FromSeconds(5);
client.BaseAddress = new Uri($"{options.Value.ServerUrl}/api/");
payload = await client.GetStringAsync($"Code?fileName=BootstrapBlazor.Server/Components/Samples/{fileName}");
}
else
{
// 读取硬盘文件
var key = $"{nameof(GetFileContentAsync)}-{fileName}-{CultureInfo.CurrentUICulture.Name}";
payload = await cacheManager.GetOrCreateAsync(key, entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(10);
return ReadFileAsync(fileName);
});
}
return payload;
}
private async Task<string> ReadFileAsync(string fileName)
{
string? payload;
var file = options.Value.IsDevelopment
? $"{options.Value.ContentRootPath}\\..\\BootstrapBlazor.Server\\Components\\Samples\\{fileName}"
: $"{GetSourceCodePath()}BootstrapBlazor.Server\\Components\\Samples\\{fileName}";
if (!OperatingSystem.IsWindows())
{
file = file.Replace('\\', '/');
}
if (File.Exists(file))
{
payload = await File.ReadAllTextAsync(file);
}
else
{
payload = "Error: File not found";
}
return payload;
}
private string GetSourceCodePath() => $"{configuration.GetValue<string>("SourceCodePath") ?? options.Value.SourceCodePath}";
private static string ReplaceSymbols(string payload) => payload
.Replace("@@", "@")
.Replace("<", "<")
.Replace(">", ">");
private static string ReplacePayload(string payload, LocalizedString l) => payload
.Replace($"@((MarkupString)Localizer[\"{l.Name}\"].Value)", l.Value)
.Replace($"@Localizer[\"{l.Name}\"]", l.Value)
.Replace($"Localizer[\"{l.Name}\"]", $"\"{l.Value}\"");
private static string RemoveBlockStatement(string payload, string removeString)
{
var index = payload.IndexOf(removeString);
if (index > -1)
{
var end = payload.IndexOf("\n", index, StringComparison.OrdinalIgnoreCase);
var target = payload[index..(end + 1)];
payload = payload.Replace(target, string.Empty);
payload = payload.TrimStart('\r', '\n');
}
return payload;
}
}