Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions BlazorAppTest/BlazorAppTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
</Compile>
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="**\*.resx" />
</ItemGroup>
</Project>

This file was deleted.

2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="$(AspNetCoreVersion9)" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="$(AspNetCoreVersion9)" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Localization" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Localization.Abstractions" Version="9.0.1" />
Expand Down
7 changes: 7 additions & 0 deletions Havit.Blazor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Havit.Blazor.TestApp", "Hav
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Havit.Blazor.TestApp.Client", "Havit.Blazor.TestApp\Havit.Blazor.TestApp.Client\Havit.Blazor.TestApp.Client.csproj", "{38D87399-13C1-4C86-9343-712EAE6095F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Havit.SourceGenerators.StrongApiStringLocalizers.Tests", "Havit.SourceGenerators.StrongApiStringLocalizers.Tests\Havit.SourceGenerators.StrongApiStringLocalizers.Tests.csproj", "{9236499E-62FF-4C2F-92A0-408143D67E72}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -180,6 +182,10 @@ Global
{38D87399-13C1-4C86-9343-712EAE6095F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38D87399-13C1-4C86-9343-712EAE6095F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38D87399-13C1-4C86-9343-712EAE6095F0}.Release|Any CPU.Build.0 = Release|Any CPU
{9236499E-62FF-4C2F-92A0-408143D67E72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9236499E-62FF-4C2F-92A0-408143D67E72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9236499E-62FF-4C2F-92A0-408143D67E72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9236499E-62FF-4C2F-92A0-408143D67E72}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -196,6 +202,7 @@ Global
{E64B4752-B697-4B5A-91F0-8A8581B1ABE5} = {79C29E4F-EF98-4F43-9782-B0D58A533C4D}
{F6A546C8-60C3-47AC-A58B-66E3513DCC7A} = {79C29E4F-EF98-4F43-9782-B0D58A533C4D}
{45BC138B-4F00-43DD-BF5D-D2FC36972857} = {D7B56FC7-6322-4B66-B34F-A972805BF740}
{9236499E-62FF-4C2F-92A0-408143D67E72} = {201D627C-BC35-4971-95D0-5BA656E771F6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BA2F0FE2-9DA0-4C29-8772-0802E2E67119}
Expand Down
127 changes: 127 additions & 0 deletions Havit.SourceGenerators.StrongApiStringLocalizers.Tests/Global.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="CzechAndHtml" xml:space="preserve">
<value>Čeština je &lt;b&gt;skvělá&lt;/b&gt;!</value>
</data>
<data name="HelloWorld" xml:space="preserve">
<value>Hello world!!!</value>
<comment>Hello world resource comment.</comment>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Remove="Global.resx" />
</ItemGroup>

<ItemGroup>
<Content Include="Global.resx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
<PackageReference Include="MSTest" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Havit.SourceGenerators.StrongApiStringLocalizers\Havit.SourceGenerators.StrongApiStringLocalizers.csproj" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;

namespace Havit.SourceGenerators.StrongApiStringLocalizers.Tests;

[TestClass]
public class StrongApiStringLocalizersGeneratorTests
{
[TestMethod]
public async Task StrongApiStringLocalizersGenerator_Test()
{
// Arrange

using var globalResxStream = File.OpenRead("Global.resx");

string projectDir = Environment.CurrentDirectory;

var test = new Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<StrongApiStringLocalizersGenerator, Microsoft.CodeAnalysis.Testing.DefaultVerifier>
{
TestState =
{
AnalyzerConfigFiles =
{
($"/.editorconfig", $@"""
is_global=true
build_property.RootNamespace = MyApp.Resources
build_property.ProjectDir = {projectDir}
""")
}
},
ReferenceAssemblies = ReferenceAssemblies.Net
.Net90
.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Localization", "9.0.1"))) // we are using IStringLocalizer from this package in the generated code
};

// resource file
test.TestState.AdditionalFiles.Add((Path.Combine(projectDir, "MyResources", "Global.resx"), SourceText.From(globalResxStream)));

// EXPECTED OUTPUT

test.TestState.GeneratedSources.Add((typeof(StrongApiStringLocalizersGenerator), "MyApp.Resources.MyResources.IGlobalLocalizer.g.cs", @"// <auto-generated />

namespace MyApp.Resources.MyResources;

using System.CodeDom.Compiler;
using Microsoft.Extensions.Localization;

[GeneratedCode(""Havit.SourceGenerators.StrongApiStringLocalizers.StrongApiStringLocalizersGenerator"", ""2.0.0.0"")]
public interface IGlobalLocalizer : IStringLocalizer
{
/// <summary>
/// Čeština je &lt;b&gt;skvělá&lt;/b&gt;!
/// </summary>
LocalizedString CzechAndHtml { get; }

/// <summary>
/// Hello world resource comment.
/// </summary>
LocalizedString HelloWorld { get; }

}
"));

// TestProject - defined by the TestState implementation
test.TestState.GeneratedSources.Add((typeof(StrongApiStringLocalizersGenerator), $"MyApp.Resources.MyResources.GlobalLocalizer.g.cs", @"// <auto-generated />

namespace MyApp.Resources.MyResources;

using System.CodeDom.Compiler;
using System.Collections.Generic;
using Microsoft.Extensions.Localization;

[GeneratedCode(""Havit.SourceGenerators.StrongApiStringLocalizers.StrongApiStringLocalizersGenerator"", ""2.0.0.0"")]
public class GlobalLocalizer : IGlobalLocalizer
{
private readonly IStringLocalizer _localizer;

public GlobalLocalizer(IStringLocalizerFactory stringLocalizerFactory)
{
_localizer = stringLocalizerFactory.Create(""MyResources.Global"", ""TestProject"");
}

/// <summary>
/// Čeština je &lt;b&gt;skvělá&lt;/b&gt;!
/// </summary>
public LocalizedString CzechAndHtml => _localizer[""CzechAndHtml""];

/// <summary>
/// Hello world resource comment.
/// </summary>
public LocalizedString HelloWorld => _localizer[""HelloWorld""];

LocalizedString IStringLocalizer.this[string name] => _localizer[name];
LocalizedString IStringLocalizer.this[string name, params object[] arguments] => _localizer[name, arguments];
IEnumerable<LocalizedString> IStringLocalizer.GetAllStrings(bool includeParentCultures) => _localizer.GetAllStrings(includeParentCultures);
}
"));

test.TestState.GeneratedSources.Add((typeof(StrongApiStringLocalizersGenerator), "MyApp.Resources.ServiceCollectionExtensions.g.cs", @"// <auto-generated />

namespace MyApp.Resources;

using System.CodeDom.Compiler;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;

[GeneratedCode(""Havit.SourceGenerators.StrongApiStringLocalizers.StrongApiStringLocalizersGenerator"", ""2.0.0.0"")]
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddGeneratedResourceWrappers(this IServiceCollection services)
{
services.AddTransient<MyApp.Resources.MyResources.IGlobalLocalizer, MyApp.Resources.MyResources.GlobalLocalizer>();
return services;
}
}
"));

// Act + Assert
await test.RunAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
HLG1001 | Usage | Warning | No resx file found
HLG1002 | Usage | Warning | Cannot parse RESX file

This file was deleted.

Loading
Loading