Skip to content

Commit 03046a2

Browse files
Colin MackayColin Mackay
authored andcommitted
WIP
1 parent 4d809cc commit 03046a2

File tree

4 files changed

+9170
-26
lines changed

4 files changed

+9170
-26
lines changed

helpers/PhosphorIcons/ConvertToEnum.ps1

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22
$iconPath = "$PSScriptRoot/../../src/Stravaig.Avalonia.Controls.Icons/Assets/PhosphorIcons"
33

44
# Output C# file path
5-
$outputPath = Join-Path $PSScriptRoot "../../src/Stravaig.Avalonia.Controls.Icons/PhosphorIcons" | Convert-Path
6-
$outputPath += "/PhosphorIconName.cs"
5+
$outputPath = Join-Path $PSScriptRoot "../../src/Stravaig.Avalonia.Controls.Icons/PhosphorIcon" | Convert-Path
6+
$enumOutputPath = $outputPath + "/PhosphorIconName.cs"
7+
$dictionaryOutputPath = $outputPath + "/PhosphorIconEnumToResourceMap-Dictionary.cs"
78

89
# Read and parse JSON
910
$svgFiles = Get-ChildItem -Path $iconPath -Filter "*.svg" -Recurse -File
1011

1112
$iconData = $svgFiles | ForEach-Object {
1213
$fileName = $_.BaseName
13-
if ($fileName -match '^(.+)-([^-]+)$') {
14+
$directory = $_.Directory.Name
15+
16+
if ($directory -eq 'regular') {
17+
[PSCustomObject]@{
18+
BaseName = $fileName
19+
Style = 'regular'
20+
}
21+
}
22+
elseif ($fileName -match '^(.+)-([^-]+)$') {
1423
[PSCustomObject]@{
1524
BaseName = $matches[1]
1625
Style = $matches[2]
@@ -23,17 +32,6 @@ $iconData = $svgFiles | ForEach-Object {
2332
}
2433
}
2534

26-
# $json = @{
27-
# icons = $iconData | ForEach-Object {
28-
# @{
29-
# properties = @{
30-
# name = $_.Name
31-
# styles = $_.Styles
32-
# }
33-
# }
34-
# }
35-
# }
36-
3735
# Extract icon name and code, format as required
3836
$entries = foreach ($icon in $iconData) {
3937
$rawName = $icon.Name;
@@ -53,27 +51,50 @@ $entries = foreach ($icon in $iconData) {
5351
}
5452

5553
# Generate C# dictionary entries
56-
$dictEntries = $entries | Sort-Object -Property Name | ForEach-Object {
54+
$enumEntries = $entries | Sort-Object -Property Name | ForEach-Object {
5755
" /// <Summary>`n" +
58-
" /// Phosphor icon '$($_.RawName)', available in styles $($_.Styles).`n" +
56+
" /// Phosphor icon '$($_.RawName)', available in $($_.Styles).`n" +
5957
" /// </Summary>`n" +
60-
" $($_.Name) = $($_.Value),`n"
58+
" $($_.Name),`n"
6159
}
6260

63-
# Write C# file
61+
$dictEntries = $entries | ForEach-Object {
62+
" { PhosphorIconName.$($_.Name), `"$($_.RawName)`" },"
63+
}
64+
65+
# Write C# file for enum
6466
@"
6567
// Auto-generated file. Do not modify directly.
66-
// Use helpers/Phosphor-Icons/ConvertToEnum.ps1 to regenerate.
67-
68-
// Some icon codes have multiple names, leading to duplicate enum values which triggers CA1069 warning.
69-
#pragma warning disable CA1069
68+
// Use helpers/PhosphorIcons/ConvertToEnum.ps1 to regenerate.
7069
71-
namespace Stravaig.Controls.Icons;
70+
namespace Stravaig.Avalonia.Controls.Icons;
7271
7372
public enum PhosphorIconName
7473
{
75-
$($dictEntries -join "`n")
74+
$($enumEntries -join "`n")
7675
}
77-
"@ | Set-Content $outputPath
76+
"@ | Set-Content $enumOutputPath
77+
78+
Write-Host "C# file generated at $enumOutputPath"
7879

79-
Write-Host "C# file generated at $outputPath"
80+
# Write C# file for the helper class to convert the enum to the resource name.
81+
82+
@"
83+
// Auto-generated file. Do not modify directly.
84+
// Use helpers/PhosphorIcons/ConvertToEnum.ps1 to regenerate.
85+
86+
using System.Collections.Frozen;
87+
using System.Collections.Generic;
88+
89+
namespace Stravaig.Avalonia.Controls.Icons;
90+
91+
internal static partial class PhosphorIconEnumToResourceMap
92+
{
93+
private static readonly FrozenDictionary<PhosphorIconName, string> IconMap =
94+
new Dictionary<PhosphorIconName, string>
95+
{
96+
$($dictEntries -join "`n")
97+
}.ToFrozenDictionary();
98+
}
99+
"@ | Set-Content $dictionaryOutputPath
100+
Write-Host "C# file generated at $enumOutputPath"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Stravaig.Avalonia.Controls.Icons;
2+
3+
internal static partial class PhosphorIconEnumToResourceMap
4+
{
5+
public static string? GetResourceName(PhosphorIconName name, PhosphorIconType type)
6+
{
7+
if (IconMap.TryGetValue(name, out var resourceNameFragment))
8+
{
9+
var pathFragment = type.ToString().ToLowerInvariant();
10+
var resourceFile = resourceNameFragment +
11+
(type == PhosphorIconType.Regular
12+
? string.Empty
13+
: $"-{pathFragment}");
14+
var fullResourcePath =
15+
$"avares://Stravaig.Avalonia.Controls.Icons/Assets/PhosphorIcons/{pathFragment}/{resourceFile}.svg";
16+
return fullResourcePath;
17+
}
18+
19+
// Should never happen as the Enum and Dictionary are generated from the same source.
20+
return null;
21+
}
22+
}

0 commit comments

Comments
 (0)