Skip to content

Commit 0725b3d

Browse files
added logical name
1 parent f7b6093 commit 0725b3d

20 files changed

+351
-304
lines changed

.idea/.idea.MSBuild.Targets/.idea/workspace.xml

Lines changed: 97 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Directory.Packages.props

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
1+
<?xml version="1.0" encoding="utf-8" ?>
32
<Project>
43
<PropertyGroup>
54
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
@@ -84,17 +83,18 @@
8483
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
8584
<PackageVersion Include="Microsoft.Playwright" Version="1.50.0" />
8685
<PackageVersion Include="Rocket.Surgery.Extensions.Testing.FakeItEasy" Version="9.0.4" />
87-
<PackageVersion Include="Rocket.Surgery.Extensions.Testing.XUnit" Version="9.0.4" />
86+
<PackageVersion Include="Rocket.Surgery.Extensions.Testing.XUnit3" Version="9.0.4" />
8887
<PackageVersion Include="Rocket.Surgery.Extensions.Testing.SourceGenerators" Version="9.0.4" />
89-
<PackageVersion Include="xunit" Version="2.9.3" />
88+
<PackageVersion Include="xunit.v3" Version="1.1.0" />
9089
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.2" />
9190
<PackageVersion Include="Verify.AngleSharp" Version="4.5.0" />
9291
<PackageVersion Include="Verify.ImageMagick" Version="3.6.0" />
93-
<PackageVersion Include="Verify.Xunit" Version="28.12.0" />
92+
<PackageVersion Include="Verify.XunitV3" Version="28.12.0" />
9493
<PackageVersion Include="Verify.Playwright" Version="3.0.0" />
9594
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
9695
</ItemGroup>
9796
<Import
9897
Project="$(MSBuildThisFileDirectory)/Directory.Packages.support.props"
99-
Condition="Exists('$(MSBuildThisFileDirectory)/Directory.Packages.support.props')" />
98+
Condition="Exists('$(MSBuildThisFileDirectory)/Directory.Packages.support.props')"
99+
/>
100100
</Project>

MSBuild.Targets.sln

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
32
# Visual Studio Version 16
43
VisualStudioVersion = 16.0.29006.145
54
MinimumVisualStudioVersion = 15.0.26124.0
65
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8FFDF555-DB50-45F9-9A2D-6410F39151C3}"
76
ProjectSection(SolutionItems) = preProject
8-
src\Directory.Build.props = src\Directory.Build.props
9-
src\Directory.Build.targets = src\Directory.Build.targets
10-
src\.editorconfig = src\.editorconfig
117
EndProjectSection
128
EndProject
139
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{DF33E0FB-9790-4654-B60F-8AB22E0CC3D1}"
1410
ProjectSection(SolutionItems) = preProject
15-
test\Directory.Build.props = test\Directory.Build.props
16-
test\Directory.Build.targets = test\Directory.Build.targets
17-
test\.editorconfig = test\.editorconfig
1811
EndProjectSection
1912
EndProject
2013
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = ".build", ".build\.build.csproj", "{55EFAE2F-A7CA-4917-A381-4A6EF5E79610}"
@@ -41,7 +34,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{C3D5FC
4134
build.ps1 = build.ps1
4235
build.sh = build.sh
4336
GitReleaseManager.yaml = GitReleaseManager.yaml
44-
.appveyor.yml = .appveyor.yml
4537
.codecov.yml = .codecov.yml
4638
GitVersion.yml = GitVersion.yml
4739
LICENSE = LICENSE
@@ -51,41 +43,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{C3D5FC
5143
.gitmodules = .gitmodules
5244
.prettierignore = .prettierignore
5345
.prettierrc = .prettierrc
54-
.lintstagedrc.js = .lintstagedrc.js
5546
NuGet.config = NuGet.config
5647
package.json = package.json
5748
EndProjectSection
5849
EndProject
59-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{6805A374-5E01-4A83-B179-224D0765AE04}"
60-
ProjectSection(SolutionItems) = preProject
61-
.github\dependabot.yml = .github\dependabot.yml
62-
.github\labels.yml = .github\labels.yml
63-
.github\release.yml = .github\release.yml
64-
.github\renovate.json = .github\renovate.json
65-
EndProjectSection
66-
EndProject
67-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{54992192-69EB-4F29-8466-778DA6530296}"
68-
ProjectSection(SolutionItems) = preProject
69-
.github\workflows\ci-ignore.yml = .github\workflows\ci-ignore.yml
70-
.github\workflows\ci.yml = .github\workflows\ci.yml
71-
.github\workflows\close-milestone.yml = .github\workflows\close-milestone.yml
72-
.github\workflows\dependabot-merge.yml = .github\workflows\dependabot-merge.yml
73-
.github\workflows\draft-release.yml = .github\workflows\draft-release.yml
74-
.github\workflows\sync-labels.yml = .github\workflows\sync-labels.yml
75-
.github\workflows\update-milestone.yml = .github\workflows\update-milestone.yml
76-
.github\workflows\lint.yml = .github\workflows\lint.yml
77-
EndProjectSection
78-
EndProject
79-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".husky", ".husky", "{78E64421-1156-466E-A77B-35DFD431C2E4}"
80-
ProjectSection(SolutionItems) = preProject
81-
.husky\pre-commit = .husky\pre-commit
82-
EndProjectSection
83-
EndProject
84-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".vscode", ".vscode", "{234984AF-8902-4FEC-AD1B-EC538A0FEB23}"
85-
ProjectSection(SolutionItems) = preProject
86-
.vscode\settings.json = .vscode\settings.json
87-
EndProjectSection
88-
EndProject
8950
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rocket.Surgery.MSBuild.GlobalAnalyzerConfig", "src\GlobalAnalyzerConfig\Rocket.Surgery.MSBuild.GlobalAnalyzerConfig.csproj", "{13C2EA12-2841-4051-9CBD-89A9E56B2445}"
9051
EndProject
9152
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rocket.Surgery.MyAssembly", "src\MyAssembly\Rocket.Surgery.MyAssembly.csproj", "{6FEAB5FC-B4CF-421F-B4E5-6386E19235B2}"
@@ -243,10 +204,6 @@ Global
243204
{30E3AA0F-4FFA-40B8-B854-33648EC701CC} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3}
244205
{AA0D3BF8-C358-4E5D-B937-A8C15D60614D} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3}
245206
{5EDE7E88-5A85-4855-A09F-55F0C840BB93} = {DF33E0FB-9790-4654-B60F-8AB22E0CC3D1}
246-
{6805A374-5E01-4A83-B179-224D0765AE04} = {C3D5FCFC-8271-4A98-BF22-D56EFD1206E8}
247-
{54992192-69EB-4F29-8466-778DA6530296} = {6805A374-5E01-4A83-B179-224D0765AE04}
248-
{78E64421-1156-466E-A77B-35DFD431C2E4} = {C3D5FCFC-8271-4A98-BF22-D56EFD1206E8}
249-
{234984AF-8902-4FEC-AD1B-EC538A0FEB23} = {C3D5FCFC-8271-4A98-BF22-D56EFD1206E8}
250207
{13C2EA12-2841-4051-9CBD-89A9E56B2445} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3}
251208
{6FEAB5FC-B4CF-421F-B4E5-6386E19235B2} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3}
252209
{6BE57205-8535-45E7-B2EB-94F88987EB49} = {8FFDF555-DB50-45F9-9A2D-6410F39151C3}

src/MyAssembly/ConstantsGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3232
if (value != null && value.StartsWith("|") && value.EndsWith("|"))
3333
value = value[1..^1].Replace('|', ';');
3434

35-
var name = x.path;
35+
var name = Path.GetFileName(x.path);
3636
if (string.IsNullOrEmpty(root))
3737
{
3838
root = "Constants";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"Generators": {
5+
"commandName": "DebugRoslynComponent",
6+
"targetProject": "/Users/david/Development/RocketSurgeonsGuild/MSBuild.Targets/test/MyAssembly.Tests/MyAssembly.Tests.csproj"
7+
}
8+
}
9+
}

src/MyAssembly/ResourcesGenerator.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1717
.Combine(context.AnalyzerConfigOptionsProvider)
1818
.Select((tuple, _) => ( file: tuple.Left, path: tuple.Left.Path, options: tuple.Right.GetOptions(tuple.Left) ))
1919
.Where(x => x.options.TryGetValue("build_metadata.EmbeddedResource.Value", out _))
20-
.Select((x, ct) =>
20+
.Combine(context.CompilationProvider)
21+
.Select((a, ct) =>
2122
{
23+
var x = a.Left;
24+
var c = a.Right;
2225
x.options.TryGetValue("build_metadata.EmbeddedResource.Value", out var resourceName);
2326
x.options.TryGetValue("build_metadata.EmbeddedResource.Area", out var area);
2427
x.options.TryGetValue("build_metadata.EmbeddedResource.Comment", out var comment);
25-
return ( x.path, resourceName: resourceName!, area: string.IsNullOrWhiteSpace(area) ? null : area, comment: string.IsNullOrWhiteSpace(comment) ? null : comment );
28+
x.options.TryGetValue("build_metadata.EmbeddedResource.LogicalName", out var logicalName);
29+
resourceName = string.Join(".", ( resourceName ?? "" ).Split(['/', '\\', '.', '-'], StringSplitOptions.RemoveEmptyEntries));
30+
area = string.IsNullOrWhiteSpace(area) ? null : string.Join("/", area!.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries));
31+
32+
return (
33+
logicalName ?? $"{c.AssemblyName}.{resourceName}",
34+
resourceName,
35+
area,
36+
comment: string.IsNullOrWhiteSpace(comment) ? null : comment
37+
);
2638
}
2739
);
2840

@@ -43,14 +55,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4355

4456
static void GenerateSource(
4557
SourceProductionContext spc,
46-
((string path, string resourceName, string area, string comment) resource, (string, string) Right) valueTuple
58+
((string logicalName, string resourceName, string? area, string? comment) resource, (string?, string?) Right) valueTuple
4759
)
4860
{
49-
var ((path, resourceName, area, comment), (ns, visibility)) = valueTuple;
50-
51-
var rest = area.Split('.', StringSplitOptions.RemoveEmptyEntries);
61+
var ((logicalName, resourceName, area, comment), (ns, visibility)) = valueTuple;
5262

53-
var property = MethodDeclaration(ParseTypeName("System.IO.Stream"), Identifier(resourceName.Replace(".", "").Replace("-", "_")))
63+
var property = MethodDeclaration(ParseTypeName("System.IO.Stream"), Identifier($"{area}/{resourceName}".Replace("/", "_").Replace(".", "_")))
5464
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.StaticKeyword)))
5565
.WithExpressionBody(
5666
ArrowExpressionClause(
@@ -71,30 +81,20 @@ static void GenerateSource(
7181
Argument(
7282
LiteralExpression(
7383
SyntaxKind.StringLiteralExpression,
74-
Literal(path)
84+
Literal(logicalName)
7585
)
7686
)
7787
)
7888
)
7989
)
8090
)
8191
)
82-
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
83-
.AddSummary(comment);
92+
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
93+
if (comment is { Length: > 0 }) property = property.AddSummary(comment);
8494

85-
var classDefinition = rest
86-
.AsEnumerable()
87-
.Reverse()
88-
.Aggregate<string, MemberDeclarationSyntax>(
89-
property,
90-
(definition, parent) => ClassDeclaration(parent)
91-
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.PartialKeyword)))
92-
.AddMembers(definition)
93-
.AddSummary($"Provides access to embedded resources under {parent}")
94-
);
95-
classDefinition = ClassDeclaration("Resources")
95+
var classDefinition = ClassDeclaration("Resources")
9696
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.PartialKeyword)))
97-
.AddMembers(classDefinition)
97+
.AddMembers(property)
9898
.AddSummary("Provides access to embedded resources");
9999

100100
classDefinition = ClassDeclaration("MyAssembly")
@@ -104,6 +104,6 @@ static void GenerateSource(
104104
var cu = CompilationUnit();
105105
cu = ns is { Length: > 0 } ? cu.AddMembers(NamespaceDeclaration(ParseName(ns)).AddMembers(classDefinition)) : cu.AddMembers(classDefinition);
106106

107-
spc.AddSource($"{area}.{resourceName}.g.cs", SourceText.From(cu.NormalizeWhitespace().GetText().ToString(), Encoding.UTF8));
107+
spc.AddSource($"{resourceName}.g.cs", SourceText.From(cu.NormalizeWhitespace().GetText().ToString(), Encoding.UTF8));
108108
}
109109
}

src/MyAssembly/build/MyAssembly.Resources.targets

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<CompilerVisibleItemMetadata Include="EmbeddedResource" MetadataName="Area" />
44
<CompilerVisibleItemMetadata Include="EmbeddedResource" MetadataName="Comment" />
55
<CompilerVisibleItemMetadata Include="EmbeddedResource" MetadataName="Value" />
6+
<CompilerVisibleItemMetadata Include="EmbeddedResource" MetadataName="LogicalName" />
67
</ItemGroup>
78

89
<ItemDefinitionGroup>
@@ -11,24 +12,23 @@
1112
<AreaPath />
1213
<Area />
1314
<Comment />
15+
<LogicalName />
1416
<Value />
1517
</EmbeddedResource>
1618
</ItemDefinitionGroup>
1719

1820
<Target Name="PrepareEmbeddedResources" DependsOnTargets="PrepareResourceNames" />
1921

20-
<Target Name="_InjectResourcesAdditionalFiles"
21-
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun"
22-
DependsOnTargets="PrepareEmbeddedResources">
22+
<Target Name="_InjectResourcesAdditionalFiles" BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun" DependsOnTargets="PrepareEmbeddedResources">
2323
<ItemGroup>
2424
<EmbeddedResource Condition="!$([System.IO.Path]::IsPathRooted('%(RelativeDir)')) OR '%(Link)' != ''">
25-
<AreaPath Condition="!$([System.IO.Path]::IsPathRooted('%(RelativeDir)'))">%(RelativeDir)%(Filename)</AreaPath>
26-
<AreaPath Condition="'%(Link)' != ''">$([System.IO.Path]::GetDirectoryName('%(Link)'))$([System.IO.Path]::DirectorySeparatorChar)$([System.IO.Path]::GetFileNameWithoutExtension('%(Link)'))</AreaPath>
25+
<AreaPath Condition="!$([System.IO.Path]::IsPathRooted('%(RelativeDir)'))">%(RelativeDir)</AreaPath>
26+
<AreaPath Condition="'%(Link)' != ''">$([System.IO.Path]::GetDirectoryName('%(Link)'))$([System.IO.Path]::DirectorySeparatorChar)</AreaPath>
2727
<FileExtension Condition="!$([System.IO.Path]::IsPathRooted('%(RelativeDir)'))">%(Extension)</FileExtension>
2828
<FileExtension Condition="'%(Link)' != ''">$([System.IO.Path]::GetExtension('%(Link)'))</FileExtension>
2929
</EmbeddedResource>
3030
<EmbeddedResource Condition="'%(AreaPath)' != ''">
31-
<Area>$([MSBuild]::ValueOrDefault('%(AreaPath)', '').Replace('\', '.').Replace('/', '.'))</Area>
31+
<Area>$([MSBuild]::ValueOrDefault('%(AreaPath)', ''))</Area>
3232
<Value>%(AreaPath)%(FileExtension)</Value>
3333
</EmbeddedResource>
3434
<AdditionalFiles Include="@(EmbeddedResource)" />

src/MyAssembly/build/Rocket.Surgery.MyAssembly.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<Import Project="$(MSBuildThisFileDirectory)MyAssembly.Constants.targets" />
33
<Import Project="$(MSBuildThisFileDirectory)MyAssembly.Metadata.targets" />
44
<Import Project="$(MSBuildThisFileDirectory)MyAssembly.Project.targets" />
5+
<Import Project="$(MSBuildThisFileDirectory)MyAssembly.Resources.targets" />
56
<Import Project="$(MSBuildThisFileDirectory)MyAssembly.targets" />
67
</Project>

test/Directory.Build.targets

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
<ItemGroup>
33
<PackageReference Include="Microsoft.NET.Test.Sdk" />
44
<PackageReference Include="Rocket.Surgery.Extensions.Testing.FakeItEasy" />
5-
<PackageReference Include="Rocket.Surgery.Extensions.Testing.XUnit" />
5+
<PackageReference Include="Rocket.Surgery.Extensions.Testing.XUnit3" />
66
<PackageReference Include="Bogus" />
77
<PackageReference Include="Microsoft.CodeCoverage" />
88
<PackageReference Include="FakeItEasy" />
99
<PackageReference Include="Shouldly" />
10-
<PackageReference Include="Verify.Xunit" />
10+
<PackageReference Include="Verify.XunitV3" />
1111
<Using Include="Shouldly" />
12-
<PackageReference Include="xunit" />
12+
<PackageReference Include="xunit.v3" />
1313
<PackageReference Include="xunit.runner.visualstudio" />
1414
</ItemGroup>
1515
<Import Project="../src/MyAssembly/build/Rocket.Surgery.MyAssembly.targets" />
1616
<Import Project="..\src\CI\build\Rocket.Surgery.MSBuild.CI.targets" />
1717
<Import Project="..\src\GitVersion\build\Rocket.Surgery.MSBuild.GitVersion.targets" />
1818
<Import Project="..\src\JetBrains\build\Rocket.Surgery.MSBuild.JetBrains.Annotations.targets" />
1919
<Import Project="..\src\SourceLink\build\Rocket.Surgery.MSBuild.SourceLink.targets" />
20-
<Import
21-
Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />
20+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />
2221
</Project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace Rocket.Surgery.MyAssembly.Tests;
2+
3+
public class ConstantsGeneratorTests(ITestContextAccessor testOutputHelper) : GeneratorTest(testOutputHelper)
4+
{
5+
[Fact]
6+
public async Task Should_Add_A_Constant()
7+
{
8+
var result = await Builder
9+
.AddConstant(
10+
"Test",
11+
"Value",
12+
"",
13+
"comment",
14+
"Constant",
15+
"rootComment"
16+
)
17+
.Build()
18+
.GenerateAsync();
19+
await Verify(result);
20+
}
21+
22+
[Fact]
23+
public async Task Should_Add_A_Nested_Constant()
24+
{
25+
var result = await Builder
26+
.AddConstant(
27+
"Test",
28+
"Value",
29+
"",
30+
"comment",
31+
"Some.Nested.Root",
32+
"should be some"
33+
)
34+
.Build()
35+
.GenerateAsync();
36+
await Verify(result);
37+
}
38+
39+
[Fact]
40+
public async Task Should_Add_A_Embedded_Resource()
41+
{
42+
var result = await Builder
43+
.AddEmbeddedResource(
44+
"somefile.cs",
45+
"somefile.cs",
46+
"kind",
47+
"comment"
48+
)
49+
.Build()
50+
.GenerateAsync();
51+
await Verify(result);
52+
}
53+
54+
[Fact]
55+
public async Task Should_Add_A_Nested_Embedded_Resource()
56+
{
57+
var result = await Builder
58+
.AddEmbeddedResource(
59+
"something/else/somefile.cs",
60+
"something.somefile.cs",
61+
"something/else",
62+
"comment"
63+
)
64+
.Build()
65+
.GenerateAsync();
66+
await Verify(result);
67+
}
68+
}

0 commit comments

Comments
 (0)