-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.js
executable file
·249 lines (204 loc) · 6.17 KB
/
new.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env node
/*
* Types = command, core, event, lib, message, numeric
*/
const fs = require("fs"),
minimist = require("minimist");
const yikes = (...a) => { console.error(...a); process.exit(1); };
const [,, ...args] = process.argv;
const options = minimist(process.argv.slice(2), {
alias: {
s: "nosrc",
h: "noheader",
o: "overwrite"
},
boolean: ["nosrc", "noheader", "overwrite"],
default: {
nosrc: false,
noheader: false,
overwrite: false,
}
});
if ((args[0] || "").match(/^(types|help)$/i)) {
console.log("Supported types:", ["command", "core", "event", "lib", "message", "numeric"].map(x => `\x1b[1m${x}\x1b[0m`).join(", "));
process.exit(0);
}
if (args.length < 2 || !args[1]) {
yikes("Usage: new.js [type=command,core,event,lib,message,numeric] [name] [-s/--nosrc] [-h/--noheader] [-o/--overwrite]");
}
const [type, name] = args;
if (!name.match(/^[\w_\d]+$/i)) {
yikes("Invalid name:", name);
}
let sourcename, headername, sourcetext, headertext;
let afterWrite = () => {};
const sourcebase = "src", headerbase = "include/pingpong";
if (type.match(/^(com(mands?)?|cmd)$/i)) {
sourcename = `commands/${name}.cpp`;
headername = `commands/${name}.h`;
const cls = `${name}_command`;
headertext = prepare(`
% #ifndef PINGPONG_COMMANDS_${name.toUpperCase()}_H_
% #define PINGPONG_COMMANDS_${name.toUpperCase()}_H_
%
% #include "pingpong/commands/Command.h"
%
% namespace PingPong {
% class ${cls}: public command {
% public:
%
% operator std::string() const override;
% virtual bool send() override;
% };
% }
%
% #endif`);
sourcetext = prepare(`
% #include "pingpong/commands/${name}.h"
%
% namespace PingPong {
% ${cls}::operator std::string() const {
%
% }
% }`);
} else if (type.match(/^(mes(sages?)?|msg|m)(-\w+)?$/i)) {
const base = type.match(/-/)? type.replace(/^.+-/, "") : null;
const parent = base? `${base}_message` : "message";
sourcename = `messages/${name}.cpp`;
headername = `messages/${name}.h`;
const cls = `${name}_message`;
headertext = prepare(`
% #ifndef PINGPONG_MESSAGES_${name.toUpperCase()}_H_
% #define PINGPONG_MESSAGES_${name.toUpperCase()}_H_
%
% #include "pingpong/messages/Message.h"
% ${base? `#include "pingpong/messages/${base}.h"\n` : ""}
% namespace PingPong {
% class ${cls}: public ${parent} {
% public:
% using ${parent}::${parent};
% static constexpr auto getName = []() -> std::string { return "${args[2] || name.toUpperCase()}"; };
%
% operator std::string() const override;
% };
% }
%
% #endif`);
sourcetext = prepare(`
% #include "pingpong/messages/${name}.h"
%
% namespace PingPong {
% ${cls}::operator std::string() const {
%
% }
% }`);
const ircPath = sourcebase + "/core/irc.cpp";
let ircText = fs.readFileSync(ircPath, "utf8");
if (ircText.indexOf(`add_ctor<${cls}>`) == -1) {
fs.writeFileSync(ircPath,
ircText = ircText.replace(/(void irc::init_messages\(\) {)/, `$1\n\t\tmessage::add_ctor<${cls}>();`));
}
if (ircText.indexOf(`"messages/${name}.h"`) == -1) {
fs.writeFileSync(ircPath,
ircText = ircText.replace(/(#include "pingpong\/events\/server_status.h"\n)/, `$1\n#include "pingpong/messages/${name}.h"`));
}
} else if (type.match(/^(ev?|ev(en)?t)(-\w+)?$/i)) {
const base = type.match(/-/)? type.replace(/^.+-/, "") : null;
const parent = base? `${base}_event` : "event";
headername = `events/${name}.h`;
const cls = `${name}_event`;
headertext = prepare(`
% #ifndef PINGPONG_EVENTS_${name.toUpperCase()}_H_
% #define PINGPONG_EVENTS_${name.toUpperCase()}_H_
%
% #include "pingpong/events/${["sourced", "targeted"].includes(base)? base : "event"}.h"
%
% namespace PingPong {
% struct ${cls}: public ${parent} {
% using ${parent}::${parent};
% };
% }
%
% #endif`);
} else if (type.match(/^l(ib(rary)?)?$/i)) {
sourcename = `lib/${name}.cpp`;
headername = `lib/${name}.h`;
headertext = prepare(`
% #ifndef PINGPONG_LIB_${name.toUpperCase()}_H_
% #define PINGPONG_LIB_${name.toUpperCase()}_H_
%
% namespace PingPong {
%
% }
%
% #endif`);
sourcetext = prepare(`
% #include "pingpong/lib/${name}.h"
%
% namespace PingPong {
%
% }`);
} else if (type.match(/^n(um(eric)?)?$/i)) {
sourcename = `numerics/${name}.cpp`;
sourcetext = prepare(`
% #include "pingpong/messages/Numeric.h"
%
% namespace PingPong {
% bool numeric_message::handle_${name}(server *serv) {
%
% return true;
% }
% }`);
afterWrite = () => {
let numeric_h = fs.readFileSync("include/pingpong/messages/Numeric.h", "utf8");
let numeric_cpp = fs.readFileSync("src/messages/numeric.cpp", "utf8");
numeric_h = numeric_h.replace(/(\n\t};\n}\n\n#endif)/, `\n\t\t\tbool handle_${name}(server *);$1`);
numeric_cpp = numeric_cpp.replace(/(default: return true;)/, `case numeric_type::${name}: return handle_${name}(serv);\n\t\t\t$1`);
fs.writeFileSync("include/pingpong/messages/Numeric.h", numeric_h);
fs.writeFileSync("src/messages/numeric.cpp", numeric_cpp);
};
} else if (type.match(/^core$/i)) {
sourcename = `core/${name}.cpp`;
headername = `core/${name}.h`;
headertext = prepare(`
% #ifndef PINGPONG_CORE_${name.toUpperCase()}_H_
% #define PINGPONG_CORE_${name.toUpperCase()}_H_
%
% namespace PingPong {
%
% }
%
% #endif`);
sourcetext = prepare(`
% #include "pingpong/core/${name}.h"
%
% namespace PingPong {
%
% }`);
} else {
console.error("Unknown type:", type);
yikes(`Expected "command" | "core" | "lib" | "message" | "event"`);
}
if (sourcetext && !options.nosrc) {
const fn = `${sourcebase}/${sourcename}`;
if (fs.existsSync(fn) && !options.overwrite) {
console.error(`Error: \x1b[1m${fn}\x1b[0m already exists.`);
} else {
fs.writeFileSync(fn, sourcetext);
console.log(`Wrote to \x1b[1m${fn}\x1b[0m.`);
}
}
if (headertext && !options.noheader) {
const fn = `${headerbase}/${headername}`;
if (fs.existsSync(fn) && !options.overwrite) {
console.error(`Error: \x1b[1m${fn}\x1b[0m already exists.`);
} else {
fs.writeFileSync(fn, headertext);
console.log(`Wrote to \x1b[1m${fn}\x1b[0m.`);
}
}
afterWrite();
function prepare(text) {
// Remove the initial newline and then the extra tabs and add a trailing newline..
return text.substr(1).replace(/^\t+%\t?/gm, "") + "\n";
}