Skip to content

Commit

Permalink
✨ Update Octicons generator script
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamparter committed Sep 22, 2024
1 parent 08303c6 commit 1983725
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions src/FluentHub.Octicons.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -63,12 +43,11 @@ private static void GenerateXamlEntities(string path, string output)
xamlTemplateBegining.AppendLine(@$" <ResourceDictionary.MergedDictionaries>");
xamlTemplateBegining.AppendLine(@$" <ResourceDictionary>");

// 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);
}
Expand All @@ -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(@$"//------------------------------------------------------------------------------");
Expand All @@ -103,16 +83,17 @@ 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(@$" }}");
stringBuilder.AppendLine(@$"}}");

var value = stringBuilder.ToString();

Directory.CreateDirectory(output);
File.WriteAllText(Path.Combine(output, "OcticonKind.cs"), value);
}

Expand All @@ -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(@$" <Path Data=""{dAttribute.Value}"" Fill=""{{TemplateBinding Foreground}}"" />");
}

var normalizedFileName = NormalizeFileName(path);
var iconSize = GetIconSize(normalizedFileName);
Expand All @@ -138,10 +140,7 @@ private static string ConvertSvgEntity(string path)
svgXamlTemplate.AppendLine(@$" Height=""{iconSize}""");
svgXamlTemplate.AppendLine(@$" Stretch=""Uniform"">");
svgXamlTemplate.AppendLine(@$" <Canvas Width=""{iconSize}"" Height=""{iconSize}"">");
svgXamlTemplate.AppendLine(@$" <Path");
svgXamlTemplate.AppendLine(@$" x:Name=""Path1""");
svgXamlTemplate.AppendLine(@$" Data=""{svgRaw}""");
svgXamlTemplate.AppendLine(@$" Fill=""{{TemplateBinding Foreground}}"" />");
svgXamlTemplate.Append(pathDataBuilder.ToString());

Check notice on line 143 in src/FluentHub.Octicons.Generator/Program.cs

View check run for this annotation

codefactor.io / CodeFactor

src/FluentHub.Octicons.Generator/Program.cs#L143

Remove the ToString call in order to use a strongly-typed StringBuilder overload. (CA1830)
svgXamlTemplate.AppendLine(@$" </Canvas>");
svgXamlTemplate.AppendLine(@$" </Viewbox>");
svgXamlTemplate.AppendLine(@$" </ControlTemplate>");
Expand All @@ -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
}
}
}

0 comments on commit 1983725

Please sign in to comment.