2 /* eslint no-console:"off" */
4 const { dest, parallel, series, src, watch } = require('gulp');
6 const child_process = require('child_process');
7 const fs = require('fs');
8 const os = require('os');
9 const path = require('path');
10 const util = require('util');
11 const stream = require('stream/promises');
13 const sass = require('gulp-sass')(require('sass'));
14 const rtlcss = require('gulp-rtlcss');
15 const sourcemaps = require('gulp-sourcemaps');
16 const autoprefixer = require('gulp-autoprefixer');
17 const concatPo = require('gulp-concat-po');
18 const exec = require('gulp-exec');
19 const merge = require('merge-stream');
20 const through2 = require('through2');
21 const Vinyl = require('vinyl');
22 const args = require('minimist')(process.argv.slice(2), { default: { 'generate-pot': 'always' } });
23 const rename = require('gulp-rename');
25 const STAFF_CSS_BASE = "koha-tmpl/intranet-tmpl/prog/css";
26 const OPAC_CSS_BASE = "koha-tmpl/opac-tmpl/bootstrap/css";
28 var CSS_BASE = args.view == "opac"
34 __dirname + '/node_modules',
35 __dirname + '/../node_modules'
39 // CSS processing for development
40 function css(css_base) {
41 css_base = css_base || CSS_BASE
42 var stream = src(css_base + "/src/**/*.scss")
43 .pipe(sourcemaps.init())
44 .pipe(sass(sassOptions).on('error', sass.logError))
46 .pipe(dest(css_base));
48 if (args.view == "opac") {
53 })) // Append "-rtl" to the filename.
54 .pipe(dest(css_base));
57 stream = stream.pipe(sourcemaps.write('./maps'))
58 .pipe(dest(css_base));
63 // CSS processing for production
64 function build(css_base) {
65 css_base = css_base || CSS_BASE;
66 sassOptions.outputStyle = "compressed";
67 var stream = src(css_base + "/src/**/*.scss")
68 .pipe(sass(sassOptions).on('error', sass.logError))
70 .pipe(dest(css_base));
72 if( args.view == "opac" ){
73 stream = stream.pipe(rtlcss())
76 })) // Append "-rtl" to the filename.
77 .pipe(dest(css_base));
84 return css(OPAC_CSS_BASE);
88 return css(STAFF_CSS_BASE);
93 extract: po_extract_marc_marc21,
94 create: po_create_marc_marc21,
95 update: po_update_marc_marc21,
98 extract: po_extract_marc_unimarc,
99 create: po_create_marc_unimarc,
100 update: po_update_marc_unimarc,
103 extract: po_extract_staff,
104 create: po_create_staff,
105 update: po_update_staff,
108 extract: po_extract_opac,
109 create: po_create_opac,
110 update: po_update_opac,
113 extract: po_extract_pref,
114 create: po_create_pref,
115 update: po_update_pref,
118 extract: po_extract_messages,
119 create: po_create_messages,
120 update: po_update_messages,
123 extract: po_extract_messages_js,
124 create: po_create_messages_js,
125 update: po_update_messages_js,
128 extract: po_extract_installer,
129 create: po_create_installer,
130 update: po_update_installer,
132 'installer-MARC21': {
133 extract: po_extract_installer_marc21,
134 create: po_create_installer_marc21,
135 update: po_update_installer_marc21,
137 'installer-UNIMARC': {
138 extract: po_extract_installer_unimarc,
139 create: po_create_installer_unimarc,
140 update: po_update_installer_unimarc,
144 const poTypes = Object.keys(poTasks);
146 function po_extract_marc (type) {
147 return src(`koha-tmpl/*-tmpl/*/en/**/*${type}*`, { read: false, nocase: true })
148 .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -F', `Koha-marc-${type}.pot`))
149 .pipe(dest('misc/translator'))
152 function po_extract_marc_marc21 () { return po_extract_marc('MARC21') }
153 function po_extract_marc_unimarc () { return po_extract_marc('UNIMARC') }
155 function po_extract_staff () {
157 'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
158 'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
159 'koha-tmpl/intranet-tmpl/prog/en/xslt/*.xsl',
160 '!koha-tmpl/intranet-tmpl/prog/en/**/*MARC21*',
161 '!koha-tmpl/intranet-tmpl/prog/en/**/*UNIMARC*',
162 '!koha-tmpl/intranet-tmpl/prog/en/**/*marc21*',
163 '!koha-tmpl/intranet-tmpl/prog/en/**/*unimarc*',
166 return src(globs, { read: false, nocase: true })
167 .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -F', 'Koha-staff-prog.pot'))
168 .pipe(dest('misc/translator'))
171 function po_extract_opac () {
173 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
174 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
175 'koha-tmpl/opac-tmpl/bootstrap/en/xslt/*.xsl',
176 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*MARC21*',
177 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*UNIMARC*',
178 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*marc21*',
179 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*unimarc*',
182 return src(globs, { read: false, nocase: true })
183 .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -F', 'Koha-opac-bootstrap.pot'))
184 .pipe(dest('misc/translator'))
187 const xgettext_options = '--from-code=UTF-8 --package-name Koha '
188 + '--package-version= -k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 '
189 + '-k__p:1c,2 -k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ '
190 + '-kN__n:1,2 -kN__p:1c,2 -kN__np:1c,2,3 '
191 + '-k -k$__ -k$__x -k$__n:1,2 -k$__nx:1,2 -k$__xn:1,2 '
194 function po_extract_messages_js () {
196 'koha-tmpl/intranet-tmpl/prog/js/vue/**/*.vue',
197 'koha-tmpl/intranet-tmpl/prog/js/**/*.js',
198 'koha-tmpl/opac-tmpl/bootstrap/js/**/*.js',
201 return src(globs, { read: false, nocase: true })
202 .pipe(xgettext(`xgettext -L JavaScript ${xgettext_options}`, 'Koha-messages-js.pot'))
203 .pipe(dest('misc/translator'))
206 function po_extract_messages () {
207 const perlStream = src(['**/*.pl', '**/*.pm'], { read: false, nocase: true })
208 .pipe(xgettext(`xgettext -L Perl ${xgettext_options}`, 'Koha-perl.pot'))
210 const ttStream = src([
211 'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
212 'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
213 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
214 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
215 ], { read: false, nocase: true })
216 .pipe(xgettext('misc/translator/xgettext-tt2 --from-code=UTF-8', 'Koha-tt.pot'))
219 'Project-Id-Version': 'Koha',
220 'Content-Type': 'text/plain; charset=UTF-8',
223 return merge(perlStream, ttStream)
224 .pipe(concatPo('Koha-messages.pot', { headers }))
225 .pipe(dest('misc/translator'))
228 function po_extract_pref () {
229 return src('koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/*.pref', { read: false })
230 .pipe(xgettext('misc/translator/xgettext-pref', 'Koha-pref.pot'))
231 .pipe(dest('misc/translator'))
234 function po_extract_installer () {
236 'installer/data/mysql/en/mandatory/*.yml',
237 'installer/data/mysql/en/optional/*.yml',
240 return src(globs, { read: false, nocase: true })
241 .pipe(xgettext('misc/translator/xgettext-installer', 'Koha-installer.pot'))
242 .pipe(dest('misc/translator'))
245 function po_extract_installer_marc (type) {
246 const globs = `installer/data/mysql/en/marcflavour/${type}/**/*.yml`;
248 return src(globs, { read: false, nocase: true })
249 .pipe(xgettext('misc/translator/xgettext-installer', `Koha-installer-${type}.pot`))
250 .pipe(dest('misc/translator'))
253 function po_extract_installer_marc21 () { return po_extract_installer_marc('MARC21') }
255 function po_extract_installer_unimarc () { return po_extract_installer_marc('UNIMARC') }
257 function po_create_type (type) {
258 const access = util.promisify(fs.access);
259 const exec = util.promisify(child_process.exec);
261 const pot = `misc/translator/Koha-${type}.pot`;
263 // Generate .pot only if it doesn't exist or --force-extract is given
264 const extract = () => stream.finished(poTasks[type].extract());
266 args['generate-pot'] === 'always' ? extract() :
267 args['generate-pot'] === 'auto' ? access(pot).catch(extract) :
268 args['generate-pot'] === 'never' ? Promise.resolve(0) :
269 Promise.reject(new Error('Invalid value for option --generate-pot: ' + args['generate-pot']))
271 return p.then(function () {
272 const languages = getLanguages();
274 for (const language of languages) {
275 const locale = language.split('-').filter(s => s.length !== 4).join('_');
276 const po = `misc/translator/po/${language}-${type}.po`;
278 const promise = access(po)
279 .catch(() => exec(`msginit -o ${po} -i ${pot} -l ${locale} --no-translator`))
280 promises.push(promise);
283 return Promise.all(promises);
287 function po_create_marc_marc21 () { return po_create_type('marc-MARC21') }
288 function po_create_marc_unimarc () { return po_create_type('marc-UNIMARC') }
289 function po_create_staff () { return po_create_type('staff-prog') }
290 function po_create_opac () { return po_create_type('opac-bootstrap') }
291 function po_create_pref () { return po_create_type('pref') }
292 function po_create_messages () { return po_create_type('messages') }
293 function po_create_messages_js () { return po_create_type('messages-js') }
294 function po_create_installer () { return po_create_type('installer') }
295 function po_create_installer_marc21 () { return po_create_type('installer-MARC21') }
296 function po_create_installer_unimarc () { return po_create_type('installer-UNIMARC') }
298 function po_update_type (type) {
299 const access = util.promisify(fs.access);
300 const exec = util.promisify(child_process.exec);
302 const pot = `misc/translator/Koha-${type}.pot`;
304 // Generate .pot only if it doesn't exist or --force-extract is given
305 const extract = () => stream.finished(poTasks[type].extract());
307 args['generate-pot'] === 'always' ? extract() :
308 args['generate-pot'] === 'auto' ? access(pot).catch(extract) :
309 args['generate-pot'] === 'never' ? Promise.resolve(0) :
310 Promise.reject(new Error('Invalid value for option --generate-pot: ' + args['generate-pot']))
312 return p.then(function () {
313 const languages = getLanguages();
315 for (const language of languages) {
316 const po = `misc/translator/po/${language}-${type}.po`;
317 promises.push(exec(`msgmerge --backup=off --no-wrap --quiet -F --update ${po} ${pot}`));
320 return Promise.all(promises);
324 function po_update_marc_marc21 () { return po_update_type('marc-MARC21') }
325 function po_update_marc_unimarc () { return po_update_type('marc-UNIMARC') }
326 function po_update_staff () { return po_update_type('staff-prog') }
327 function po_update_opac () { return po_update_type('opac-bootstrap') }
328 function po_update_pref () { return po_update_type('pref') }
329 function po_update_messages () { return po_update_type('messages') }
330 function po_update_messages_js () { return po_update_type('messages-js') }
331 function po_update_installer () { return po_update_type('installer') }
332 function po_update_installer_marc21 () { return po_update_type('installer-MARC21') }
333 function po_update_installer_unimarc () { return po_update_type('installer-UNIMARC') }
336 * Gulp plugin that executes xgettext-like command `cmd` on all files given as
337 * input, and then outputs the result as a POT file named `filename`.
338 * `cmd` should accept -o and -f options
340 function xgettext (cmd, filename) {
341 const filenames = [];
343 function transform (file, encoding, callback) {
344 filenames.push(path.relative(file.cwd, file.path));
348 function flush (callback) {
349 fs.mkdtemp(path.join(os.tmpdir(), 'koha-'), (err, folder) => {
350 const outputFilename = path.join(folder, filename);
351 const filesFilename = path.join(folder, 'files');
352 fs.writeFile(filesFilename, filenames.join(os.EOL), err => {
353 if (err) return callback(err);
355 const command = `${cmd} -o ${outputFilename} -f ${filesFilename}`;
356 child_process.exec(command, err => {
357 if (err) return callback(err);
359 fs.readFile(outputFilename, (err, data) => {
360 if (err) return callback(err);
362 const file = new Vinyl();
363 file.path = path.join(file.base, filename);
364 file.contents = data;
365 callback(null, file);
372 return through2.obj(transform, flush);
376 * Return languages selected for PO-related tasks
378 * This can be either languages given on command-line with --lang option, or
379 * all the languages found in misc/translator/po otherwise
381 function getLanguages () {
382 if (Array.isArray(args.lang)) {
390 const filenames = fs.readdirSync('misc/translator/po')
391 .filter(filename => filename.endsWith('.po'))
392 .filter(filename => !filename.startsWith('.'))
394 const re = new RegExp('-(' + poTypes.join('|') + ')\.po$');
395 languages = filenames.map(filename => filename.replace(re, ''))
397 return Array.from(new Set(languages));
400 exports.build = function(next){build(); next();};
401 exports.css = function(next){css(); next();};
402 exports.opac_css = opac_css;
403 exports.staff_css = staff_css;
404 exports.watch = function () {
405 watch(OPAC_CSS_BASE + "/src/**/*.scss", series('opac_css'));
406 watch(STAFF_CSS_BASE + "/src/**/*.scss", series('staff_css'));
409 exports['po:create'] = parallel(...poTypes.map(type => poTasks[type].create));
410 exports['po:update'] = parallel(...poTypes.map(type => poTasks[type].update));
411 exports['po:extract'] = parallel(...poTypes.map(type => poTasks[type].extract));