-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
85 lines (76 loc) · 3.06 KB
/
build.js
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
const fs = require("fs");
const path = require("path");
/**
* Helper function to parse the given json file.
* @param {*} fileName
*/
const loadJsonFile = (fileName) => {
if (!fs.existsSync(fileName)) {
console.error(`Package file ${fileName} not found`);
}
return JSON.parse(fs.readFileSync(fileName, "utf8"));
};
/**
* Convert a snake case string to a camel case string.
* @param {*} str
* @returns
*/
const SnakeCaseToCamelCase = (str) => str.toLowerCase().replace(/((^|[-_])[a-z0-9])/g, (group) => group.toUpperCase().replace("-", "").replace("_", ""));
const writeSvg = (output, metaItem, indent = "\t") => {
const regexSvgPath = /<path(.*?)\/\>/g;
const regexReplaceQuotes = /\"/g;
const svgFileName = path.join("node_modules", "@mdi", "svg", "svg", metaItem.fileName);
const svgData = fs.readFileSync(svgFileName, "utf-8");
if ((m = regexSvgPath.exec(svgData)) !== null) {
const pathData = m[0].replace(regexReplaceQuotes, '""');
output.push(`${indent}/// <summary>`);
output.push(`${indent}/// <b>Codepoint: </b>${metaItem.codepoint}<br/>`);
output.push(`${indent}/// <b>Name: </b>mdi-${metaItem.name}<br/>`);
output.push(`${indent}/// <b>Author: </b>${metaItem.author}<br/>`);
output.push(`${indent}/// </summary>`);
output.push(`${indent}public const string ${metaItem.iconName} = @"${pathData}";`);
output.push("");
} else {
console.error(`Could not extract svg path from "${svgFileName}"`);
}
};
const writeSvgCategory = (metaItems, category) => {
console.info(`Writing ${metaItems.length} ${category} icons...`);
const output = [];
output.push("namespace Bromix.MudBlazor.MaterialDesignIcons;");
output.push("");
output.push("/// Collection of Material Design Icons");
output.push("public static partial class MaterialDesignIcons");
output.push("{");
output.push(`\t/// ${category} variant of Material Design Icons`);
output.push(`\tpublic static class ${category}`);
output.push("\t\t{");
for (var metaItem of metaItems) {
writeSvg(output, metaItem, "\t\t");
}
output.push("\t\t}");
output.push("}");
const sourceFileName = path.join(`Bromix.MudBlazor.MaterialDesignIcons/MaterialDesignIcons.${category}.cs`);
fs.writeFileSync(sourceFileName, output.join("\n"));
};
const packageFileName = path.join("node_modules", "@mdi", "svg", "package.json");
const package = loadJsonFile(packageFileName);
console.info(`Material Design Icon v${package.version}`);
const metaFileName = path.join("node_modules", "@mdi", "svg", "meta.json");
const meta = loadJsonFile(metaFileName);
const normalIcons = meta
.filter((item) => !item.name.endsWith("-outline"))
.map((item) => {
item.fileName = `${item.name}.svg`;
item.iconName = SnakeCaseToCamelCase(item.name);
return item;
});
writeSvgCategory(normalIcons, "Normal");
const outlineIcons = meta
.filter((item) => item.name.endsWith("-outline"))
.map((item) => {
item.fileName = `${item.name}.svg`;
item.iconName = SnakeCaseToCamelCase(item.name.replace("-outline", ""));
return item;
});
writeSvgCategory(outlineIcons, "Outline");