add draft
This commit is contained in:
16
ResourceString.Net.Logic/DomainObjects/Resx/File.cs
Normal file
16
ResourceString.Net.Logic/DomainObjects/Resx/File.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace ResourceString.Net.Logic.DomainObjects.Resx;
|
||||
|
||||
internal record File
|
||||
{
|
||||
public File(IEnumerable<Resource> resources)
|
||||
{
|
||||
Resources = new Seq<Resource>(
|
||||
resources?.Where(o => o != null) ?? Enumerable.Empty<Resource>()
|
||||
);
|
||||
}
|
||||
|
||||
public IEnumerable<Resource> Resources { get; }
|
||||
}
|
||||
16
ResourceString.Net.Logic/DomainObjects/Resx/Resource.cs
Normal file
16
ResourceString.Net.Logic/DomainObjects/Resx/Resource.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace ResourceString.Net.Logic.DomainObjects.Resx;
|
||||
|
||||
internal record Resource
|
||||
{
|
||||
public Resource(string name, string value)
|
||||
{
|
||||
Name = name ?? string.Empty;
|
||||
Value = value ?? string.Empty;
|
||||
}
|
||||
|
||||
public string Name { get;}
|
||||
|
||||
public Option<string> Type { get; init;}
|
||||
|
||||
public string Value { get; }
|
||||
}
|
||||
80
ResourceString.Net.Logic/Factories/CodeSnippetFactory.cs
Normal file
80
ResourceString.Net.Logic/Factories/CodeSnippetFactory.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using ResourceString.Net.Logic.DomainObjects.Resx;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace ResourceString.Net.Logic.Factories;
|
||||
|
||||
internal static class CodeSnippetFactory
|
||||
{
|
||||
public static IResourceString CreateResourceMangerMemberCodeSnippet(string typeName)
|
||||
{
|
||||
var formatString = Properties.Resources.ResourceManagerMemberTemplate;
|
||||
return new FormattableResourceString(
|
||||
formatString,
|
||||
LiteralString.Factory(typeName)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static IEnumerable<IResourceString> CreateMemberCodeSnippets(IEnumerable<Resource> resources)
|
||||
{
|
||||
return CreateMemberCodeSnippets(
|
||||
resources,
|
||||
Properties.Resources.DefaultPropertyName_ResourceManager
|
||||
);
|
||||
}
|
||||
|
||||
public static IEnumerable<IResourceString> CreateMemberCodeSnippets(
|
||||
IEnumerable<Resource> resources,
|
||||
IResourceString resourceManagerName
|
||||
)
|
||||
{
|
||||
if (resources is null)
|
||||
{
|
||||
return Enumerable.Empty<IResourceString>();
|
||||
}
|
||||
|
||||
var formatString = Properties.Resources.ResourceStringMembers;
|
||||
return resources.Select(r => new FormattableResourceString(
|
||||
formatString,
|
||||
LiteralString.Factory(r.Name),
|
||||
resourceManagerName
|
||||
));
|
||||
}
|
||||
|
||||
public static IResourceString CreateResourceClassCodeSnippet(
|
||||
string namespaceString,
|
||||
string resourceClassName,
|
||||
IResourceString resourceManagerSnippet,
|
||||
IEnumerable<IResourceString> memberSnippets
|
||||
)
|
||||
{
|
||||
var formatString = Properties.Resources.ResourcesClassTemplate;
|
||||
return new FormattableResourceString(
|
||||
formatString,
|
||||
LiteralString.Factory(namespaceString),
|
||||
LiteralString.Factory(resourceClassName),
|
||||
resourceManagerSnippet,
|
||||
new JoinedResourceString(
|
||||
LiteralString.Empty,
|
||||
memberSnippets?.ToArray() ?? Array.Empty<IResourceString>()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static IResourceString CreateResourceClassCodeSnippet(
|
||||
string namespaceString,
|
||||
string resourceClassName,
|
||||
IResourceString resourceManagerSnippet,
|
||||
IEnumerable<Resource> resources
|
||||
)
|
||||
{
|
||||
return CreateResourceClassCodeSnippet(
|
||||
namespaceString,
|
||||
resourceClassName,
|
||||
resourceManagerSnippet,
|
||||
CreateMemberCodeSnippets(resources)
|
||||
);
|
||||
}
|
||||
}
|
||||
3
ResourceString.Net.Logic/IsExternalInit.cs
Normal file
3
ResourceString.Net.Logic/IsExternalInit.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace System.Runtime.CompilerServices;
|
||||
|
||||
internal static class IsExternalInit { }
|
||||
38
ResourceString.Net.Logic/Parsers/Resx/Parser.cs
Normal file
38
ResourceString.Net.Logic/Parsers/Resx/Parser.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using ResourceString.Net.Logic.DomainObjects.Resx;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
|
||||
namespace ResourceString.Net.Logic.Parsers.Resx;
|
||||
|
||||
internal static class Parser
|
||||
{
|
||||
public static Option<File> TryParse(string xmlString)
|
||||
{
|
||||
var doc = new XmlDocument();
|
||||
|
||||
try
|
||||
{
|
||||
doc.LoadXml(xmlString);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Option<File>.None;
|
||||
}
|
||||
|
||||
var resources = doc.SelectNodes("descendant::data").OfType<XmlElement>().Select((i, _) =>
|
||||
{
|
||||
var name = i.GetAttribute("name");
|
||||
var type = i.GetAttribute("type");
|
||||
var value = i.SelectSingleNode("descendant::value")?.InnerXml;
|
||||
|
||||
return new Resource(name, value ?? string.Empty)
|
||||
{
|
||||
Type = type
|
||||
};
|
||||
}).ToArray();
|
||||
|
||||
return resources.Any()
|
||||
? Option<File>.Some(new File(resources))
|
||||
: Option<File>.None;
|
||||
}
|
||||
}
|
||||
67
ResourceString.Net.Logic/Properties/Resources.cs
Normal file
67
ResourceString.Net.Logic/Properties/Resources.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Resources;
|
||||
using System.Threading;
|
||||
|
||||
namespace ResourceString.Net.Logic.Properties
|
||||
{
|
||||
internal static class Resources
|
||||
{
|
||||
#region ResourceManager
|
||||
|
||||
private static readonly Type _Type = typeof(Resources);
|
||||
|
||||
private static readonly Lazy<ResourceManager> _ResourceManager = new Lazy<ResourceManager>(
|
||||
() => new ResourceManager(_Type.FullName, _Type.Assembly),
|
||||
LazyThreadSafetyMode.PublicationOnly
|
||||
);
|
||||
|
||||
public static ResourceManager ResourceManager => _ResourceManager.Value;
|
||||
|
||||
#endregion // ResourceManager
|
||||
|
||||
#region ResourceStringMembers
|
||||
|
||||
private static readonly Lazy<IResourceString> LazyResourceStringMembers = new Lazy<IResourceString>(
|
||||
() => new ResourceManagerString("ResourceStringMembers", ResourceManager, CultureInfo.CurrentCulture),
|
||||
LazyThreadSafetyMode.PublicationOnly
|
||||
);
|
||||
|
||||
public static IResourceString ResourceStringMembers => LazyResourceStringMembers.Value;
|
||||
|
||||
#endregion // ResourceStringMembers
|
||||
|
||||
#region ResourceManagerMemberTemplate
|
||||
|
||||
private static readonly Lazy<IResourceString> LazyResourceManagerMemberTemplate = new Lazy<IResourceString>(
|
||||
() => new ResourceManagerString("ResourceManagerMemberTemplate", ResourceManager, CultureInfo.CurrentCulture),
|
||||
LazyThreadSafetyMode.PublicationOnly
|
||||
);
|
||||
|
||||
public static IResourceString ResourceManagerMemberTemplate => LazyResourceManagerMemberTemplate.Value;
|
||||
|
||||
#endregion // ResourceManagerMemberTemplate
|
||||
|
||||
#region ResourcesClassTemplate
|
||||
|
||||
private static readonly Lazy<IResourceString> LazyResourcesClassTemplate = new Lazy<IResourceString>(
|
||||
() => new ResourceManagerString("ResourcesClassTemplate", ResourceManager, CultureInfo.CurrentCulture),
|
||||
LazyThreadSafetyMode.PublicationOnly
|
||||
);
|
||||
|
||||
public static IResourceString ResourcesClassTemplate => LazyResourcesClassTemplate.Value;
|
||||
|
||||
#endregion // ResourcesClassTemplate
|
||||
|
||||
#region DefaultPropertyName_ResourceManager
|
||||
|
||||
private static readonly Lazy<IResourceString> LazyDefaultPropertyName_ResourceManager = new Lazy<IResourceString>(
|
||||
() => new ResourceManagerString("DefaultPropertyName_ResourceManager", ResourceManager, CultureInfo.CurrentCulture),
|
||||
LazyThreadSafetyMode.PublicationOnly
|
||||
);
|
||||
|
||||
public static IResourceString DefaultPropertyName_ResourceManager => LazyDefaultPropertyName_ResourceManager.Value;
|
||||
|
||||
#endregion // DefaultPropertyName_ResourceManager
|
||||
}
|
||||
}
|
||||
54
ResourceString.Net.Logic/Properties/Resources.resx
Normal file
54
ResourceString.Net.Logic/Properties/Resources.resx
Normal file
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<data name="ResourceStringMembers" xml:space="preserve">
|
||||
<value>
|
||||
#region {0}
|
||||
|
||||
private static readonly Lazy<IResourceString> Lazy{0} = new Lazy<IResourceString>(
|
||||
() => new ResourceManagerString("{0}", {1}, CultureInfo.CurrentCulture),
|
||||
LazyThreadSafetyMode.PublicationOnly
|
||||
);
|
||||
|
||||
public static IResourceString {0} => Lazy{0}.Value;
|
||||
|
||||
#endregion // {0}
|
||||
</value>
|
||||
</data>
|
||||
<data name="ResourcesClassTemplate" xml:space="preserve">
|
||||
<value>
|
||||
using ResourceString.Net.Contract;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Resources;
|
||||
using System.Threading;
|
||||
|
||||
namespace {0}
|
||||
{{
|
||||
internal static class {1}
|
||||
{{
|
||||
{2}
|
||||
{3}
|
||||
}}
|
||||
}}
|
||||
</value>
|
||||
</data>
|
||||
<data name="ResourceManagerMemberTemplate" xml:space="preserve">
|
||||
<value>
|
||||
#region ResourceManager
|
||||
|
||||
private static readonly Type _Type = typeof({0});
|
||||
|
||||
private static readonly Lazy<ResourceManager> _ResourceManager = new Lazy<ResourceManager>(
|
||||
() => new ResourceManager(_Type.FullName, _Type.Assembly),
|
||||
LazyThreadSafetyMode.PublicationOnly
|
||||
);
|
||||
|
||||
public static ResourceManager ResourceManager => _ResourceManager.Value;
|
||||
|
||||
#endregion // ResourceManager
|
||||
</value>
|
||||
</data>
|
||||
<data name="DefaultPropertyName_ResourceManager">
|
||||
<value>ResourceManager</value>
|
||||
</data>
|
||||
</root>
|
||||
23
ResourceString.Net.Logic/ResourceString.Net.Logic.csproj
Normal file
23
ResourceString.Net.Logic/ResourceString.Net.Logic.csproj
Normal file
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LanguageExt.Core" Version="4.4.2" />
|
||||
<PackageReference Include="System.Resources.Extensions" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ResourceString.Net.Contract\ResourceString.Net.Contract.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
4
ResourceString.Net.Logic/Usings.cs
Normal file
4
ResourceString.Net.Logic/Usings.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
global using LanguageExt;
|
||||
|
||||
global using ResourceString.Net.Contract;
|
||||
|
||||
Reference in New Issue
Block a user