-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathImportSvgIcons.cs
More file actions
executable file
·141 lines (134 loc) · 4.52 KB
/
Copy pathImportSvgIcons.cs
File metadata and controls
executable file
·141 lines (134 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env dotnet run
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
// Constants.
const string IconsFilePath = "PixelViewer/Styles/Icons.axaml";
const string IconPathInSvgPattern = "<path[\\s\\r\\n]+([\\w\\:]+=\"[^\"]*\"[\\s\\r\\n]+)*d=\"(?<Path>[^\"]+)\"";
const string IconPathInXamlPattern = "^\\s*<(StreamGeometry|PathGeometry)\\s+x:Key=\"Geometry/(?<Key>[^\"]+)\"(\\s+[\\w\\:]+=\"[^\"]*\")*>(?<Path>[^\\<]*)";
// check arguments
if (args.Length == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("No file or directory specified.");
return;
}
// collect svg files
var defaultConsoleColor = Console.ForegroundColor;
var svgFilePaths = new HashSet<string>();
foreach (var path in args)
{
if (File.Exists(path))
svgFilePaths.Add(path);
else if (Directory.Exists(path))
{
foreach (var filePath in Directory.EnumerateFiles(path, "*.svg"))
svgFilePaths.Add(filePath);
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Path '{path}' not found.");
}
}
if (svgFilePaths.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("No SVG files found.");
return;
}
Console.ForegroundColor = defaultConsoleColor;
Console.WriteLine($"{svgFilePaths.Count} SVG file(s) found.");
// extract icon paths
var iconPaths = new Dictionary<string, string>();
var iconPathPattern = new Regex(IconPathInSvgPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
var utf8 = new UTF8Encoding(false);
foreach (var svgFilePath in svgFilePaths)
{
try
{
Console.ForegroundColor = defaultConsoleColor;
Console.WriteLine($"Extract icon path from '{svgFilePath}'.");
using var reader = new StreamReader(svgFilePath, utf8);
var rawSvg = reader.ReadToEnd();
var match = iconPathPattern.Match(rawSvg);
if (match.Success)
{
var iconPath = match.Groups["Path"].Value;
if (match.NextMatch()?.Success != true)
iconPaths[Path.GetFileNameWithoutExtension(svgFilePath)] = iconPath;
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"Invalid icon path in '{svgFilePath}'.");
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"Icon path not found in '{svgFilePath}'.");
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"Error occurred while extracting icon path from '{svgFilePath}'. {ex.GetType().Name}: {ex.Message}");
}
}
if (iconPaths.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("No icon paths extracted.");
return;
}
Console.ForegroundColor = defaultConsoleColor;
Console.WriteLine($"{iconPaths.Count} icon path(s) extracted.");
// import paths of geometries
try
{
// read all lines
var lines = new List<string>();
using (var reader = new StreamReader(IconsFilePath, utf8))
{
var line = reader.ReadLine();
while (line is not null)
{
lines.Add(line);
line = reader.ReadLine();
}
}
Console.WriteLine($"{lines.Count} line(s) read from Icons.axaml.");
// import and write lines
var geometryPattern = new Regex(IconPathInXamlPattern);
var importCount = 0;
using (var writer = new StreamWriter(IconsFilePath, append: false, utf8))
{
foreach (var line in lines)
{
var match = geometryPattern.Match(line);
if (match.Success)
{
var key = match.Groups["Key"].Value;
if (iconPaths.TryGetValue(key, out var iconPath))
{
Console.WriteLine($"Import icon '{key}'.");
var pathGroup = match.Groups["Path"];
++importCount;
writer.Write(line[..pathGroup.Index]);
writer.Write(iconPath);
writer.WriteLine(line[(pathGroup.Index + pathGroup.Length)..]);
} else
writer.WriteLine(line);
}
else
writer.WriteLine(line);
}
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"Error occurred while importing icon paths. {ex.GetType().Name}: {ex.Message}");
}