Skip to content

Commit

Permalink
Updated font icon generators.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix-CodingClimber committed Jun 30, 2024
1 parent 4488839 commit 862fc7e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
Binary file not shown.
96 changes: 96 additions & 0 deletions src/DotNetElements.Extensions.Icons/CodiconsFontGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;
using System.Text;

namespace DotNetElements.Extensions.Icons;

internal partial class CodiconsFontGenerator
{
private readonly HttpClient httpClient;
private readonly ILogger<CodiconsFontGenerator> logger;

public CodiconsFontGenerator(HttpClient httpClient, ILogger<CodiconsFontGenerator> logger)
{
this.httpClient = httpClient;
this.logger = logger;
}

public async Task Run()
{
IReadOnlyList<Codicon>? iconInfo = await GetIconInfoAsync();

if (iconInfo is null)
return;

await WriteToFileAsync(iconInfo);

logger.LogInformation("Generated Codicon icons");
}

private async Task<IReadOnlyList<Codicon>?> GetIconInfoAsync()
{
Dictionary<string, int>? codepointInfo = await httpClient.GetFromJsonAsync<Dictionary<string, int>>("https://raw.githubusercontent.com/microsoft/vscode-codicons/main/src/template/mapping.json");

if (codepointInfo is null)
{
logger.LogError("Failed to get available icons from Github! (Failed to fetch codepoint info.)");
return null;
}

List<Codicon> iconSet = [];

foreach ((string name, int unicode) in codepointInfo)
{
iconSet.Add(new Codicon(name, unicode.ToString()));
}

return iconSet;
}

private static async Task WriteToFileAsync(IReadOnlyList<Codicon> iconInfo)
{
StringBuilder resultBuilder = new();
resultBuilder.AppendLine(fileHeader);

StringBuilder iconBuilder = new();

foreach (Codicon icon in iconInfo)
{
string varName = icon.Id!.ConvertDashToPascalCase();

iconBuilder.AppendLine($" {varName} = {$"{icon.Unicode}"},");
}

resultBuilder.Append(iconBuilder);
resultBuilder.Append(fileFooter);

await File.WriteAllTextAsync("CodiconIcons.cs", resultBuilder.ToString());
}

private const string fileHeader =
"""
//----------------------
// <auto-generated>
// Generated by the DotNetElements.Extensions.Icons CodiconsFontGenerator. DO NOT EDIT!
// source: CodiconsFontGenerator.cs
// </auto-generated>
//----------------------
namespace DotNetElements.Extensions.Icons;
public static partial class Icons
{
public enum Codicon
{
None = 0,
""";

private const string fileFooter =
"""
}
}
""";

private record Codicon(string Id, string Unicode);
}
8 changes: 6 additions & 2 deletions src/DotNetElements.Extensions.Icons/ConsoleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ internal sealed class ConsoleApplication : IHostedService
private readonly MaterialIconsSvgGenerator materialIconsSvgGenerator;

private readonly MaterialIconsFontGenerator materialIconsFontGenerator;
private readonly CodiconsFontGenerator codiconsFontGenerator;

public ConsoleApplication(
ILogger<ConsoleApplication> logger,
IHostApplicationLifetime appLifetime,
FontAwesomeSvgGenerator fontAwesomeSvgGenerator,
MaterialIconsSvgGenerator materialIconsSvgGenerator,
MaterialIconsFontGenerator materialIconsFontGenerator)
MaterialIconsFontGenerator materialIconsFontGenerator,
CodiconsFontGenerator codiconsFontGenerator)
{
this.logger = logger;
this.fontAwesomeSvgGenerator = fontAwesomeSvgGenerator;
this.materialIconsSvgGenerator = materialIconsSvgGenerator;
this.materialIconsFontGenerator = materialIconsFontGenerator;
this.codiconsFontGenerator = codiconsFontGenerator;

appLifetime.ApplicationStarted.Register(OnStarted);
appLifetime.ApplicationStopping.Register(OnStopping);
Expand All @@ -46,7 +49,8 @@ private async void OnStarted()
{
logger.LogInformation("Application started.");

await materialIconsFontGenerator.Run();
await codiconsFontGenerator.Run();
//await materialIconsFontGenerator.Run();
//await fontAwesomeSvgGenerator.Run();
//await materialIconsSvgGenerator.Run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static async Task WriteToFileAsync(IReadOnlyList<MaterialIcon> iconInfo)
{
string varName = icon.Id!.ConvertSnakeToPascalCase();

iconBuilder.AppendLine($" {varName} = {$"0x{icon.Unicode}"},");
iconBuilder.AppendLine($" {varName} = {$"0x{icon.Unicode}"},");
}

resultBuilder.Append(iconBuilder);
Expand All @@ -92,7 +92,7 @@ public static partial class Icons
{
public enum Material
{
None = 0,
None = 0,
""";

private const string fileFooter =
Expand All @@ -102,7 +102,5 @@ public enum Material
""";

private record MaterialIconFontInfo(string Name, string UnicodeUrl);

private record MaterialIcon(string Id, string Unicode);
}
4 changes: 4 additions & 0 deletions src/DotNetElements.Extensions.Icons/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ static IHostBuilder CreateHostBuilder()
{
options.DefaultRequestHeaders.Add("User-Agent", "request");
});
services.AddHttpClient<CodiconsFontGenerator>(options =>
{
options.DefaultRequestHeaders.Add("User-Agent", "request");
});
});
}

0 comments on commit 862fc7e

Please # to comment.