Skip to content

Commit 1983725

Browse files
committed
✨ Update Octicons generator script
1 parent 08303c6 commit 1983725

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

src/FluentHub.Octicons.Generator/Program.cs

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,19 @@ public class Program
1313
{
1414
static void Main(string[] args)
1515
{
16-
args = args.Append("..\\..\\..\\..\\FluentHub.Octicons.Generator\\").ToArray();
16+
string basePath = AppDomain.CurrentDomain.BaseDirectory;
17+
string assetsPath = Path.Combine(basePath, "..\\..\\..\\..\\FluentHub.Octicons.Generator\\Assets\\");
18+
string stylesOutputPath = Path.Combine(basePath, "..\\..\\..\\..\\FluentHub.App\\Styles\\");
19+
string enumsOutputPath = Path.Combine(basePath, "..\\..\\..\\..\\FluentHub.Core\\Data\\Enums\\");
1720

18-
args = args.Append("..\\..\\..\\..\\FluentHub.App\\Styles\\").ToArray();
19-
20-
args = args.Append("..\\..\\..\\..\\FluentHub.Core\\Data\\Enums\\").ToArray();
21-
22-
if (args.Length != 3)
21+
if (!Directory.Exists(assetsPath))
2322
{
24-
StringBuilder message = new(1024);
25-
26-
message.AppendLine();
27-
message.AppendLine("Copyright (c) 2022-2024 0x5BFA");
28-
message.AppendLine("Licensed under the MIT License. See the LICENSE.");
29-
message.AppendLine();
30-
message.AppendLine("FluentHub Octicons Generator");
31-
message.AppendLine("--------------------------------------------------");
32-
message.AppendLine();
33-
message.AppendLine("An error occurred in parsing command line arguments.");
34-
message.AppendLine();
35-
message.AppendLine("Syntax:");
36-
message.AppendLine(" filename.exe [project folder path]");
37-
message.AppendLine();
38-
39-
Console.Write(message.ToString());
40-
41-
message.Clear();
42-
23+
Console.WriteLine($"Assets directory not found. ({assetsPath})");
4324
return;
4425
}
4526

46-
GenerateCSharpEnumEntities(args[0], args[2]);
47-
48-
GenerateXamlEntities(args[0], args[1]);
27+
GenerateCSharpEnumEntities(assetsPath, enumsOutputPath);
28+
GenerateXamlEntities(assetsPath, stylesOutputPath);
4929
}
5030

5131
private static void GenerateXamlEntities(string path, string output)
@@ -63,12 +43,11 @@ private static void GenerateXamlEntities(string path, string output)
6343
xamlTemplateBegining.AppendLine(@$" <ResourceDictionary.MergedDictionaries>");
6444
xamlTemplateBegining.AppendLine(@$" <ResourceDictionary>");
6545

66-
// Delete all existing C# source files
67-
foreach (var csFile in Directory.EnumerateFiles(path + "Assets", "*.svg"))
46+
foreach (var svgFile in Directory.EnumerateFiles(path, "*.svg"))
6847
{
69-
Console.WriteLine($"Converting {csFile.Split('\\').Last()[..^4]} into ColorIcon{NormalizeFileName(csFile)}");
48+
Console.WriteLine($"Converting {Path.GetFileNameWithoutExtension(svgFile)} into ColorIcon{NormalizeFileName(svgFile)}");
7049

71-
var convertedStr = ConvertSvgEntity(csFile);
50+
var convertedStr = ConvertSvgEntity(svgFile);
7251

7352
xamlTemplateBegining.Append(convertedStr);
7453
}
@@ -80,13 +59,14 @@ private static void GenerateXamlEntities(string path, string output)
8059

8160
var value = xamlTemplateBegining.ToString();
8261

62+
Directory.CreateDirectory(output);
8363
File.WriteAllText(Path.Combine(output, "OcticonIcons.xaml"), value);
8464
}
8565

8666
private static void GenerateCSharpEnumEntities(string path, string output)
8767
{
8868
StringBuilder stringBuilder = new();
89-
stringBuilder.AppendLine(@$"// Copyright (c) 0x5BFA");
69+
stringBuilder.AppendLine(@$"// Copyright (c) 0x5BFA");
9070
stringBuilder.AppendLine(@$"// Licensed under the MIT License. See the LICENSE.");
9171
stringBuilder.AppendLine(@$"");
9272
stringBuilder.AppendLine(@$"//------------------------------------------------------------------------------");
@@ -103,16 +83,17 @@ private static void GenerateCSharpEnumEntities(string path, string output)
10383
stringBuilder.AppendLine(@$" public enum OcticonKind");
10484
stringBuilder.AppendLine(@$" {{");
10585

106-
foreach (var csFile in Directory.EnumerateFiles(path + "Assets", "*.svg"))
86+
foreach (var svgFile in Directory.EnumerateFiles(path, "*.svg"))
10787
{
108-
stringBuilder.AppendLine(@$" Octicon{NormalizeFileName(csFile)},");
88+
stringBuilder.AppendLine(@$" Octicon{NormalizeFileName(svgFile)},");
10989
}
11090

11191
stringBuilder.AppendLine(@$" }}");
11292
stringBuilder.AppendLine(@$"}}");
11393

11494
var value = stringBuilder.ToString();
11595

96+
Directory.CreateDirectory(output);
11697
File.WriteAllText(Path.Combine(output, "OcticonKind.cs"), value);
11798
}
11899

@@ -121,7 +102,28 @@ private static string ConvertSvgEntity(string path)
121102
XmlDocument doc = new();
122103
doc.Load(path);
123104

124-
var svgRaw = doc.FirstChild.FirstChild.Attributes.Item(0).InnerText;
105+
var svgElement = doc.DocumentElement;
106+
if (svgElement == null)
107+
{
108+
throw new InvalidOperationException($"Invalid SVG file: {path}");
109+
}
110+
111+
var pathElements = svgElement.GetElementsByTagName("path");
112+
if (pathElements.Count == 0)
113+
{
114+
throw new InvalidOperationException($"No path elements found in SVG file: {path}");
115+
}
116+
117+
StringBuilder pathDataBuilder = new();
118+
foreach (XmlNode pathElement in pathElements)
119+
{
120+
var dAttribute = pathElement.Attributes["d"];
121+
if (dAttribute == null)
122+
{
123+
throw new InvalidOperationException($"No 'd' attribute found in path element of SVG file: {path}");
124+
}
125+
pathDataBuilder.AppendLine(@$" <Path Data=""{dAttribute.Value}"" Fill=""{{TemplateBinding Foreground}}"" />");
126+
}
125127

126128
var normalizedFileName = NormalizeFileName(path);
127129
var iconSize = GetIconSize(normalizedFileName);
@@ -138,10 +140,7 @@ private static string ConvertSvgEntity(string path)
138140
svgXamlTemplate.AppendLine(@$" Height=""{iconSize}""");
139141
svgXamlTemplate.AppendLine(@$" Stretch=""Uniform"">");
140142
svgXamlTemplate.AppendLine(@$" <Canvas Width=""{iconSize}"" Height=""{iconSize}"">");
141-
svgXamlTemplate.AppendLine(@$" <Path");
142-
svgXamlTemplate.AppendLine(@$" x:Name=""Path1""");
143-
svgXamlTemplate.AppendLine(@$" Data=""{svgRaw}""");
144-
svgXamlTemplate.AppendLine(@$" Fill=""{{TemplateBinding Foreground}}"" />");
143+
svgXamlTemplate.Append(pathDataBuilder.ToString());
145144
svgXamlTemplate.AppendLine(@$" </Canvas>");
146145
svgXamlTemplate.AppendLine(@$" </Viewbox>");
147146
svgXamlTemplate.AppendLine(@$" </ControlTemplate>");
@@ -154,16 +153,19 @@ private static string ConvertSvgEntity(string path)
154153

155154
private static string NormalizeFileName(string path)
156155
{
157-
string fileName = path.Split('\\').Last();
158-
159-
string str = fileName[..^4].Replace('-', '_');
160-
156+
string fileName = Path.GetFileNameWithoutExtension(path);
157+
string str = fileName.Replace('-', '_');
161158
return str.Pascalize();
162159
}
163160

164161
private static int GetIconSize(string fileName)
165162
{
166-
return Convert.ToInt32(fileName.Substring(fileName.Length - 2));
163+
var parts = fileName.Split('-');
164+
if (parts.Length > 1 && int.TryParse(parts.Last(), out int size))
165+
{
166+
return size;
167+
}
168+
return 16; // Default size if not specified
167169
}
168170
}
169171
}

0 commit comments

Comments
 (0)