add version 0.0.1

This commit is contained in:
2023-02-26 19:03:30 +01:00
parent cedb0bda76
commit 9da07371e9
39 changed files with 2226 additions and 0 deletions

View 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; }
}

View File

@@ -0,0 +1,18 @@
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 Option<string> Comment { get; init;}
public string Value { get; }
}

View File

@@ -0,0 +1,126 @@
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)
{
return Properties.Resources.ResourceManagerMemberTemplate.From(
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 stringResourses = resources.Where(r => r.Type.Match(
v => typeof(string).IsAssignableFrom(Type.GetType(v.Trim(), false, true)),
() => true
));
return stringResourses.Select(r =>
{
var openBraces = r.Value
.Replace("{{", string.Empty)
.Replace("{{", string.Empty)
.Split('{')
.Skip(1)
.ToArray();
if (openBraces.Any())
{
var formatNames = new System.Collections.Generic.HashSet<string>(
openBraces.Select(b => b.Split('}').First())
);
var resolveParameterNames = r.Comment.Match(
c => c.Split(',')
.Select(str => str.Split('='))
.Where(parts => parts.Count() == 2)
.Select(parts => (
parts.First().Trim().Trim('{', '}').Split(' ').First(),
parts.Last().Trim().Split(' ').First()
)).Where(t => uint.TryParse(t.Item1, out var _))
.GroupBy(t => t.Item1)
.ToDictionary(k => k.Key, v => v.First().Item2),
() => new Dictionary<string, string>()
);
var parameterNames = formatNames.Select(
n => resolveParameterNames.TryGetValue(n.Trim(), out var paramName)
? paramName.Substring(0, 1).ToLower() + paramName.Substring(1)
: uint.TryParse(n, out var value) ? $"p{value + 1}" : n
).ToArray();
var from = Properties.Resources.ResourceFormatClassFromMethod.From(
new JoinedResourceString(
LiteralString.Factory(", "),
parameterNames.Select(n => LiteralString.Factory($"{nameof(IResourceString)} {n}")).ToArray()
),
new JoinedResourceString(
LiteralString.Factory($",{System.Environment.NewLine} "),
parameterNames.Select(n => LiteralString.Factory($"{n}")).ToArray()
)
);
return Properties.Resources.ResourceFormatClassMembers.From(
LiteralString.Factory(r.Name), resourceManagerName, from
);
}
return Properties.Resources.ResourceStringMembers.From(
LiteralString.Factory(r.Name), resourceManagerName
);
});
}
public static IResourceString CreateResourceClassCodeSnippet(
string namespaceString,
string resourceClassName,
IResourceString resourceManagerSnippet,
IEnumerable<IResourceString> memberSnippets
)
{
return Properties.Resources.ResourcesClassTemplate.From(
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)
);
}
}

View File

@@ -0,0 +1,3 @@
namespace System.Runtime.CompilerServices;
internal static class IsExternalInit { }

View File

@@ -0,0 +1,44 @@
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 comment = i.SelectSingleNode("descendant::comment")?.InnerXml ?? string.Empty;
var value = i.SelectSingleNode("descendant::value")?.InnerXml ?? string.Empty;
return new Resource(name, value)
{
Type = string.IsNullOrWhiteSpace(type)
? Option<string>.None
: Option<string>.Some(type),
Comment = string.IsNullOrWhiteSpace(comment)
? Option<string>.None
: Option<string>.Some(comment)
};
}).ToArray();
return resources.Any()
? Option<File>.Some(new File(resources))
: Option<File>.None;
}
}

View File

@@ -0,0 +1,125 @@
using ResourceString.Net.Contract;
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 ?? string.Empty, _Type.Assembly),
LazyThreadSafetyMode.PublicationOnly
);
public static ResourceManager ResourceManager => _ResourceManager.Value;
#endregion // ResourceManager
internal static class ResourceStringMembers
{
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceStringMembers", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString resourceId, IResourceString resourceManagerPropertyName) => new FormattedResourceString(
Format,
resourceId,
resourceManagerPropertyName
);
}
internal static class ResourceFormatClassMembers
{
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceFormatClassMembers", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString resourceId, IResourceString resourceManagerPropertyName, IResourceString fromMethodDefinition) => new FormattedResourceString(
Format,
resourceId,
resourceManagerPropertyName,
fromMethodDefinition
);
}
internal static class ResourceFormatClassFromMethod
{
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceFormatClassFromMethod", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString fromMethodSignature, IResourceString parameterNames) => new FormattedResourceString(
Format,
fromMethodSignature,
parameterNames
);
}
internal static class ResourcesClassTemplate
{
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourcesClassTemplate", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString ns, IResourceString className, IResourceString resourceManagerRegion, IResourceString resourceRegions) => new FormattedResourceString(
Format,
ns,
className,
resourceManagerRegion,
resourceRegions
);
}
internal static class ResourceManagerMemberTemplate
{
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("ResourceManagerMemberTemplate", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString resourceManagerTypeName) => new FormattedResourceString(
Format,
resourceManagerTypeName
);
}
#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
}
}

View File

@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<data name="ResourceStringMembers" xml:space="preserve">
<value>
#region {0}
private static readonly Lazy&lt;IResourceString> Lazy{0} = new Lazy&lt;IResourceString>(
() => new ResourceManagerString("{0}", {1}, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString {0} => Lazy{0}.Value;
#endregion // {0}
</value>
<comment>0 = ResourceId, 1 = ResourceManagerPropertyName</comment>
</data>
<data name="ResourceFormatClassMembers" xml:space="preserve">
<value>
internal static class {0}
{{
private static readonly Lazy&lt;IResourceString> LazyFormat = new Lazy&lt;IResourceString>(
() => new ResourceManagerString("{0}", {1}, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Format => LazyFormat.Value;
{2}
}}
</value>
<comment>0 = ResourceId, 1 = ResourceManagerPropertyName, 2 = FromMethodDefinition</comment>
</data>
<data name="ResourceFormatClassFromMethod" xml:space="preserve">
<value>
public static IResourceString From({0}) => new FormattedResourceString(
Format,
{1}
);
</value>
<comment>0 = FromMethodSignature, 1 = ParameterNames</comment>
</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>
<comment>0 = ns, 1 = ClassName, 2 = ResourceManagerRegion, 3 = ResourceRegions</comment>
</data>
<data name="ResourceManagerMemberTemplate" xml:space="preserve">
<value>
#region ResourceManager
private static readonly Type _Type = typeof({0});
private static readonly Lazy&lt;ResourceManager> _ResourceManager = new Lazy&lt;ResourceManager>(
() => new ResourceManager(_Type.FullName ?? string.Empty, _Type.Assembly),
LazyThreadSafetyMode.PublicationOnly
);
public static ResourceManager ResourceManager => _ResourceManager.Value;
#endregion // ResourceManager
</value>
<comment>0 = ResourceManagerTypeName</comment>
</data>
<data name="DefaultPropertyName_ResourceManager">
<value>ResourceManager</value>
</data>
</root>

View File

@@ -0,0 +1,29 @@
<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.3" />
<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>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>ResourceString.Net.App.Console</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>ResourceString.Net</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,4 @@
global using LanguageExt;
global using ResourceString.Net.Contract;