@@ -13,39 +13,19 @@ public class Program
13
13
{
14
14
static void Main ( string [ ] args )
15
15
{
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\\ " ) ;
17
20
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 ) )
23
22
{
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 } )") ;
43
24
return ;
44
25
}
45
26
46
- GenerateCSharpEnumEntities ( args [ 0 ] , args [ 2 ] ) ;
47
-
48
- GenerateXamlEntities ( args [ 0 ] , args [ 1 ] ) ;
27
+ GenerateCSharpEnumEntities ( assetsPath , enumsOutputPath ) ;
28
+ GenerateXamlEntities ( assetsPath , stylesOutputPath ) ;
49
29
}
50
30
51
31
private static void GenerateXamlEntities ( string path , string output )
@@ -63,12 +43,11 @@ private static void GenerateXamlEntities(string path, string output)
63
43
xamlTemplateBegining . AppendLine ( @$ " <ResourceDictionary.MergedDictionaries>") ;
64
44
xamlTemplateBegining . AppendLine ( @$ " <ResourceDictionary>") ;
65
45
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" ) )
68
47
{
69
- Console . WriteLine ( $ "Converting { csFile . Split ( ' \\ ' ) . Last ( ) [ .. ^ 4 ] } into ColorIcon{ NormalizeFileName ( csFile ) } ") ;
48
+ Console . WriteLine ( $ "Converting { Path . GetFileNameWithoutExtension ( svgFile ) } into ColorIcon{ NormalizeFileName ( svgFile ) } ") ;
70
49
71
- var convertedStr = ConvertSvgEntity ( csFile ) ;
50
+ var convertedStr = ConvertSvgEntity ( svgFile ) ;
72
51
73
52
xamlTemplateBegining . Append ( convertedStr ) ;
74
53
}
@@ -80,13 +59,14 @@ private static void GenerateXamlEntities(string path, string output)
80
59
81
60
var value = xamlTemplateBegining . ToString ( ) ;
82
61
62
+ Directory . CreateDirectory ( output ) ;
83
63
File . WriteAllText ( Path . Combine ( output , "OcticonIcons.xaml" ) , value ) ;
84
64
}
85
65
86
66
private static void GenerateCSharpEnumEntities ( string path , string output )
87
67
{
88
68
StringBuilder stringBuilder = new ( ) ;
89
- stringBuilder . AppendLine ( @$ " // Copyright (c) 0x5BFA") ;
69
+ stringBuilder . AppendLine ( @$ "// Copyright (c) 0x5BFA") ;
90
70
stringBuilder . AppendLine ( @$ "// Licensed under the MIT License. See the LICENSE.") ;
91
71
stringBuilder . AppendLine ( @$ "") ;
92
72
stringBuilder . AppendLine ( @$ "//------------------------------------------------------------------------------") ;
@@ -103,16 +83,17 @@ private static void GenerateCSharpEnumEntities(string path, string output)
103
83
stringBuilder . AppendLine ( @$ " public enum OcticonKind") ;
104
84
stringBuilder . AppendLine ( @$ " {{") ;
105
85
106
- foreach ( var csFile in Directory . EnumerateFiles ( path + "Assets" , "*.svg" ) )
86
+ foreach ( var svgFile in Directory . EnumerateFiles ( path , "*.svg" ) )
107
87
{
108
- stringBuilder . AppendLine ( @$ " Octicon{ NormalizeFileName ( csFile ) } ,") ;
88
+ stringBuilder . AppendLine ( @$ " Octicon{ NormalizeFileName ( svgFile ) } ,") ;
109
89
}
110
90
111
91
stringBuilder . AppendLine ( @$ " }}") ;
112
92
stringBuilder . AppendLine ( @$ "}}") ;
113
93
114
94
var value = stringBuilder . ToString ( ) ;
115
95
96
+ Directory . CreateDirectory ( output ) ;
116
97
File . WriteAllText ( Path . Combine ( output , "OcticonKind.cs" ) , value ) ;
117
98
}
118
99
@@ -121,7 +102,28 @@ private static string ConvertSvgEntity(string path)
121
102
XmlDocument doc = new ( ) ;
122
103
doc . Load ( path ) ;
123
104
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
+ }
125
127
126
128
var normalizedFileName = NormalizeFileName ( path ) ;
127
129
var iconSize = GetIconSize ( normalizedFileName ) ;
@@ -138,10 +140,7 @@ private static string ConvertSvgEntity(string path)
138
140
svgXamlTemplate.AppendLine(@$" Height = "" { iconSize } """);
139
141
svgXamlTemplate.AppendLine(@$" Stretch=""Uniform"">");
140
142
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());
145
144
svgXamlTemplate.AppendLine(@$" </Canvas>");
146
145
svgXamlTemplate.AppendLine(@$" </Viewbox>");
147
146
svgXamlTemplate.AppendLine(@$" </ControlTemplate>");
@@ -154,16 +153,19 @@ private static string ConvertSvgEntity(string path)
154
153
155
154
private static string NormalizeFileName(string path)
156
155
{
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('-', '_');
161
158
return str.Pascalize();
162
159
}
163
160
164
161
private static int GetIconSize(string fileName)
165
162
{
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
167
169
}
168
170
}
169
171
}
0 commit comments