-
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.
- Loading branch information
1 parent
4488839
commit 862fc7e
Showing
5 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
Binary file not shown.
96 changes: 96 additions & 0 deletions
96
src/DotNetElements.Extensions.Icons/CodiconsFontGenerator.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,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); | ||
} |
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
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