forked from carterjones/hackmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
194 lines (177 loc) · 6.55 KB
/
Gruntfile.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
module.exports = function(grunt) {
"use strict";
function prepare_for_uglify(code) {
code = code.replace(/#s\./g, "SCRIPTOR.");
code = code.replace(/#db\./g, "DATABASE.");
code = code.replace(/([\s\S]*)/, "($1)();");
return code;
}
function prepare_for_upload(code) {
code = code.replace(/^\!([\s\S]*)\(\);$/, "$1");
code = code.replace(/^\(([\s\S]*)\)\(\);$/, "$1");
code = code.replace(/DATABASE\./g, "#db.");
code = code.replace(/SCRIPTOR\./g, "#s.");
return code;
}
function calculate_dependencies(required, library) {
library = library || [];
var dependencies = {};
var current;
var uses = [];
while (required.length) {
current = required.shift();
if (current in dependencies) continue;
if (library.indexOf(current) > -1) {
if (uses.indexOf(current) == -1) {
uses.push(current);
}
continue;
}
var source = grunt.file.read("include/" + current + ".js");
var includes = source.match(/.*INCLUDE\(\w+\).*/g) || [];
includes = includes.map(function(value) { return value.replace(/.*\((.*)\).*/, "$1"); });
required = required.concat(includes);
dependencies[current] = includes;
}
var unordered = [];
for (var key in dependencies) { unordered.push({ 'file': key, 'deps': dependencies[key] }); }
var ordered = [];
while (unordered.length) {
current = unordered.shift();
var nodeps = true;
for (var item in current.deps) {
if (current.deps[item] in dependencies) { nodeps = false; break; }
}
if (nodeps) {
delete dependencies[current.file];
ordered.push(current.file);
} else {
unordered.push(current);
}
}
return [ordered, uses];
}
function generate_includes(includes, library) {
library = library || [];
var code = "";
var i;
if (library.length) {
code += "var __lib__ = SCRIPTOR." + grunt.config('meta.user') + ".lib();\n";
for (i in library) {
code += "var " + library[i] + " = __lib__." + library[i] + ";\n";
}
}
for (i in includes) {
var file = includes[i];
var tmp = grunt.file.read("include/" + file + ".js");
tmp = tmp.replace(/.*INCLUDE\(\w+\).*/g, "");
code += "var " + file + " = " + tmp + ";\n";
}
return code;
}
function create_lib() {
var required = grunt.config('meta.lib');
if (!required.length) return;
grunt.log.write('Request library with ').write(required).writeln(' ... ');
grunt.log.write('Calculating dependencies... ');
var tmp = calculate_dependencies(required);
grunt.config('meta.lib', tmp[0]);
grunt.log.ok();
required = grunt.config('meta.lib');
grunt.log.write('Generate library with ').write(required).write(' ... ');
var code = "function (){\n";
code += generate_includes(required);
code += "return {\n";
for (var i in required) {
var file = required[i];
code += file + " : " + file + ",\n";
}
code += "};\n}";
grunt.file.write("build/lib.js", prepare_for_uglify(code));
grunt.log.ok();
}
grunt.initConfig({
meta: {
user: grunt.option("user") || "dr_dvorak",
lib: [].concat(grunt.option("lib") || []),
},
clean: {
build: ["build/"],
release: ["release/"],
},
copy: {
pre: {
expand: true,
flatten: true,
src: ["tools/*.js", "hacking/*.js", "poc/*.js"],
dest: "build/",
options: {
process: function(content) {
// Determine includes
var includes = content.match(/.*INCLUDE\(\w+\).*/g);
if (includes) {
includes = includes.map(function(value) {
return value.replace(/.*\((.*)\).*/, "$1");
});
var tmp = calculate_dependencies(includes, grunt.config('meta.lib'));
includes = generate_includes(tmp[0], tmp[1]);
content = content.replace(/.*INCLUDE\(\w+\).*/, includes);
content = content.replace(/.*INCLUDE\(\w+\).*/g, "");
}
// prepare code for linting and uglifing
return prepare_for_uglify(content);
}
}
},
post: {
expand: true,
flatten: true,
src: ["build/*.js"],
dest: "release/",
options: {
process: prepare_for_upload
}
}
},
jshint: {
files: ["Gruntfile.js", "build/*.js"]
},
jsbeautifier: {
files: ["include/*.js", "tools/*.js", "hacking/*.js", "poc/*.js", "Gruntfile.js",
"package.json"
],
options: {
js: {
braceStyle: "collapse-preserve-inline",
wrapLineLength: 120
}
}
},
uglify: {
options: {
mangle: true,
compress: true,
beautify: true
},
all: {
files: [{
expand: true,
src: "build/*.js",
}]
}
}
});
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-jsbeautifier");
grunt.registerTask('lib', 'Assemble the library', create_lib);
grunt.registerTask("default", ["dynamic"]);
grunt.registerTask("dynamic", "build static scripts", ["clean", "jsbeautifier", 'lib', "copy:pre", "jshint",
"uglify", "copy:post", "clean:build"
]);
grunt.registerTask("static", "build static scripts", ["clean", "jsbeautifier", "copy:pre", "jshint",
"uglify", "copy:post", "clean:build"
]);
};