diff --git a/src/FluentHub.Octicons.Generator/Program.cs b/src/FluentHub.Octicons.Generator/Program.cs index d1c6d698e..e5c18e08c 100644 --- a/src/FluentHub.Octicons.Generator/Program.cs +++ b/src/FluentHub.Octicons.Generator/Program.cs @@ -13,39 +13,19 @@ public class Program { static void Main(string[] args) { - args = args.Append("..\\..\\..\\..\\FluentHub.Octicons.Generator\\").ToArray(); + string basePath = AppDomain.CurrentDomain.BaseDirectory; + string assetsPath = Path.Combine(basePath, "..\\..\\..\\..\\FluentHub.Octicons.Generator\\Assets\\"); + string stylesOutputPath = Path.Combine(basePath, "..\\..\\..\\..\\FluentHub.App\\Styles\\"); + string enumsOutputPath = Path.Combine(basePath, "..\\..\\..\\..\\FluentHub.Core\\Data\\Enums\\"); - args = args.Append("..\\..\\..\\..\\FluentHub.App\\Styles\\").ToArray(); - - args = args.Append("..\\..\\..\\..\\FluentHub.Core\\Data\\Enums\\").ToArray(); - - if (args.Length != 3) + if (!Directory.Exists(assetsPath)) { - StringBuilder message = new(1024); - - message.AppendLine(); - message.AppendLine("Copyright (c) 2022-2024 0x5BFA"); - message.AppendLine("Licensed under the MIT License. See the LICENSE."); - message.AppendLine(); - message.AppendLine("FluentHub Octicons Generator"); - message.AppendLine("--------------------------------------------------"); - message.AppendLine(); - message.AppendLine("An error occurred in parsing command line arguments."); - message.AppendLine(); - message.AppendLine("Syntax:"); - message.AppendLine(" filename.exe [project folder path]"); - message.AppendLine(); - - Console.Write(message.ToString()); - - message.Clear(); - + Console.WriteLine($"Assets directory not found. ({assetsPath})"); return; } - GenerateCSharpEnumEntities(args[0], args[2]); - - GenerateXamlEntities(args[0], args[1]); + GenerateCSharpEnumEntities(assetsPath, enumsOutputPath); + GenerateXamlEntities(assetsPath, stylesOutputPath); } private static void GenerateXamlEntities(string path, string output) @@ -63,12 +43,11 @@ private static void GenerateXamlEntities(string path, string output) xamlTemplateBegining.AppendLine(@$" "); xamlTemplateBegining.AppendLine(@$" "); - // Delete all existing C# source files - foreach (var csFile in Directory.EnumerateFiles(path + "Assets", "*.svg")) + foreach (var svgFile in Directory.EnumerateFiles(path, "*.svg")) { - Console.WriteLine($"Converting {csFile.Split('\\').Last()[..^4]} into ColorIcon{NormalizeFileName(csFile)}"); + Console.WriteLine($"Converting {Path.GetFileNameWithoutExtension(svgFile)} into ColorIcon{NormalizeFileName(svgFile)}"); - var convertedStr = ConvertSvgEntity(csFile); + var convertedStr = ConvertSvgEntity(svgFile); xamlTemplateBegining.Append(convertedStr); } @@ -80,13 +59,14 @@ private static void GenerateXamlEntities(string path, string output) var value = xamlTemplateBegining.ToString(); + Directory.CreateDirectory(output); File.WriteAllText(Path.Combine(output, "OcticonIcons.xaml"), value); } private static void GenerateCSharpEnumEntities(string path, string output) { StringBuilder stringBuilder = new(); - stringBuilder.AppendLine(@$"// Copyright (c) 0x5BFA"); + stringBuilder.AppendLine(@$"// Copyright (c) 0x5BFA"); stringBuilder.AppendLine(@$"// Licensed under the MIT License. See the LICENSE."); stringBuilder.AppendLine(@$""); stringBuilder.AppendLine(@$"//------------------------------------------------------------------------------"); @@ -103,9 +83,9 @@ private static void GenerateCSharpEnumEntities(string path, string output) stringBuilder.AppendLine(@$" public enum OcticonKind"); stringBuilder.AppendLine(@$" {{"); - foreach (var csFile in Directory.EnumerateFiles(path + "Assets", "*.svg")) + foreach (var svgFile in Directory.EnumerateFiles(path, "*.svg")) { - stringBuilder.AppendLine(@$" Octicon{NormalizeFileName(csFile)},"); + stringBuilder.AppendLine(@$" Octicon{NormalizeFileName(svgFile)},"); } stringBuilder.AppendLine(@$" }}"); @@ -113,6 +93,7 @@ private static void GenerateCSharpEnumEntities(string path, string output) var value = stringBuilder.ToString(); + Directory.CreateDirectory(output); File.WriteAllText(Path.Combine(output, "OcticonKind.cs"), value); } @@ -121,7 +102,28 @@ private static string ConvertSvgEntity(string path) XmlDocument doc = new(); doc.Load(path); - var svgRaw = doc.FirstChild.FirstChild.Attributes.Item(0).InnerText; + var svgElement = doc.DocumentElement; + if (svgElement == null) + { + throw new InvalidOperationException($"Invalid SVG file: {path}"); + } + + var pathElements = svgElement.GetElementsByTagName("path"); + if (pathElements.Count == 0) + { + throw new InvalidOperationException($"No path elements found in SVG file: {path}"); + } + + StringBuilder pathDataBuilder = new(); + foreach (XmlNode pathElement in pathElements) + { + var dAttribute = pathElement.Attributes["d"]; + if (dAttribute == null) + { + throw new InvalidOperationException($"No 'd' attribute found in path element of SVG file: {path}"); + } + pathDataBuilder.AppendLine(@$" "); + } var normalizedFileName = NormalizeFileName(path); var iconSize = GetIconSize(normalizedFileName); @@ -138,10 +140,7 @@ private static string ConvertSvgEntity(string path) svgXamlTemplate.AppendLine(@$" Height=""{iconSize}"""); svgXamlTemplate.AppendLine(@$" Stretch=""Uniform"">"); svgXamlTemplate.AppendLine(@$" "); - svgXamlTemplate.AppendLine(@$" "); + svgXamlTemplate.Append(pathDataBuilder.ToString()); svgXamlTemplate.AppendLine(@$" "); svgXamlTemplate.AppendLine(@$" "); svgXamlTemplate.AppendLine(@$" "); @@ -154,16 +153,19 @@ private static string ConvertSvgEntity(string path) private static string NormalizeFileName(string path) { - string fileName = path.Split('\\').Last(); - - string str = fileName[..^4].Replace('-', '_'); - + string fileName = Path.GetFileNameWithoutExtension(path); + string str = fileName.Replace('-', '_'); return str.Pascalize(); } private static int GetIconSize(string fileName) { - return Convert.ToInt32(fileName.Substring(fileName.Length - 2)); + var parts = fileName.Split('-'); + if (parts.Length > 1 && int.TryParse(parts.Last(), out int size)) + { + return size; + } + return 16; // Default size if not specified } } }