Skip to content

Commit

Permalink
Prepare for Material Icons font pack as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletKuro committed May 29, 2024
1 parent 6f27881 commit ca18af8
Show file tree
Hide file tree
Showing 25 changed files with 10,936 additions and 110 deletions.
10 changes: 8 additions & 2 deletions MudBlazor.IconPack.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaterialSymbolsParser", "sr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{62C839DE-403F-4001-AF1A-32DCCB75DB7F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MudBlazor.FontIcons.MaterialIcons", "src\MudBlazor.FontIcons.MaterialIcons\MudBlazor.FontIcons.MaterialIcons.csproj", "{AD04F39B-1A24-4B64-BF44-0E649168B85D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{62C839DE-403F-4001-AF1A-32DCCB75DB7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{62C839DE-403F-4001-AF1A-32DCCB75DB7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57334B6A-1E52-4571-B01A-8986A04877C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57334B6A-1E52-4571-B01A-8986A04877C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57334B6A-1E52-4571-B01A-8986A04877C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -25,6 +25,12 @@ Global
{451E5BC8-C1D9-46DA-8151-B3A204300562}.Debug|Any CPU.Build.0 = Debug|Any CPU
{451E5BC8-C1D9-46DA-8151-B3A204300562}.Release|Any CPU.ActiveCfg = Release|Any CPU
{451E5BC8-C1D9-46DA-8151-B3A204300562}.Release|Any CPU.Build.0 = Release|Any CPU
{62C839DE-403F-4001-AF1A-32DCCB75DB7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{62C839DE-403F-4001-AF1A-32DCCB75DB7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD04F39B-1A24-4B64-BF44-0E649168B85D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD04F39B-1A24-4B64-BF44-0E649168B85D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD04F39B-1A24-4B64-BF44-0E649168B85D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD04F39B-1A24-4B64-BF44-0E649168B85D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
23 changes: 23 additions & 0 deletions src/MaterialSymbolsParser/Extensions/EnumExtensions.cs
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();
}
}
7 changes: 7 additions & 0 deletions src/MaterialSymbolsParser/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ public static string ConvertToCamelCase(this string input)

return string.Join(string.Empty, words);
}

public static string RemoveWhitespace(this string input)
{
return new string(input.ToCharArray()
.Where(c => !char.IsWhiteSpace(c))
.ToArray());
}
}
100 changes: 100 additions & 0 deletions src/MaterialSymbolsParser/Generator/GeneratorBase.cs
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));
}
}
16 changes: 16 additions & 0 deletions src/MaterialSymbolsParser/Generator/GeneratorFactory.cs
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}!")
};
}
}
11 changes: 11 additions & 0 deletions src/MaterialSymbolsParser/Generator/IGenerator.cs
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 src/MaterialSymbolsParser/Generator/MaterialIconsGenerator.cs
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";
}
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";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;

namespace MaterialSymbolsParser.Model;
namespace MaterialSymbolsParser.Model.Google;

public class Icon
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;

namespace MaterialSymbolsParser.Model;
namespace MaterialSymbolsParser.Model.Google;

public class Metadata
{
Expand Down
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;
7 changes: 6 additions & 1 deletion src/MaterialSymbolsParser/Model/IconType.cs
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
}
10 changes: 6 additions & 4 deletions src/MaterialSymbolsParser/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MaterialSymbolsParser.Model;
using MaterialSymbolsParser.Extensions;
using MaterialSymbolsParser.Model;
using MaterialSymbolsParser.Service;

namespace MaterialSymbolsParser;
Expand All @@ -7,12 +8,13 @@ public static class Program
{
public static async Task Main(string[] args)
{
var iconType = IconType.MaterialIcons;
var codeGenerator = new CodeGenerationService();
using var client = new IconHttpClientService();
var metadata = await client.ParseIconsAsync().ConfigureAwait(false);
var filteredIcons = Utility.IconFilter.FilterByFamily(metadata, IconType.MaterialSymbols);
var groupedIcons = Utility.IconFilter.GroupIconsByFamilies(filteredIcons, IconType.MaterialSymbols);
var filteredIcons = Utility.IconFilter.FilterByFamily(metadata, iconType);
var groupedIcons = Utility.IconFilter.GroupIconsByFamilies(filteredIcons, iconType);

codeGenerator.GenerateCsFilesUsingRoslyn(groupedIcons, folder: "../../../../MudBlazor.Icons.MaterialSymbols");
codeGenerator.GenerateCsFilesUsingRoslyn(iconType, groupedIcons, folder: $"../../../../MudBlazor.FontIcons.{iconType.GetDescription()}");
}
}
Loading

0 comments on commit ca18af8

Please sign in to comment.