Skip to content

Commit

Permalink
feat: HResourcesGenerator_Resource="true" is required now for custom …
Browse files Browse the repository at this point in the history
…resources.
  • Loading branch information
HavenDV committed Feb 26, 2025
1 parent 9e9b439 commit a20c524
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var text = H.Resources.name_txt.AsText();

<ItemGroup Label="Images">
<EmbeddedResource Include="Images\*.png" />
<AdditionalFiles Include="Images\*.png" />
<AdditionalFiles Include="Images\*.png" HResourcesGenerator_Resource="true" />
</ItemGroup>
```

Expand Down Expand Up @@ -62,7 +62,7 @@ By default, it includes this code:
```xml
<ItemGroup Condition="$(HResourcesGenerator_AddResourcesFolder)">
<EmbeddedResource Include="Resources\**\*.*" />
<AdditionalFiles Include="Resources\**\*.*" />
<AdditionalFiles Include="Resources\**\*.*" HResourcesGenerator_Resource="true" />
<Compile Remove="Resources\**\*.*" />
</ItemGroup>
```
Expand Down
2 changes: 1 addition & 1 deletion src/libs/H.Resources.Generator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace {@namespace}
{modifier} static class {className}
{{
{
string.Join(Environment.NewLine, properties.Select(static resource =>
string.Join("\n", properties.Select(static resource =>
$" public static Resource {resource.name} => new Resource(\"{resource.fileName}\");"))
}
}}
Expand Down
5 changes: 3 additions & 2 deletions src/libs/H.Resources.Generator/H.Resources.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NoWarn>$(NoWarn);NU5128;CA1031</NoWarn>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<PropertyGroup Label="NuGet">
Expand All @@ -25,8 +26,8 @@ Suitable for situations where you want to have a multi-target solution containin
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="H.Generators.Extensions" Version="1.24.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
<PackageReference Include="H.Generators.Extensions" Version="1.24.2" PrivateAssets="all" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion src/libs/H.Resources.Generator/H.Resources.Generator.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<CompilerVisibleProperty Include="HResourcesGenerator_WithSystemDrawing"/>
</ItemGroup>

<ItemGroup>
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="HResourcesGenerator_Resource" />
</ItemGroup>

<ItemGroup>
<!--<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="HResourcesGenerator_Type" />-->
</ItemGroup>
Expand All @@ -19,7 +23,7 @@

<ItemGroup Condition="$(HResourcesGenerator_AddResourcesFolder)">
<EmbeddedResource Include="Resources\**\*.*" />
<AdditionalFiles Include="Resources\**\*.*" />
<AdditionalFiles Include="Resources\**\*.*" HResourcesGenerator_Resource="true" />
<Compile Remove="Resources\**\*.*" />
</ItemGroup>

Expand Down
5 changes: 4 additions & 1 deletion src/libs/H.Resources.Generator/HResourcesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class HResourcesGenerator : IIncrementalGenerator
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.AdditionalTextsProvider
.Select(static (x, _) => new Resource(x.Path))
.Combine(context.AnalyzerConfigOptionsProvider)
.Where(static pair =>
pair.Right.GetOption(pair.Left, "Resource", prefix: "HResourcesGenerator")?.ToUpperInvariant() == "TRUE")
.Select(static (x, _) => new Resource(x.Left.Path))
.Collect()
.Combine(context.AnalyzerConfigOptionsProvider)
.SelectAndReportExceptions(GetSourceCode, context, Id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup Label="Base packages">
<Import Project="$(MSBuildThisFileDirectory)../../libs/H.Resources.Generator/H.Resources.Generator.props" />

<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.8.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.8.2" />
<PackageReference Include="FluentAssertions" Version="8.1.1" />
<PackageReference Include="AwesomeAssertions" Version="8.0.1" />
</ItemGroup>

<ItemGroup Label="GlobalUsings">
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
<Using Include="FluentAssertions" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\**\*.*" />
<AdditionalFiles Include="Resources\**\*.*" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\libs\H.Resources.Generator\H.Resources.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="H.Generators.Extensions" Version="1.24.1" />
<PackageReference Include="H.Generators.Tests.Extensions" Version="1.24.1" />
<PackageReference Include="H.Generators.Extensions" Version="1.24.2" />
<PackageReference Include="H.Generators.Tests.Extensions" Version="1.24.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.8.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.8.2" />
<PackageReference Include="FluentAssertions" Version="8.1.1" />
<PackageReference Include="AwesomeAssertions" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" Version="1.1.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.13.0" />
<PackageReference Include="Verify.MSTest" Version="28.12.1" />
<PackageReference Include="Verify.MSTest" Version="28.13.0" />
<PackageReference Include="Verify.SourceGenerators" Version="2.5.0" />
</ItemGroup>

Expand Down
14 changes: 13 additions & 1 deletion src/tests/H.Resources.Generator.SnapshotTests/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using H.Generators;
using H.Generators.Tests.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
Expand All @@ -13,6 +14,16 @@ public static async Task CheckSourceAsync(
AdditionalText[] additionalTexts,
CancellationToken cancellationToken = default)
{
var globalOptions = new Dictionary<string, string>();

var additionalTextOptions = new Dictionary<string, Dictionary<string, string>>();
foreach (var additionalText in additionalTexts)
{
var options = new Dictionary<string, string>();
options.TryAdd("build_metadata.AdditionalFiles.HResourcesGenerator_Resource", "true");
additionalTextOptions.Add(additionalText.Path, options);
}

var referenceAssemblies = ReferenceAssemblies.Net.Net60;
var references = await referenceAssemblies.ResolveAsync(null, cancellationToken);
var compilation = (Compilation)CSharpCompilation.Create(
Expand All @@ -21,7 +32,8 @@ public static async Task CheckSourceAsync(
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var driver = CSharpGeneratorDriver
.Create(new HResourcesGenerator())
.AddAdditionalTexts(ImmutableArray.Create(additionalTexts))
.AddAdditionalTexts([..additionalTexts])
.WithUpdatedAnalyzerConfigOptions(new DictionaryAnalyzerConfigOptionsProvider(globalOptions, additionalTextOptions: additionalTextOptions))
.RunGeneratorsAndUpdateCompilation(compilation, out compilation, out _, cancellationToken);
var diagnostics = compilation.GetDiagnostics(cancellationToken);

Expand Down

0 comments on commit a20c524

Please sign in to comment.