-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathgen-flags.ts
More file actions
145 lines (133 loc) · 4.48 KB
/
gen-flags.ts
File metadata and controls
145 lines (133 loc) · 4.48 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
142
143
144
145
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
// prettier-ignore
const args = [
"Silent", "s",
"Silent2", "silent",
"FastInstall", "fast-install",
"PrintToDefault", "print-to-default",
"PrintDialog", "print-dialog",
"Help", "h",
"Help2", "?",
"Help3", "help",
"ExitWhenDone", "exit-when-done",
"ExitOnPrint", "exit-on-print",
"Restrict", "restrict",
"Presentation", "presentation",
"FullScreen", "fullscreen",
"InvertColors", "invertcolors",
"InvertColors2", "invert-colors",
"Console", "console",
"Install", "install",
"UnInstall", "uninstall",
"WithFilter", "with-filter",
"WithSearch", "with-search",
"WithPreview", "with-preview",
"Rand", "rand",
"Regress", "regress",
"Extract", "x",
"Tester", "tester",
"TestApp", "testapp",
"NewWindow", "new-window",
"Log", "log",
"CrashOnOpen", "crash-on-open",
"ReuseInstance", "reuse-instance",
"EscToExit", "esc-to-exit",
"ArgEnumPrinters", "enum-printers",
"ListPrinters", "list-printers",
"SleepMs", "sleep-ms",
"PrintTo", "print-to",
"PrintSettings", "print-settings",
"InverseSearch", "inverse-search",
"ForwardSearch1", "forward-search",
"ForwardSearch2", "fwdsearch",
"NamedDest", "nameddest",
"NamedDest2", "named-dest",
"Page", "page",
"View", "view",
"Zoom", "zoom",
"Scroll", "scroll",
"AppData", "appdata",
"Plugin", "plugin",
"StressTest", "stress-test",
"N", "n",
"Max", "max",
"MaxFiles", "max-files",
"Render", "render",
"ExtractText", "extract-text",
"Bench", "bench",
"Dir", "d",
"InstallDir", "install-dir",
"Lang", "lang",
"UpdateSelfTo", "update-self-to",
"ArgDeleteFile", "delete-file",
"BgCol", "bgcolor",
"BgCol2", "bg-color",
"FwdSearchOffset", "fwdsearch-offset",
"FwdSearchWidth", "fwdsearch-width",
"FwdSearchColor", "fwdsearch-color",
"FwdSearchPermanent", "fwdsearch-permanent",
"MangaMode", "manga-mode",
"Search", "search",
"AllUsers", "all-users",
"AllUsers2", "allusers",
"RunInstallNow", "run-install-now",
"Adobe", "a",
"DDE", "dde",
"EngineDump", "engine-dump",
"SetColorRange", "set-color-range",
"PreviewPipe", "preview-pipe",
"IFilterPipe", "ifilter-pipe",
"TestPreviewPipe", "test-preview-pipe",
];
function generateCode(): string {
const lines: string[] = [];
// collect enum names and string names
const enumNames: string[] = [];
const strNames: string[] = [];
for (let i = 0; i < args.length; i += 2) {
enumNames.push(args[i]);
strNames.push(args[i + 1]);
}
// generate enum class Arg, 4 per line
lines.push("// clang-format off");
lines.push("enum class Arg {");
lines.push(" Unknown = -1,");
for (let i = 0; i < enumNames.length; i += 4) {
const chunk = enumNames.slice(i, i + 4);
const parts = chunk.map((name, j) => `${name} = ${i + j}`);
lines.push(` ${parts.join(", ")},`);
}
lines.push("};");
lines.push("");
// generate gArgNames, 4 per line
lines.push("static const char* gArgNames =");
for (let i = 0; i < strNames.length; i += 4) {
const chunk = strNames.slice(i, i + 4);
const parts = chunk.map(s => `"${s}\\0"`).join(" ");
const isLast = i + 4 >= strNames.length;
lines.push(` ${parts}${isLast ? ";" : ""}`);
}
lines.push("// clang-format on");
return lines.join("\n");
}
function main() {
const rootDir = join(import.meta.dir, "..");
const flagsPath = join(rootDir, "src", "Flags.cpp");
const content = readFileSync(flagsPath, "utf-8");
const startMarker = "// @gen-start flags";
const endMarker = "// @gen-end flags";
const startIdx = content.indexOf(startMarker);
const endIdx = content.indexOf(endMarker);
if (startIdx < 0 || endIdx < 0) {
console.error("Could not find gen markers in src/Flags.cpp");
process.exit(1);
}
const generated = generateCode();
const before = content.substring(0, startIdx + startMarker.length);
const after = content.substring(endIdx);
const newContent = before + "\n" + generated + "\n" + after;
writeFileSync(flagsPath, newContent, "utf-8");
console.log("Generated flags code in src/Flags.cpp");
}
main();