-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
526 lines (411 loc) · 17 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
"use strict;"
/**************************************************************
* Author: Kashif Iqbal Khan
* Email: [email protected]
* Copyright: 2013-2014 AppBakerZ (appbakerz.com)
***************************************************************/
var distdirRoot = "dist/",
tempdir = distdirRoot + "temp/",
finalIncludesDir = tempdir + "final-includes/";
function extend(defaults, override){
for(var key in override) {
if(override.hasOwnProperty(key)) {
var val = override[key];
if (val instanceof Array) {
var arr = new Array(val.length);
for (var i=0 ; i<val.length; i++) {
arr[i] = val[i];
}
defaults[key] = arr;
}
else if (typeof val == "object") {
var target = defaults[key];
if (!target) {
target = {};
defaults[key] = target;
}
extend(target, val);
}
else {
defaults[key] = val;
}
}
}
return defaults;
}
function copyValueIfMissing(obj, key1, key2, extra){
if (!obj.hasOwnProperty(key1) || obj[key1].length == 0) {
obj[key1] = obj[key2] + (extra? " " + extra: "");
}
}
var namingHelper = require("./grunt-modules/naming-helper.js"),
taskUtils = require("./grunt-modules/task-utils.js"),
customPostHandler = require("./grunt-modules/custom-post-handler.js");
module.exports = function(grunt) {
var path = require("path");
var userParams = grunt.file.readJSON("build.json"),
buildParams = {
"author-name": "",
"author-uri": "",
"author-email": "",
"owner-name": "",
"owner-uri": "",
"owner-email": "",
"plugin-name": "",
"plugin-slug": "",
"plugin-desc": "",
"plugin-uri": "",
"settings": {},
"custom-posts": [],
"options": {
"settings-page": true,
"widgets": []
},
};
// Valid that user has provided values for required parameters in build.json
var missing = ["plugin-name", "author-name", "author-email"].filter(function(item) {
var v = userParams[item];
return (!v || v.trim().length == 0);
});
if (missing.length) {
grunt.fail.fatal("Not found valid values for: " + missing);
return;
}
// Use default values for parameters not in build.json
extend(buildParams, userParams);
for(var key in buildParams) {
if(buildParams.hasOwnProperty(key)) {
var val = buildParams[key]
if (typeof val == "string") {
buildParams[key] = buildParams[key].trim();
}
}
}
if (buildParams["plugin-slug"]) {
// TODO: validate that plugin-slug should not contain invalid chars
} else {
// auto generated plugin-slug should be a valid file-system name
buildParams["plugin-slug"] = buildParams["plugin-name"].toLowerCase().replace(namingHelper.RE_WORD_BOUNDARY, "-");
}
if (buildParams["plugin-class-name"]) {
// TODO: validate that className is valid identifier
} else {
// auto generated plugin-class-name should be a valid identifier
buildParams["plugin-class-name"] = namingHelper.makeValidIdentifier(buildParams["plugin-name"].replace(namingHelper.RE_WORD_BOUNDARY, "_"));
}
buildParams["plugin-class-name-upper"] = buildParams["plugin-class-name"].toUpperCase();
copyValueIfMissing(buildParams, "plugin-desc", "plugin-name", "Short Description");
copyValueIfMissing(buildParams, "owner-name", "author-name");
copyValueIfMissing(buildParams, "owner-email", "author-email");
copyValueIfMissing(buildParams, "owner-uri", "author-uri");
var repodir = distdirRoot + buildParams["plugin-slug"] + "/",
distdir = repodir + "src/", // The path to package directory
replacements = [];
for(key in buildParams) {
if(buildParams.hasOwnProperty(key)) {
replacements.push(taskUtils.makeReplacementObject(key, buildParams[key]));
}
}
// don't copy widget/custom-post related files, these will be included in a separate files object
var phpSourceFiles = ["**/*.php", "!**/*{plugin-widget*.*", "!**/custom-post-*.*"];
// Grunt default task list
var taskList = ["clean", "string-replace:prod", "fileregexrename:prod"];
// Grunt project configuration.
var cfg = {
array2file: {},
clean: [
tempdir + "custom-posts",
tempdir + "final-includes",
tempdir,
distdirRoot + "temp2/",
distdirRoot + buildParams["plugin-slug"], "dist/"
],
copy: {
"grunt-modules": {
src : ["grunt-modules/copyhelper.js"],
dest: repodir
},
"node-modules": {
files: [
// Note that .php/.css files are copied as .php.js/.css.js. This is to hack preprocess to think .php.js file as js files
{expand: true, cwd: "node_modules", dest: repodir + "node_modules/", src : [
"grunt/**",
"grunt-contrib-clean/**",
"grunt-contrib-copy/**",
"grunt-string-replace/**",
"gettext-parser/**",
"grunt-preprocess/**"
] }
]
}
},
concat: {
"merge-section-functions": {
src : [distdirRoot + "temp2/*-function.txt"],
dest: tempdir + 'sections-functions.inc'
}
},
"string-replace": {
prod: {
options: {
replacements: replacements
},
files: [
// Note that .php/.css files are copied as .php.js/.css.js. This is to hack preprocess to think .php.js file as js files
{expand: true, cwd: "src/plugin-template", src : phpSourceFiles, dest: distdir, ext: ".php.js" },
{expand: true, cwd: "src/plugin-template", src : ["**/*.css", "!**/*plugin-widget*.css","!**/*custom-post*.css"], dest: distdir, ext: ".css.js" },
{expand: true, cwd: "src/plugin-template", src : ["**/*.*", "!**/*.css", "!**/*.php", "!**/*plugin-widget*.*", "!**/*custom-post*.*"], dest: distdir },
{expand: true, cwd: "src/grunt-includes", src : ["**/*.*"], dest: tempdir },
{expand: true, cwd: "src/repo-dir/", src : ["**/*.*", ".gitignore"], dest: repodir }
]
},
plugininfofile: {
options: {
inline:true,
replacements: [{
pattern: /\@\@ifdef\s/g,
replacement: "@ifdef "
},{
pattern: /\@\@ifndef\s/g,
replacement: "@ifndef "
},{
pattern: /\@\@endif\s/g,
replacement: "@endif "
}
]
},
src: distdir + buildParams["plugin-slug"] + ".php.js",
dest: distdir + buildParams["plugin-slug"] + ".php.js"
}
},
fileregexrename: {
prod: {
options: {
replacements: replacements
},
files: [
{ expand: true, cwd: distdir, src: "**/*.*", dest: distdir }
]
},
repodir: {
options: {
replacements: replacements
},
files: [
{ expand: true, cwd: repodir, src: ["*.*"], dest: repodir }
]
},
},
preprocess: {
options: {
// NOTE that if context is defined in the task, it will replace the global context, (not merge it)
// This looks like a grunt bug where deep merging is not performed for options
context : {
}
},
prod: {
options: {
inline: true,
},
src : [ 'dist/**/*.php.js', 'dist/**/*.js', 'dist/**/*.css.js' ]
}
},
"generate-widget-include-file": {
prod : {
files: [ { src: [] , dest: tempdir + "widgets.php" }]
}
}
};
var preprocessContext = cfg.preprocess.options.context;
for(var prop in buildParams.options) {
var opt = buildParams.options[prop];
if ((typeof opt == "boolean" && opt) || (typeof opt == "Array" && opt.length > 0)) {
var key = prop.toUpperCase().replace(/-/g, "");
preprocessContext[key] = opt;
}
}
if (buildParams.options["settings-page"] !== true) {
phpSourceFiles.push("!**inc/admin-settings.php");
}
var stringReplaceTask = cfg["string-replace"],
fileRenameTask = cfg["fileregexrename"]
var widgets = buildParams.options.widgets,
widgetFiles = [];
if (widgets.length > 0) {
var widgetHandler = require("./grunt-modules/widget-handler.js");
// Set preprocessor context variable so it is available for grunt-preprocess @ifdef
preprocessContext["WIDGETS"] = true;
widgets.forEach(function(widget) {
grunt.log.debug("widgets.forEach: " + widget );
// Valid that one of widget-id and widget-class-name are present
if (!widget.id) {
grunt.log.fail("You should provide id for every widget.");
return;
}
var widgetResults = widgetHandler.generate(grunt, widget, buildParams, replacements, distdir);
// add a new string-replace task for this widget
stringReplaceTask[widget.id] = widgetResults["string-replace"];
// add a new fileregexrename task for this widget
fileRenameTask[widget.id] = widgetResults["fileregexrename"];
// add string-replace task in default task list for this widget
taskList.push("string-replace:" + widget.id);
// add fileregexrename task in default task list for this widget
taskList.push("fileregexrename:" + widget.id);
widgetFiles.push("class-" + widgetResults.widgetId + ".php");
}); // end widgets.forEach
cfg["generate-widget-include-file"].prod.files[0].src = widgetFiles;
}
// The preprocess plugin requires that every include file must be present even if the include is
// dynamic and inside a falsy @ifdef block.
// We need to add the task to default task list even when no widget so that widgets.php is always generated.
taskList.push("generate-widget-include-file");
var settings = buildParams.settings,
settingFiles = []
sectionIds = [];
if (settings && Object.keys(settings).length > 0) {
// Set preprocessor context variable so it is available for grunt-preprocess @ifdef
preprocessContext["SETTINGS"] = true;
// found atleast one property in setting object
grunt.log.debug("found settings");
var sectionHandler = require("./grunt-modules/section-handler.js");
for (var pageProp in settings) {
var page = settings[pageProp];
// TODO: for now, we support only one page
grunt.log.debug("page: " + pageProp);
var sectionIndex = 0;
for (var sectionProp in page) {
// Each property of a page is a section
var section = page[sectionProp];
section["pluginGeneratorMeta"] = {
title: sectionProp,
index: sectionIndex++
};
var sectionResult = sectionHandler.generate(grunt, section, buildParams, replacements, distdirRoot);
var sectionStringReplaceTasks = sectionResult["string-replace"];
grunt.log.debug("Adding tasks...");
sectionResult.taskNameList.forEach(function(qualifiedTaskName) {
grunt.log.debug(qualifiedTaskName);
var taskNameParts = qualifiedTaskName.split(":"),
taskPlugin = taskNameParts[0].trim(),
taskName = taskNameParts[1].trim();
if (taskPlugin == "string-replace") {
stringReplaceTask[taskName] = sectionStringReplaceTasks[taskName];
taskList.push(qualifiedTaskName);
}
});
grunt.log.debug("Adding files...");
sectionResult.settingFiles.forEach(function(item) {
grunt.log.debug(item);
settingFiles.push(item);
});
} // end-for sectionProp
} // end-for pageProp
}
var customPosts = buildParams["custom-posts"],
customPostMetaBoxes = {};
if (customPosts && Object.keys(customPosts).length > 0) {
// Set preprocessor context variable so it is available for grunt-preprocess @ifdef
preprocessContext["CUSTOMPOSTS"] = true;
// found atleast one property in custom posts object
grunt.log.debug("found custom posts");
customPosts.forEach(function(customPost) {
grunt.log.debug("custom post: " + customPost.name);
var customPostResult = customPostHandler.generate(grunt, customPost, buildParams, replacements, distdir, tempdir);
grunt.log.debug("Adding tasks...");
taskUtils.populateCfgTasks( customPostResult.taskNameList,
cfg,
customPostResult.tasks,
taskList,
grunt
)
customPostMetaBoxes[ customPostResult.postSlug ] = customPostResult.metaBoxes;
}); // end customPosts.forEach
} //end if customPosts
/*********************************************************************************************************
* concat all {custom-post-slug}-require-custom-post.inc files to a single file custom-post-require.inc
*********************************************************************************************************/
// We need to generate custom-post-require.inc anyway because preprocess will keep throwing error if
// the file does not exusts although its include tag is inside @ifdef
cfg.concat["merge-require-custom-post"] = {
src : [tempdir + customPostHandler.WORKING_FOLDER_NAME + "/*-require-custom-post.inc"],
dest: finalIncludesDir + "custom-post-require.inc"
};
taskList.push("concat:merge-require-custom-post");
grunt.initConfig(cfg);
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks('grunt-preprocess');
grunt.loadNpmTasks("grunt-string-replace");
grunt.loadNpmTasks("grunt-file-regex-rename");
grunt.loadTasks("./grunt-modules/tasks/");
grunt.registerTask("generate-include-files", "Generates temp2/handle_admin_init.txt and handle-add-meta-boxes.inc to be included in plugin file", function() {
var fs = require("fs");
var spaces = " ",
spaces4 = spaces + spaces + spaces + spaces;
var codeLines = [
spaces + "// Make " + sectionIds.length + " Sections",
spaces + "$sections = array(",
spaces4 + '"' + sectionIds.join('",\r\n' + spaces4 + '"') + '"' ,
spaces + spaces + ");",
"\r\n"
];
var contents = [];
if (settingFiles.length) {
contents = settingFiles.map(function(item){
return fs.readFileSync(item);
});
}
fs.writeFileSync(tempdir + "handle_admin_init.txt", codeLines.join("\r\n") + contents.join("\r\n"));
// generate the include file {custom-post-slug}-handle-add-meta-boxes.inc for custom post
for (var postSlug in customPostMetaBoxes) {
var metaBoxes = customPostMetaBoxes[postSlug];
fs.writeFileSync( tempdir + postSlug + "-handle-add-meta-boxes.inc",
metaBoxes.map(function(mb){
return " $this->add_meta_box('"
+ mb.id
+ "', __('"+ mb.name +"', '" + buildParams["plugin-slug"] + "'), "
+ "'render_" + mb.funcName + "_metabox');";
}).join("\r\n")
);
}
});
taskList.push("generate-include-files");
grunt.registerTask("perform-final-tasks", "", function() {
// Force task into async mode and grab a handle to the "done" function.
var done = this.async();
var fs = require('fs'),
copyHelper = require("./grunt-modules/copyhelper");
copyHelper.walk(distdir, function(error, found) {
if (error) {
grunt.fail.fatal(error);
done();
return;
}
/*
found.dirs.forEach(function(item){
grunt.file.copy("src/grunt-includes/index.php", path.resolve(path.join(item, "index.php")));
});
*/
found.files.forEach(function(item){
// if the file extension is .php.js or .css.js, remove the trailing .js
var trail = item.substr(item.length-7);
if(trail == ".php.js"
|| trail == ".css.js"
) {
fs.renameSync(item, item.substr(0, item.length-3));
}
});
done();
});
});
// Default task(s).
taskList.push("concat:merge-section-functions");
taskList.push("preprocess");
taskList.push("copy:grunt-modules");
taskList.push("string-replace:plugininfofile");
// taskList.push("fileregexrename:repodir");
// taskList.push("copy:node-modules");
// taskList.push("perform-final-tasks");
grunt.registerTask("default", taskList);
};