-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.pike
372 lines (294 loc) · 10.9 KB
/
export.pike
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
string DIR = dirname(__FILE__);
private array(string) _input_files;
array(string) `input_files() {
if (_input_files) return _input_files;
array(string) ret = ({ });
foreach (Filesystem.Traversion(combine_path(DIR, "toolkit")); string dir; string f) {
if (!has_suffix(f, ".js")) continue;
ret += ({ combine_path(dir, f) });
}
return _input_files = ret;
}
string find_name(string line) {
if (-1 == search(line, "TK.class")) return 0;
array(string) tmp = line / "=";
tmp = map(tmp, String.trim_all_whites);
array tmp2;
tmp2 = filter(tmp, has_prefix, "TK.");
if (sizeof(tmp2)) return tmp2[0][3..];
tmp2 = filter(tmp, has_prefix, "w.TK.");
if (sizeof(tmp2)) return tmp2[0][5..];
return 0;
}
string filter_comment(string in) {
String.Buffer out = String.Buffer();
for (int i = 0; i < sizeof(in); i++) {
int c = in[i];
if (c == '/' && i+1 < sizeof(in)) {
if (in[i+1] == '/') {
int end = search(in, '\n', i);
if (end == -1) error("EOF in comment\n");
i = end;
continue;
} else if (in[i+1] == '*') {
int end = search(in, "*/", i+2);
if (end == -1) error("EOF in comment\n");
i = end;
continue;
}
}
out->putchar(c);
}
return out->get();
}
constant ARGS = ({
({ "mode", Getopt.HAS_ARG, ({ "-m", "--mode" }) }),
({ "theme", Getopt.HAS_ARG, ({ "-t", "--theme" }) }),
({ "output", Getopt.HAS_ARG, ({ "-o", "--output" }) }),
({ "help", Getopt.NO_ARG, ({ "-h", "--help" }) }),
({ "debug", Getopt.NO_ARG, ({ "-d", "--debug" }) }),
({ "gzip", Getopt.NO_ARG, ({ "-g", "--gzip" }) }),
({ "module", Getopt.HAS_ARG, ({ "--module" }) }),
});
void help() {
werror(
#"Usage: pike export.pike [OPTIONS]
This export script scans the toolkit source code to build a dependency graph.
This dependency graph can then be used to generate custom minified versions
of both the JavaScript code and the CSS files.
Options:
--mode Export mode. Either 'js' or 'css'. Defaults to 'js'.
-m
--output Output directory. All files will be placed into the
-o directory specified here.
--debug Generate a 'debug' version which is not minified.
-d
--help Print this help.
-h
--gzip Gzip all files by default.
-g
");
}
mapping(string:array(string)) calculate_dependencies(mapping(string:string) widget_to_file,
mapping(string:string) file_to_widget,
mapping(string:int) js_files) {
array(string) src = map(map(map(map(input_files, Function.curry(combine_path)(DIR)), Stdio.read_file),
utf8_to_string), filter_comment);
mapping(string:array(string)) dependencies = ([]);
void add_manual_dep(string widget, string file, string ... deps) {
string tmp = filter(input_files, has_suffix, file)[0];
widget_to_file[widget] = tmp;
file_to_widget[tmp] = widget;
dependencies[widget] = deps;
};
add_manual_dep("RAF", "raf.js");
add_manual_dep("G", "G.js");
add_manual_dep("LIB", "toolkit.js", "RAF", "G");
foreach (src; int i; string file) {
array(string) lines = file/"\n";
foreach (lines;; string line) {
string name = find_name(line);
if (name == "class") continue;
if (name) {
widget_to_file[name] = input_files[i];
file_to_widget[input_files[i]] = name;
break;
}
}
}
object regexp = Regexp.PCRE.Widestring("\(TK.[A-Za-z_]+\)");
foreach (src; int i; string file) {
array(string) tmp = ({ "LIB" });
array(string) words = ({ });
regexp.matchall(file, lambda(array a) {
words += a;
});
foreach (widget_to_file; string widget; string fname) {
if (input_files[i] == fname) continue;
if (widget == "Base" || has_value(words, "TK."+widget)) {
tmp += ({ widget });
}
}
if (string widget = file_to_widget[input_files[i]]) {
if (dependencies[widget]) continue;
// it is a widget
dependencies[widget] = tmp;
//werror("%s -> %{%O, %} \n", file_to_widget[input_files[i]], tmp);
} else {
// it is not a widget, so we have to take all dependencies
foreach (tmp;; string widget) js_files[widget_to_file[widget]] = 1;
}
}
return dependencies;
}
array(string) add_dependencies(mapping(string:array(string)) dependencies, mapping(string:int) widgets,
string widget) {
array(string) ret = ({ });
if (widgets[widget]) return ret;
widgets[widget] = 1;
if (!dependencies[widget])
error("No such widget: %s\n", widget);
foreach (dependencies[widget];; string w) {
ret += add_dependencies(dependencies, widgets, w);
}
werror("Adding widget %O\n", widget);
ret += ({ widget });
return ret;
}
array(string) dependency_sort(mapping(string:int) widgets, mapping(string:array(string)) dependencies) {
array(string) ret = ({ });
void add(string widget) {
if (widgets[widget]) {
widgets[widget] = 0;
map(sort(dependencies[widget]), add);
ret += ({ widget });
}
};
map(sort(indices(widgets)), add);
return ret;
}
int main(int argc, array(string) argv) {
mapping(string:string|array|int) options = ([]);
array(string) orig_args = argv[1..];
{
array(array) args = Getopt.find_all_options(argv, ARGS);
foreach (args;; array arg) {
if (!has_index(options, arg[0]))
options[arg[0]] = arg[1];
else if (arrayp(options[arg[0]]))
options[arg[0]] += ({ arg[1] });
else
options[arg[0]] = ({ options[arg[0]], arg[1] });
}
}
if (options->help) {
help();
exit(0);
}
if (!options->mode) options->mode = "js";
if (!options->output) {
werror("Missing output directory.\n");
help();
exit(1);
}
if (!Stdio.is_dir(options->output)) {
werror("No such directory: %s\n", options->output);
exit(1);
}
if (!options->theme) options->theme = "plain";
/* js files to _always_ include */
mapping(string:int) js_files = ([ ]);
mapping(string:string) widget_to_file = ([]);
mapping(string:string) file_to_widget = ([]);
mapping(string:array(string)) dependencies = calculate_dependencies(widget_to_file, file_to_widget,
js_files);
/* widgets we need */
array(string) W = ({});
{
mapping(string:int) widgets = ([]);
array(string) tmp = filter(argv[1..], stringp);
if (!sizeof(tmp)) {
tmp = indices(widget_to_file);
}
foreach (tmp;; string name) {
W += add_dependencies(dependencies, widgets, name);
}
}
if (options->mode == "js") {
array(string) cmd;
if (options->debug) {
cmd = ({ "cat" }) + map(W, widget_to_file);
} else {
cmd = ({ "closure-compiler", "--language_in", "ECMASCRIPT5_STRICT" }) +
map(W, widget_to_file);
}
string fname = combine_path(options->output, "toolkit.min.js");
werror("Running '%s' with output to '%s'\n", cmd*" ", fname);
Stdio.File out = Stdio.File(fname, "wc");
out->truncate(0);
out->write(
#"/* This file is part of the Toolkit library.
* It has been generated using the command
* pike export.pike %{%s %}
*/
", orig_args);
Process.create_process(
cmd,
([
"stderr" : Stdio.stderr,
"stdout" : out,
])
)->wait();
if (options->module == "ES2015") {
out->write("export { TK };\n");
} else if (options->module) {
error("Unsupported module type: %O\n", options->module);
}
out->close();
if (options->gzip)
Process.create_process(({ "gzip", fname }), ([ "stderr" : Stdio.stderr ]))->wait();
} else if (options->mode == "css") {
string DST = combine_path(options->output, "css");
string SRC = combine_path(DIR, "toolkit", "styles");
array(string) files = ({ "toolkit.css" });
if (!Stdio.is_dir(combine_path(SRC, options->theme))) {
werror("Cannot find theme directory: %s\n", combine_path(SRC, options->theme));
exit(1);
}
Stdio.mkdirhier(DST);
string theme_dir = combine_path(SRC, options->theme);
/* super slow */
foreach (W;; string widget) {
widget = lower_case(widget) + ".css";
foreach (Filesystem.Traversion(theme_dir); string dir; string file) {
string path = combine_path(dir, file);
path = path[sizeof(theme_dir)+1..];
if (file == widget) {
files += ({ combine_path(options->theme, path) });
break;
}
}
}
foreach (files;; string path) {
string dst = combine_path(DST, path);
Stdio.mkdirhier(dirname(dst));
if (!Stdio.cp(combine_path(SRC, path), dst)) {
werror("cp '%s' '%s' failed.\n", combine_path(SRC, path), dst);
}
if (!options->debug) {
array cmd = ({ "csso", "-i", dst, "-o", dst });
Process.create_process(cmd, ([ "stderr": Stdio.stderr ]))->wait();
}
if (options->gzip)
Process.create_process(({ "gzip", dst }), ([ "stderr" : Stdio.stderr ]))->wait();
}
string dst = combine_path(DST, "toolkit.all.css");
Stdio.File out = Stdio.File(dst, "wc");
out->truncate(0);
out->write(
#"/* This file is part of the Toolkit library.
* It has been generated using the command
* pike export.pike %{%s %}
*/
", orig_args);
foreach (files;; string path) {
/* this is not really json, but works for this purpose */
out->write("@import %s;\n", string_to_utf8(Standards.JSON.encode(path)));
}
out->close();
if (options->gzip)
Process.create_process(({ "gzip", dst }), ([ "stderr" : Stdio.stderr ]))->wait();
} else if (options->mode == "print") {
werror("Dependencies:\n");
foreach (W;; string widget) {
array(string) widgets = dependencies[widget];
werror("%20s -> %{%s %}\n", widget, sort(widgets));
}
werror("Result: %{ %s %}\n", W);
} else {
werror("No such mode: %s\n", options->mode);
help();
exit(1);
}
return 0;
}