|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
| 2 | +using System.Collections.Immutable; |
3 | 3 | using System.IO; |
4 | 4 | using System.Linq; |
5 | 5 | using System.Text; |
@@ -30,47 +30,75 @@ public void Initialize(IncrementalGeneratorInitializationContext context) |
30 | 30 | x.Right.GetOptions(x.Left).TryGetValue("build_metadata.EmbeddedResource.Value", out var resourceName); |
31 | 31 | x.Right.GetOptions(x.Left).TryGetValue("build_metadata.EmbeddedResource.Kind", out var kind); |
32 | 32 | x.Right.GetOptions(x.Left).TryGetValue("build_metadata.EmbeddedResource.Comment", out var comment); |
33 | | - return (resourceName!, kind, comment: string.IsNullOrWhiteSpace(comment) ? null : comment); |
| 33 | + return (resourceName: resourceName!, kind, comment: string.IsNullOrWhiteSpace(comment) ? null : comment); |
34 | 34 | }) |
| 35 | + .Collect() |
35 | 36 | .Combine(context.AnalyzerConfigOptionsProvider |
36 | | - .Select((p, _) => |
| 37 | + .SelectMany((p, _) => |
37 | 38 | { |
38 | 39 | if (!p.GlobalOptions.TryGetValue("build_property.EmbeddedResourceStringExtensions", out var extensions) || |
39 | 40 | extensions == null) |
40 | | - return new HashSet<string>(); |
| 41 | + return Array.Empty<string>(); |
41 | 42 |
|
42 | | - return new HashSet<string>(extensions.Split('|'), StringComparer.OrdinalIgnoreCase); |
43 | | - })); |
| 43 | + return extensions.Split('|'); |
| 44 | + }) |
| 45 | + .WithComparer(StringComparer.OrdinalIgnoreCase) |
| 46 | + .Collect()); |
44 | 47 |
|
45 | 48 | context.RegisterSourceOutput( |
46 | 49 | files, |
47 | 50 | GenerateSource); |
48 | 51 | } |
49 | 52 |
|
50 | | - static void GenerateSource(SourceProductionContext spc, ((string resourceName, string? kind, string? comment), HashSet<string> extensions) arg2) |
| 53 | + static void GenerateSource( |
| 54 | + SourceProductionContext spc, |
| 55 | + ( |
| 56 | + ImmutableArray<(string resourceName, string? kind, string? comment)> files, |
| 57 | + ImmutableArray<string> extensions) arg2) |
51 | 58 | { |
52 | | - var ((resourceName, kind, comment), extensions) = arg2; |
| 59 | + var (files, extensions) = arg2; |
53 | 60 |
|
54 | 61 | var file = "CSharp.sbntxt"; |
55 | 62 | var template = Template.Parse(EmbeddedResource.GetContent(file), file); |
56 | 63 |
|
57 | | - var isText = kind?.Equals("text", StringComparison.OrdinalIgnoreCase) == true || |
58 | | - extensions.Contains(Path.GetExtension(resourceName)); |
59 | | - var root = Area.Load(new Resource(resourceName, comment, isText)); |
60 | | - var model = new Model(root); |
| 64 | + var groupsWithoutExtensions = files |
| 65 | + .GroupBy(f => Path.Combine( |
| 66 | + Path.GetDirectoryName(f.resourceName), |
| 67 | + Path.GetFileNameWithoutExtension(f.resourceName))); |
| 68 | + foreach (var group in groupsWithoutExtensions) |
| 69 | + { |
| 70 | + var basePath = group.Key; |
| 71 | + var resources = group |
| 72 | + .Select(f => |
| 73 | + { |
| 74 | + var isText = f.kind?.Equals("text", StringComparison.OrdinalIgnoreCase) == true || |
| 75 | + extensions.Contains(Path.GetExtension(f.resourceName)); |
| 76 | + var name = group.Count() == 1 |
| 77 | + ? Path.GetFileNameWithoutExtension(f.resourceName) |
| 78 | + : Path.GetExtension(f.resourceName)[1..]; |
| 79 | + return new Resource(name, f.comment, isText) |
| 80 | + { |
| 81 | + Path = f.resourceName, |
| 82 | + }; |
| 83 | + }) |
| 84 | + .ToList(); |
| 85 | + |
| 86 | + var root = Area.Load(basePath, resources); |
| 87 | + var model = new Model(root); |
61 | 88 |
|
62 | | - var output = template.Render(model, member => member.Name); |
| 89 | + var output = template.Render(model, member => member.Name); |
63 | 90 |
|
64 | | - // Apply formatting since indenting isn't that nice in Scriban when rendering nested |
65 | | - // structures via functions. |
66 | | - output = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseCompilationUnit(output) |
67 | | - .NormalizeWhitespace() |
68 | | - .GetText() |
69 | | - .ToString(); |
| 91 | + // Apply formatting since indenting isn't that nice in Scriban when rendering nested |
| 92 | + // structures via functions. |
| 93 | + output = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseCompilationUnit(output) |
| 94 | + .NormalizeWhitespace() |
| 95 | + .GetText() |
| 96 | + .ToString(); |
70 | 97 |
|
71 | | - spc.AddSource( |
72 | | - $"{resourceName.Replace('\\', '.').Replace('/', '.')}.g.cs", |
73 | | - SourceText.From(output, Encoding.UTF8)); |
| 98 | + spc.AddSource( |
| 99 | + $"{basePath.Replace('\\', '.').Replace('/', '.')}.g.cs", |
| 100 | + SourceText.From(output, Encoding.UTF8)); |
| 101 | + } |
74 | 102 | } |
75 | 103 | } |
76 | 104 | } |
0 commit comments