forked from versionpress/versionpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
354 lines (292 loc) · 9.91 KB
/
gulpfile.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
/**
* VersionPress build script. Run `gulp` or `gulp help` to see the main tasks.
*/
var gulp = require('gulp-help')(require('gulp'));
var del = require('del');
var shell = require('gulp-shell');
var shelljs = require('shelljs/global');
var rename = require('gulp-rename');
var zip = require('gulp-zip');
var replace = require('gulp-replace');
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var removeLines = require('gulp-remove-lines');
var yaml = require('yamljs');
var composer = require('gulp-composer');
var git = require('gulp-git');
var merge = require('merge-stream');
var runSequence = require('run-sequence');
/**
* Version to be displayed both in WordPress administration and used as a suffix of the generated ZIP file
* @type {string}
*/
var packageVersion = "";
/**
* Directory where the VP files are copied to and some actions
* like `composer install` etc. are run on them.
*
* One exception is the `test-deploy` task that basically just runs the `copy`
* and sets buildDir directly to some WP test site, see `prepare-test-deploy`.
*
* @type {string}
*/
var buildDir = './build';
/**
* Where to put the final ZIP file
*
* @type {string}
*/
var distDir = './dist';
/**
* VersionPress source directory
*
* @type {string}
*/
var vpDir = './plugins/versionpress';
/**
* Frontend source directory
*
* @type {string}
*/
var frontendDir = './frontend';
/**
* Frontend destination directory
*
* @type {string}
*/
var adminGuiDir = vpDir + '/admin/public/gui';
/**
* Array of globs that will be used in gulp.src() to copy files from source to destination.
* Prepared in `prepare-src-definition`.
*
* @type {Array}
*/
var srcDef = [];
/**
* Set to true for test-deploy build
*
* @type {boolean}
*/
var isTestDeployBuild = false;
/**
* Sets `buildDir` and `isTestDeployBuild` so that the copy methods copies to the WP test site.
*/
gulp.task('prepare-test-deploy', false, function () {
var testConfigStr = fs.readFileSync(vpDir + '/tests/test-config.yml', 'utf-8');
var testConfig = yaml.parse(testConfigStr);
var sitePath = process.env.VP_DEPLOY_TARGET || testConfig["sites"][testConfig["test-site"]]["wp-site"]["path"];
if (isRelative(sitePath)) {
sitePath = vpDir + '/tests/' + sitePath;
}
buildDir = sitePath + "/wp-content/plugins/versionpress";
isTestDeployBuild = true;
});
/**
* Prepares `srcDef` that is later used in the `copy` task. The src definition
* is slightly different for production build and test-deploy build.
*/
gulp.task('prepare-src-definition', false, function () {
srcDef = [];
srcDef.push(vpDir + '/**'); // add all, except:
srcDef.push('!' + vpDir + '/.idea{,/**}');
srcDef.push('!' + vpDir + '/.gitignore'); // this .gitignore is valid for a project, not for deployed VP
srcDef.push('!' + vpDir + '/.editorconfig');
srcDef.push('!' + vpDir + '/.gitattributes');
srcDef.push('!' + vpDir + '/tests{,/**}'); // tests might be useful for `test-deploy` but we don't currently need them
srcDef.push('!' + vpDir + '/log/**/!(.gitignore)'); // keep just the .gitignore inside `log` folder
srcDef.push('!' + vpDir + '/**/*.md'); // all Markdown files are considered documentation
// and now some build-specific patterns:
if (isTestDeployBuild) {
srcDef.push('!' + vpDir + '/composer.json');
srcDef.push('!' + vpDir + '/composer.lock');
srcDef.push('!' + vpDir + '/vendor/**/.git{,/**}'); // keep vendor but ignore all nested Git repositories in it
} else {
srcDef.push('!' + vpDir + '/vendor{,/**}'); // `vendor` is fresh-generated later in the `composer-install` task
// keep composer.json|lock
}
});
/**
* Cleans buildDir and distDir
*/
gulp.task('clean', false, function (cb) {
return del([buildDir, distDir], {force: true});
});
/**
* Copies files defined by `srcDef` to `buildDir`
*/
gulp.task('copy', false, ['clean', 'prepare-src-definition', 'frontend-deploy'], function (cb) {
var srcOptions = {dot: true, base: vpDir};
return gulp.src(srcDef, srcOptions).pipe(gulp.dest(buildDir));
});
/**
* Builds the frontend.
*/
gulp.task('frontend-build', false, ['init-frontend'], shell.task([
'npm run build'
], {cwd: frontendDir}));
/**
* Deploys the frontend.
*/
gulp.task('frontend-deploy', false, function (cb) {
var src = frontendDir + '/build/**';
var srcOptions = {dot: true};
return gulp.src(src, srcOptions).pipe(gulp.dest(adminGuiDir));
});
/**
* Builds and deploys the frontend.
*/
gulp.task('frontend-build-and-deploy', false, function (cb) {
runSequence('frontend-build', 'frontend-deploy', cb);
});
/**
* Installs Composer packages, ignores dev packages prefers dist ones
*/
gulp.task('composer-install', false, ['copy'], shell.task(['composer install -d ' + buildDir + ' --no-dev --prefer-dist --ignore-platform-reqs --optimize-autoloader']));
/**
* Removes composer.json|lock after the `composer-install` task is done
*/
gulp.task('remove-composer-files', false, ['composer-install'], function (cb) {
return del([buildDir + '/composer.json', buildDir + '/composer.lock']);
});
/**
* Disables the debugger because we don't want to handle all exceptions and errors caused by all plugins. See WP-268.
*/
gulp.task('disable-debugger', false, ['copy'], function (cb) {
return gulp.src(buildDir + '/bootstrap.php').pipe(removeLines(
{filters: [/^Debugger::enable/]}
)).pipe(gulp.dest(buildDir));
});
/**
* Fills the packageVersion variable
*/
gulp.task('fill-vp-version', false, function(cb) {
// E.g., 2.1.1-50-g5cab646. See https://git-scm.com/docs/git-describe#_examples
packageVersion = exec('git describe --tags', {silent: true}).output.trim();
cb();
});
/**
* Updated versionpress.php to contain the current package version
*/
gulp.task('update-plugin-version', false, ['fill-vp-version', 'copy'], function(cb) {
var pluginFile = buildDir + '/versionpress.php';
fs.readFile(pluginFile, {encoding: 'UTF-8'}, function (err, content) {
if (err) {
return console.log(err);
}
var result = content.replace(/^Version: (.*)$/m, 'Version: ' + packageVersion);
fs.writeFile(pluginFile, result, 'utf8', function(err) {
if (err) {
return console.log(err);
}
cb();
});
});
});
/**
* Builds the final ZIP in the `distDir` folder.
*/
gulp.task('zip', false, ['copy', 'disable-debugger', 'remove-composer-files', 'fill-vp-version', 'update-plugin-version'], function (cb) {
return gulp.src(buildDir + '/**', {dot: true}).
pipe(rename(function (path) {
path.dirname = 'versionpress/' + path.dirname;
})).
pipe(zip('versionpress-' + packageVersion + '.zip')).
pipe(gulp.dest(distDir));
});
/**
* After the ZIP has been built, cleans the build directory
*/
gulp.task('clean-build', false, ['zip'], function (cb) {
return del(['build']);
});
/**
* Installs Composer external libs.
*/
gulp.task('composer-install-ext-libs', false, function () {
return composer('update', {cwd: './ext-libs', bin: 'composer', 'ignore-platform-reqs': true});
});
/**
* Installs Composer libs.
*/
gulp.task('composer-install-versionpress-libs', false, function () {
return composer({cwd: './plugins/versionpress', bin: 'composer', 'ignore-platform-reqs': true});
});
/**
* Inits the tests.
*/
gulp.task('init-tests', false, shell.task([
'npm install'
], {cwd: vpDir + '/tests'}));
/**
* Inits the frontend project.
*/
gulp.task('init-frontend', false, function () {
var configPath = frontendDir + '/src/config.local.sample.ts';
var targetName = configPath.replace('.sample', '');
if (fs.existsSync(targetName)) {
targetName = configPath;
}
return gulp.src(configPath)
.pipe(rename(targetName))
.pipe(gulp.dest('.'))
.pipe(shell([
'npm install'
], {cwd: frontendDir}));
});
/**
* Sets git to be case sensitive.
*/
gulp.task('git-config', false, function (cb) {
return git.exec({args: 'config core.ignorecase false'}, function (err, stdout) {
if (err) {
console.log(err);
}
cb();
});
});
//--------------------------------------
// Main callable tasks
//--------------------------------------
gulp.task('default', false, ['help'], function () {
});
/**
* Task that exports production build
*/
gulp.task('build', 'Task that exports production build', ['clean-build'], function () {
console.log(" ");
console.log("Build ready: " + chalk.white.bold.bgGreen("versionpress-" + packageVersion + ".zip"));
console.log(" ");
});
/**
* Task called from WpAutomation to copy the plugin files to the test directory
* specified in `test-config.yml`. Basically does only the `copy` task.
*/
gulp.task('test-deploy', 'Task called from WpAutomation to copy the plugin files to the test directory.', ['prepare-test-deploy', 'copy']);
/**
* Inits dev environment.
* Install vendors, set env variables.
*/
gulp.task('init-dev', 'Inits user development environment.', ['git-config', 'composer-install-ext-libs', 'composer-install-versionpress-libs', 'frontend-build-and-deploy', 'init-tests']);
//--------------------------------------
// IDE tasks
//--------------------------------------
/**
* Setup project files for IDEA / PhpStorm
*/
gulp.task('idea', 'Setup project files for IDEA / PhpStorm', function () {
var ideaProjects = [
{src: './.ide-tpl/.idea-versionpress/**', dest: vpDir + '/.idea'},
{src: './.ide-tpl/.idea-frontend/**', dest: frontendDir + '/.idea'}
];
var streams = ideaProjects.map(function (project) {
return gulp.src(project.src, {dot: true}).pipe(gulp.dest(project.dest));
});
return merge.apply(null, streams);
});
//--------------------------------------
// Helper functions
//--------------------------------------
function isRelative(sitePath) {
return path.normalize(sitePath) != path.resolve(sitePath);
}