forked from bueltge/multisite-global-media
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
275 lines (246 loc) · 6.29 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
const gulp = require('gulp')
const gulpPhpUnit = require('gulp-phpunit')
const gulpZip = require('gulp-zip')
const gulpDel = require('del')
const minimist = require('minimist')
const fs = require('fs')
const pump = require('pump')
const usage = require('gulp-help-doc')
const { exec } = require('child_process')
const ENV_PRODUCTION = 'production'
const ENV_DEVELOPMENT = 'development'
const PACKAGE_NAME = 'multisite-global-media'
const BASE_PATH = './'
const TMP_DESTINATION_PATH = './dist'
const PACKAGE_PATH = `${TMP_DESTINATION_PATH}/${PACKAGE_NAME}`
const options = minimist(
process.argv.slice(2),
{
string: [
'distVersion',
'destination',
],
default: {
destination: process.destination || BASE_PATH,
},
}
)
function setupCheckPackageVersion ({ distVersion }) {
return function checkPackageVersion (done) {
return new Promise((resolve, reject) => {
if (distVersion) {
done()
}
reject('Missing --version option with a semver value.')
})
}
}
function setupComposer ({ environment, basePath }) {
let parameters = ''
if (environment === ENV_PRODUCTION) {
parameters = `--prefer-dist --optimize-autoloader --classmap-authoritative --no-dev --working-dir=${basePath}`
}
return function composer (done) {
return exec(
`composer install ${parameters}`,
(error, stdout, stderr) => {
if (error) {
throw new Error(error)
}
done()
},
)
}
}
function setupPhpunit () {
return function phpunit (done) {
return exec(
'./vendor/bin/phpunit --testdox',
(error, stdout, stderr) => {
if (error) {
throw new Error(error)
}
done()
}
)
}
}
function setupPhpcs () {
return function phpcs (done) {
return exec(
'./vendor/bin/phpcs',
(error, stdout, stderr) => {
if (error) {
throw new Error(error)
}
done()
}
)
}
}
function setupPsalm() {
return function psalm(done) {
return exec(
'./vendor/bin/psalm',
(error, stdout, stderr) => {
if (error) {
throw new Error(error)
}
done()
}
)
}
}
function setupCopyFiles ({ sources, destination, basePath }) {
return function copyPackageFiles (done) {
return new Promise(() => {
pump(
gulp.src(
sources,
{
base: basePath,
}
),
gulp.dest(destination),
done,
)
})
}
}
function deleteTemporaryFiles () {
if (!fs.existsSync(TMP_DESTINATION_PATH)) {
throw new Error(`Cannot create package, ${TMP_DESTINATION_PATH} doesn't exists.`)
}
gulpDel.sync(
[
`${TMP_DESTINATION_PATH}/**/.gitignore`,
`${TMP_DESTINATION_PATH}/**/.gitattributes`,
`${TMP_DESTINATION_PATH}/**/.travis.yml`,
`${TMP_DESTINATION_PATH}/**/.scrutinizer.yml`,
`${TMP_DESTINATION_PATH}/**/.gitattributes`,
`${TMP_DESTINATION_PATH}/**/.git`,
`${TMP_DESTINATION_PATH}/**/changelog.txt`,
`${TMP_DESTINATION_PATH}/**/changelog.md`,
`${TMP_DESTINATION_PATH}/**/CHANGELOG.md`,
`${TMP_DESTINATION_PATH}/**/CHANGELOG`,
`${TMP_DESTINATION_PATH}/**/README`,
`${TMP_DESTINATION_PATH}/**/README.md`,
`!${PACKAGE_PATH}/README.md`,
`${TMP_DESTINATION_PATH}/**/readme.md`,
`!${PACKAGE_PATH}/readme.md`,
`${TMP_DESTINATION_PATH}/**/readme.txt`,
`${TMP_DESTINATION_PATH}/**/CONTRIBUTING.md`,
`${TMP_DESTINATION_PATH}/**/CONTRIBUTING`,
`${TMP_DESTINATION_PATH}/**/composer.json`,
`${TMP_DESTINATION_PATH}/**/composer.lock`,
`${TMP_DESTINATION_PATH}/**/phpcs.xml`,
`${TMP_DESTINATION_PATH}/**/phpcs.xml.dist`,
`${TMP_DESTINATION_PATH}/**/phpunit.xml`,
`${TMP_DESTINATION_PATH}/**/phpunit.xml.dist`,
`${TMP_DESTINATION_PATH}/**/bitbucket-pipelines.yml`,
`${TMP_DESTINATION_PATH}/**/test`,
`${TMP_DESTINATION_PATH}/**/tests`,
`${TMP_DESTINATION_PATH}/**/bin`,
`${TMP_DESTINATION_PATH}/**/Dockerfile`,
`${TMP_DESTINATION_PATH}/**/Makefile`,
],
)
}
function setupDestinationPackage ({ distVersion, destination, basePath }) {
return function compressPackage (done) {
const timeStamp = new Date().getTime()
deleteTemporaryFiles()
return new Promise(() => {
exec(
`git log -n 1 | head -n 1 | sed -e 's/^commit //' | head -c 8`,
{},
(error, stdout) => {
const shortHash = error ? timeStamp : stdout
pump(
gulp.src(`${TMP_DESTINATION_PATH}/**/*`, {
base: TMP_DESTINATION_PATH,
}),
gulpZip(`${PACKAGE_NAME}-${distVersion}-${shortHash}.zip`),
gulp.dest(
destination,
{
base: TMP_DESTINATION_PATH,
cwd: basePath,
},
),
done,
)
},
)
})
}
}
function setupCleanDist () {
return async function cleanDist () {
await gulpDel(TMP_DESTINATION_PATH)
}
}
function help () {
return usage(gulp)
}
const cleanDist = setupCleanDist()
const testsTask = gulp.series(
setupPhpunit(),
setupPhpcs(),
setupPsalm()
)
/**
* Create the plugin package distribution.
*
* @task {dist}
* @arg {distVersion} Package version, the version must to be conformed to semver.
* @arg {destination} Where the resulting package zip have to be stored.
*/
exports.dist = gulp.series(
setupCheckPackageVersion({
...options,
environment: ENV_PRODUCTION
}),
cleanDist,
setupCopyFiles({
...options,
environment: ENV_PRODUCTION,
basePath: BASE_PATH,
sources: [
'./assets/**/*',
'./src/**/*',
'./multisite-global-media.php',
'./LICENSE',
'./composer.json',
'./composer.lock',
'./readme.md'
],
destination: PACKAGE_PATH,
}),
setupComposer({
...options,
basePath: PACKAGE_PATH,
environment: ENV_PRODUCTION
}),
setupDestinationPackage({
...options,
environment: ENV_PRODUCTION
}),
cleanDist
)
/**
* Setup the Development Environment, usually for the first time
*
* @task {setup}
*/
exports.setup = gulp.series(
setupComposer({ environment: ENV_DEVELOPMENT }),
)
exports.help = help
exports.default = help
/**
* Run Tests
*
* @task {tests}
*/
exports.tests = testsTask