-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathLibManResourceVersionGenerator.cs
More file actions
56 lines (49 loc) · 2.06 KB
/
Copy pathLibManResourceVersionGenerator.cs
File metadata and controls
56 lines (49 loc) · 2.06 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
using Lombiq.HelpfulLibraries.Attributes;
using Microsoft.CodeAnalysis;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace Lombiq.HelpfulLibraries.SourceGenerators;
[Generator]
public class LibManResourceVersionGenerator : GeneratorFromFileBase
{
protected override Type Attribute => typeof(LibManVersionsAttribute);
protected override string? GenerateCode(GeneratorFromFileModel model)
{
var fileContent = model
.AttributeData
.Select(dictionary => dictionary.TryGetValue("fileName", out var fileNameValue)
? fileNameValue.Trim('"')
: "libman.json")
.Select(model.GetFileContent)
.FirstOrDefault(text => !string.IsNullOrWhiteSpace(text));
if (fileContent == null) return null;
var jsonDocument = JsonDocument.Parse(fileContent);
return CreateSubclassConstants(
model.NamespaceName,
model.ClassName,
"LibManVersions",
jsonDocument
.RootElement
.GetProperty("libraries")
.EnumerateArray()
.Select(arrayItem => ParseLibraryExpression(arrayItem.GetProperty("library").GetString()))
.Where(pair => !string.IsNullOrWhiteSpace(pair.Value)));
}
private static (string Name, string Value) ParseLibraryExpression(string? library)
{
var index = library?.IndexOf('@') ?? -1;
return index <= 0
? default
: (Name: SanitizeToApproximatePascalCase(library!.Substring(0, index)), Value: library.Substring(index + 1));
}
[SuppressMessage("Security", "MA0009:Add regex evaluation timeout", Justification = "Not applicable here.")]
private static string SanitizeToApproximatePascalCase(string text)
{
var words = Regex.Replace(text, "[^a-zA-Z0-9]+", " ").Trim();
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(words).Replace(" ", string.Empty);
}
}