-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgulpfile.js
More file actions
262 lines (231 loc) · 7.87 KB
/
gulpfile.js
File metadata and controls
262 lines (231 loc) · 7.87 KB
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
import gulp from 'gulp';
import webpack from "webpack";
import webpackStream from "webpack-stream";
import WebpackDevServer from "webpack-dev-server";
import config from "./js/webpack-config.js";
import translateConfig from "./js/webpack-config-translation.js";
import {deleteSync} from "del";
import zip from "gulp-zip";
import {find, sortBy, identity, values, mapObjIndexed, append, prepend, map, flatten} from "ramda";
import fs from "fs";
import path from "path";
import merge from "merge-stream";
import replace from "gulp-replace";
import {exec} from "child_process";
import rename from "gulp-rename";
import license from "gulp-license-check";
function getFolders(dir) {
return flatten(fs
.readdirSync(dir)
.filter(file => fs.statSync(path.join(dir, file)).isDirectory())
.map(file => {
const subdirs = getFolders(dir+'/'+file);
return prepend(dir+'/'+file, subdirs);
})
);
}
function getLatestCommitHash(cb) {
exec('git log -n 1 --pretty=format:"%h"', (err, stdout, stderr) => {
if (err || !stdout) {
throw new Error(err);
}
cb(stdout);
});
}
function getVersion() {
var raw = fs.readFileSync('php/revws.php', 'utf8');
var lines = map(line => line.replace(/\s+/g, ""), raw.split("\n"));
var line = find(line => line.indexOf("$this->version") === 0, lines);
if (line) {
return line.replace(/[^0-9\.]+/g, '');
}
throw new Error("version not found");
}
function getFileVersion() {
return getVersion().replace(/\./g, '_');
}
function getBootstrapJs() {
return 'revws-bootstrap-' + getFileVersion() + '.js';
}
function readTranslations(path) {
var keys = JSON.parse(fs.readFileSync(path, 'utf8'));
return sortBy(identity, values(mapObjIndexed((value, key) => {
return '"' + key + '" => $this->l(\'' + value.replace(/'/g, "\\'") +'\')';
}, keys)));
}
gulp.task("devel", function() {
const compiler = webpack(config(false, getVersion()));
return new WebpackDevServer( {
hot: true,
port: 8080,
allowedHosts: "all"
}, compiler).start();
});
gulp.task('clean', function(done) {
deleteSync(['./build'], { force: true });
done();
});
gulp.task('build-javascript', function() {
process.env.NODE_ENV = 'production';
return gulp.src('js')
.pipe(webpackStream(config(true, getVersion()), webpack))
.pipe(gulp.dest('./build'));
});
gulp.task('copy-javascript', function(done) {
gulp.src('./build/*.js')
.pipe(gulp.dest('./build/staging/revws/views/js'))
.on('end', done);
});
gulp.task('create-zip', function(done) {
var fileVersion = getFileVersion();
return gulp
.src('./build/staging/**/*')
.pipe(zip('revws-'+fileVersion+'.zip'))
.pipe(gulp.dest('./build'));
});
gulp.task('copy-text-files', function(done) {
getLatestCommitHash(commit => {
const version = getVersion();
const bootstrapJs = getBootstrapJs();
const ext = ['php', 'tpl', 'js', 'sql', 'html', 'md', 'txt'];
let sources = map(e => 'php/**/*.'+e, ext);
sources = append('php/**/css/back.*css', sources);
sources = append('php/**/css/fallback.*css', sources);
sources = append('php/**/css/themes/*.css', sources);
sources = append('!php/license-header.*', sources);
sources = append('!php/**/revws_bootstrap.js', sources);
return gulp
.src(sources)
.pipe(replace(/CACHE_CONTROL/g, commit))
.pipe(replace(/CONSTANT_VERSION/g, version))
.pipe(replace(/revws_bootstrap.js/g, bootstrapJs))
.pipe(gulp.dest('./build/staging/revws'))
.on('end', done);
});
});
gulp.task('copy-bootstrap-js', function(done) {
return gulp
.src(['php/**/revws_bootstrap.js'])
.pipe(rename(getBootstrapJs()))
.pipe(gulp.dest('./build/staging/revws/views/js/'))
.on('end', done);
});
gulp.task('copy-binary-files', function(done) {
const ext = ['png', 'svg', 'jpg'];
const sources = [];
for (var i=0; i<ext.length; i++) {
const e = ext[i];
sources.push('php/**/*.'+e);
sources.push('!php/data/**/*.'+e);
}
return gulp
.src(sources)
.pipe(gulp.dest('./build/staging/revws'))
.on('end', done);
});
gulp.task('copy-build', function(done) {
const ver = getFileVersion();
return gulp
.src(['./build/front-'+ver+'.js', './build/back-'+ver+'.js'])
.pipe(gulp.dest('./build/staging/revws/views/js'))
.on('end', done);
});
gulp.task('create-index', function(done) {
if (!fs.existsSync('./build/staging/revws/translations')){
fs.mkdirSync('./build/staging/revws/translations');
}
if (!fs.existsSync('./build/staging/revws/data')) {
fs.mkdirSync('./build/staging/revws/data');
}
if (!fs.existsSync('./build/staging/revws/data/images')) {
fs.mkdirSync('./build/staging/revws/data/images');
}
var folders = getFolders('./build/staging/revws');
var tasks = folders.map(folder => gulp.src('php/index.php').pipe(gulp.dest(folder)));
return merge(tasks);
});
gulp.task('create-config-xml', function(done) {
const version = getVersion();
return gulp.src(['php/config.xml.src'])
.pipe(replace(/CONSTANT_VERSION/g, version))
.pipe(rename('config.xml'))
.pipe(gulp.dest('./build/staging/revws'))
.on('end', done);
});
gulp.task('extract-front-translations', function(done) {
process.env.NODE_ENV = 'production';
gulp.src('js')
.pipe(webpackStream(translateConfig('front'), webpack))
.pipe(gulp.dest('./build'))
.on('end', () => {
deleteSync(['./build/transl.js', './build/transl.js.LICENSE.txt'], { force: true });
done();
});
});
gulp.task('extract-back-translations', function(done) {
process.env.NODE_ENV = 'production';
gulp.src('js')
.pipe(webpackStream(translateConfig('back'), webpack))
.pipe(gulp.dest('./build'))
.on('end', () => {
deleteSync(['./build/transl.js', './build/transl.js.LICENSE.txt'], { force: true });
done();
});
});
gulp.task('create-translations', function(done) {
const frontTranslations = readTranslations('./build/front-translation-keys.json');
const backTranslations = readTranslations('./build/back-translation-keys.json');
gulp
.src('php/app-translation.php')
.pipe(replace(/([ ]*)\/\/ FRONT_TRANSLATIONS/, (_, spaces) => {
return spaces + frontTranslations.join(",\n"+spaces);
}))
.pipe(replace(/([ ]*)\/\/ BACK_TRANSLATIONS/, (_, spaces) => {
return spaces + backTranslations.join(",\n"+spaces);
}))
.pipe(gulp.dest('./build/staging/revws/'))
.on('end', done);
});
gulp.task('merge-translations-keys', function(done) {
exec('php ./utils/merge-keys.php', (err, stdout, stderr) => {
if (err) {
throw new Error(err);
}
console.log(stdout.replace(/\s$/g, ""));
done();
});
});
gulp.task('translate', gulp.series('extract-front-translations', 'extract-back-translations', 'create-translations', 'merge-translations-keys'));
gulp.task('stage', gulp.series('copy-text-files', 'copy-binary-files', 'copy-bootstrap-js', 'copy-build', 'create-index', 'create-config-xml', 'translate'));
gulp.task('upload', function(done) {
const file = path.resolve('./build/revws-'+getFileVersion()+'.zip');
exec('release-module '+file+' revws free', (err, stdout, stderr) => {
if (err) {
throw new Error(err);
}
console.log(stdout);
done();
});
});
gulp.task('check-license-php', function() {
return gulp
.src(['php/**/*.php', '!php/translations/**'])
.pipe(license({
path: 'php/license-header.php',
blocking: true,
logInfo: false
}));
});
gulp.task('check-license-tpl', function(done) {
return gulp
.src(['php/**/*.tpl'])
.pipe(license({
path: 'php/license-header.tpl',
blocking: true,
logInfo: false
}));
});
gulp.task('check-license', gulp.series('check-license-php', 'check-license-tpl'));
gulp.task('build', gulp.series('clean', 'build-javascript', 'copy-javascript'));
gulp.task('release', gulp.series('clean', 'check-license', 'build-javascript', 'copy-javascript', 'stage', 'create-zip'));
gulp.task('default', gulp.series('devel'));