-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare for Material Icons font pack as well
- Loading branch information
1 parent
6f27881
commit ca18af8
Showing
25 changed files
with
10,936 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.ComponentModel; | ||
|
||
namespace MaterialSymbolsParser.Extensions; | ||
|
||
public static class EnumExtensions | ||
{ | ||
public static string GetDescription(this Enum value) | ||
{ | ||
var type = value.GetType(); | ||
|
||
var memberInfo = type.GetMember(value.ToString()); | ||
|
||
if (memberInfo is { Length: > 0 }) | ||
{ | ||
if (memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() is DescriptionAttribute attribute) | ||
{ | ||
return attribute.Description; | ||
} | ||
} | ||
|
||
return value.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using MaterialSymbolsParser.Extensions; | ||
using MaterialSymbolsParser.Model.Google; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace MaterialSymbolsParser.Generator; | ||
|
||
public abstract class GeneratorBase : IGenerator | ||
{ | ||
public abstract string Namespace { get; } | ||
|
||
public CompilationUnitSyntax GetCompilationUnitSyntax(KeyValuePair<string, IReadOnlyCollection<Icon>> group, string className, string familyPath) | ||
{ | ||
var usings = SyntaxFactory.List(new[] | ||
{ | ||
SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")) | ||
}); | ||
|
||
var namespaceDeclaration = SyntaxFactory | ||
.NamespaceDeclaration(SyntaxFactory.ParseName(Namespace)) | ||
.AddMembers(GenerateFamilyClass(group, className, familyPath)); | ||
|
||
var commentTrivia = AddComment(); | ||
|
||
return SyntaxFactory.CompilationUnit() | ||
.WithUsings(usings) | ||
.WithMembers(SyntaxFactory.SingletonList<MemberDeclarationSyntax>(namespaceDeclaration)) | ||
.WithLeadingTrivia(commentTrivia); | ||
} | ||
|
||
private static SyntaxTriviaList AddComment() | ||
{ | ||
var commentText = $"This file was auto-generated by MaterialSymbolsParser {DateTime.Now:yyyy/MM/dd_HH:mm:ss}"; | ||
return SyntaxFactory.TriviaList( | ||
SyntaxFactory.Comment($"/*\n * {commentText}\n */"), | ||
SyntaxFactory.CarriageReturnLineFeed, | ||
SyntaxFactory.CarriageReturnLineFeed | ||
); | ||
} | ||
|
||
/* | ||
Only if we want use nested classes like its currently done in MudBlazor, but we actually should use namespace and do a breaking changed in MudBlazor | ||
private static ClassDeclarationSyntax GenerateIconsClass(KeyValuePair<string, IReadOnlyCollection<Icon>> group, string className, string familyPath) | ||
{ | ||
return SyntaxFactory.ClassDeclaration("Icons") | ||
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PartialKeyword)) | ||
.AddMembers(GenerateMaterialSymbolsClass(group, className, familyPath)); | ||
} | ||
private static ClassDeclarationSyntax GenerateMaterialSymbolsClass(KeyValuePair<string, IReadOnlyCollection<Icon>> group, string className, string familyPath) | ||
{ | ||
return SyntaxFactory.ClassDeclaration("MaterialSymbols") | ||
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PartialKeyword)) | ||
.AddMembers(GenerateFamilyClass(group, className, familyPath)); | ||
} | ||
*/ | ||
|
||
private static ClassDeclarationSyntax GenerateFamilyClass(KeyValuePair<string, IReadOnlyCollection<Icon>> group, string className, string familyPath) | ||
{ | ||
return SyntaxFactory.ClassDeclaration(className) | ||
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PartialKeyword)) | ||
.AddMembers(group.Value.Select(icon => GenerateIconField(icon, familyPath)).Cast<MemberDeclarationSyntax>().ToArray()); | ||
} | ||
|
||
private static bool IsKeyword(string keyword) | ||
{ | ||
var syntaxKind = SyntaxFacts.GetKeywordKind(keyword); | ||
return SyntaxFacts.IsKeywordKind(syntaxKind); | ||
} | ||
|
||
private static FieldDeclarationSyntax GenerateIconField(Icon icon, string familyPath) | ||
{ | ||
var iconName = icon.Name.ConvertToCamelCase(); | ||
if (char.IsDigit(iconName[0])) | ||
{ | ||
iconName = $"_{iconName}"; | ||
} | ||
|
||
if (IsKeyword(iconName)) | ||
{ | ||
iconName = $"@{iconName}"; | ||
} | ||
|
||
return SyntaxFactory.FieldDeclaration( | ||
SyntaxFactory.VariableDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.StringKeyword))) | ||
.AddVariables( | ||
SyntaxFactory.VariableDeclarator(iconName) | ||
.WithInitializer(SyntaxFactory.EqualsValueClause( | ||
SyntaxFactory.LiteralExpression( | ||
SyntaxKind.StringLiteralExpression, | ||
SyntaxFactory.Literal($"{familyPath}/{icon.Name}") | ||
) | ||
)) | ||
) | ||
) | ||
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.ConstKeyword)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using MaterialSymbolsParser.Model; | ||
|
||
namespace MaterialSymbolsParser.Generator; | ||
|
||
public static class GeneratorFactory | ||
{ | ||
public static IGenerator Create(IconType iconType) | ||
{ | ||
return iconType switch | ||
{ | ||
IconType.MaterialIcons => new MaterialIconsGenerator(), | ||
IconType.MaterialSymbols => new MaterialSymbolsGenerator(), | ||
_ => throw new InvalidOperationException($"No generator found for icon type {iconType}!") | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using MaterialSymbolsParser.Model.Google; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace MaterialSymbolsParser.Generator; | ||
|
||
public interface IGenerator | ||
{ | ||
public string Namespace { get; } | ||
|
||
CompilationUnitSyntax GetCompilationUnitSyntax(KeyValuePair<string, IReadOnlyCollection<Icon>> group, string className, string familyPath); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/MaterialSymbolsParser/Generator/MaterialIconsGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MaterialSymbolsParser.Generator; | ||
|
||
public class MaterialIconsGenerator : GeneratorBase | ||
{ | ||
public override string Namespace => "MudBlazor.FontIcons.MaterialIcons"; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/MaterialSymbolsParser/Generator/MaterialSymbolsGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MaterialSymbolsParser.Generator; | ||
|
||
public class MaterialSymbolsGenerator : GeneratorBase | ||
{ | ||
public override string Namespace => "MudBlazor.FontIcons.MaterialSymbols"; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/MaterialSymbolsParser/Model/Icon.cs → ...aterialSymbolsParser/Model/Google/Icon.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/MaterialSymbolsParser/Model/Metadata.cs → ...ialSymbolsParser/Model/Google/Metadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...er/Model/MetadataJsonSerializerContext.cs → ...l/Google/MetadataJsonSerializerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MaterialSymbolsParser.Model; | ||
namespace MaterialSymbolsParser.Model.Google; | ||
|
||
[JsonSerializable(typeof(Metadata))] | ||
public partial class MetadataJsonSerializerContext : JsonSerializerContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
namespace MaterialSymbolsParser.Model; | ||
using System.ComponentModel; | ||
|
||
namespace MaterialSymbolsParser.Model; | ||
|
||
public enum IconType | ||
{ | ||
[Description("MaterialSymbols")] | ||
MaterialSymbols = 0, | ||
|
||
[Description("MaterialIcons")] | ||
MaterialIcons = 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.