forked from Skytils/SkytilsMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropertyConverter.ts
71 lines (52 loc) · 2.78 KB
/
propertyConverter.ts
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
// propertyConverter.ts
// Read the input file
const inputFile = await Deno.readTextFile(Deno.args[0]);
// Regular expression patterns to match @Property annotations
const propertyPattern2 = /@Property\((.+?)\s{4}\)/gs;
const i18nKeys: { [key: string]: string } = {};
const i18nCategories: Map<string, string> = new Map();
const i18nSubcategories: Map<string, string> = new Map();
// Process each @Property annotation
let updatedFile = inputFile;
for (const match of inputFile.matchAll(propertyPattern2)) {
const [originalAnnotation, properties] = match;
updateAnnotation(originalAnnotation, properties);
}
// Write the updated file with modified annotations
await Deno.writeTextFile('updatedFile.kt', updatedFile);
// Generate the en_US.lang file
const langFileLines = Object.entries(i18nKeys)
.map(([key, value]) => `${key}=${value}`);
// Add i18nCategory and i18nSubcategory entries
for (const [key, value] of i18nCategories) {
langFileLines.push(`${key}=${value}`);
}
for (const [key, value] of i18nSubcategories) {
langFileLines.push(`${key}=${value}`);
}
const langFileContent = langFileLines.join('\n');
await Deno.writeTextFile('en_US.lang', langFileContent);
console.log('updatedFile.kt and en_US.lang files generated successfully!');
function updateAnnotation(originalAnnotation: string, properties: string) {
const propMap = new Map<string, string>();
const propRegex = /(\w+)\s*=\s*(".*?"|'.*?'|[^,\s]+)/g;
for (const prop of properties.matchAll(propRegex)) {
const [_, key, value] = prop;
propMap.set(key, value.replace(/^(")|("$)/g, '').replace(/^(')|('$)/g, ''));
}
const name = propMap.get('name') || '';
const category = propMap.get('category') || '';
const subcategory = propMap.get('subcategory') || '';
const normalizedName = name.toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '');
const normalizedCategory = category.toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '');
const normalizedSubcategory = subcategory.toLowerCase().replace(/\s+/g, '_').replace(/[^a-z0-9_]/g, '');
const i18nKey = `skytils.config.${normalizedCategory}.${normalizedSubcategory}.${normalizedName}`;
i18nKeys[i18nKey] = name;
const i18nName = `skytils.config.${normalizedCategory}.${normalizedSubcategory}.${normalizedName}`;
const i18nCategory = `skytils.config.${normalizedCategory}`;
const i18nSubcategory = `skytils.config.${normalizedCategory}.${normalizedSubcategory}`;
i18nCategories.set(i18nCategory, category);
i18nSubcategories.set(i18nSubcategory, subcategory);
const newAnnotation = `@Property(${properties}\n, i18nName = "${i18nName}", i18nCategory = "${i18nCategory}", i18nSubcategory = "${i18nSubcategory}")`;
updatedFile = updatedFile.replace(originalAnnotation, newAnnotation);
}