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');
12 const sass = require("gulp-sass");
13 const cssnano = require("gulp-cssnano");
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));
23 const rename = require('gulp-rename');
25 const STAFF_JS_BASE = "koha-tmpl/intranet-tmpl/prog/js";
26 const STAFF_CSS_BASE = "koha-tmpl/intranet-tmpl/prog/css";
27 const OPAC_JS_BASE = "koha-tmpl/opac-tmpl/bootstrap/js";
28 const OPAC_CSS_BASE = "koha-tmpl/opac-tmpl/bootstrap/css";
30 if (args.view == "opac") {
31 var css_base = OPAC_CSS_BASE;
32 var js_base = OPAC_JS_BASE;
34 var css_base = STAFF_CSS_BASE;
35 var js_base = STAFF_JS_BASE;
39 errLogToConsole: true,
43 // CSS processing for development
45 var stream = src(css_base + "/src/**/*.scss")
46 .pipe(sourcemaps.init())
47 .pipe(sass(sassOptions).on('error', sass.logError))
49 .pipe(dest(css_base));
51 if (args.view == "opac") {
56 })) // Append "-rtl" to the filename.
57 .pipe(dest(css_base));
60 stream = stream.pipe(sourcemaps.write('./maps'))
61 .pipe(dest(css_base));
67 // CSS processing for production
69 var stream = src(css_base + "/src/**/*.scss")
70 .pipe(sass(sassOptions).on('error', sass.logError))
75 .pipe(dest(css_base));
77 if( args.view == "opac" ){
78 stream = stream.pipe(rtlcss())
81 })) // Append "-rtl" to the filename.
82 .pipe(dest(css_base));
90 extract: po_extract_marc_marc21,
91 create: po_create_marc_marc21,
92 update: po_update_marc_marc21,
95 extract: po_extract_marc_unimarc,
96 create: po_create_marc_unimarc,
97 update: po_update_marc_unimarc,
100 extract: po_extract_staff,
101 create: po_create_staff,
102 update: po_update_staff,
105 extract: po_extract_opac,
106 create: po_create_opac,
107 update: po_update_opac,
110 extract: po_extract_pref,
111 create: po_create_pref,
112 update: po_update_pref,
115 extract: po_extract_messages,
116 create: po_create_messages,
117 update: po_update_messages,
120 extract: po_extract_messages_js,
121 create: po_create_messages_js,
122 update: po_update_messages_js,
125 extract: po_extract_installer,
126 create: po_create_installer,
127 update: po_update_installer,
129 'installer-MARC21': {
130 extract: po_extract_installer_marc21,
131 create: po_create_installer_marc21,
132 update: po_update_installer_marc21,
136 const poTypes = Object.keys(poTasks);
138 function po_extract_marc (type) {
139 return src(`koha-tmpl/*-tmpl/*/en/**/*${type}*`, { read: false, nocase: true })
140 .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', `Koha-marc-${type}.pot`))
141 .pipe(dest('misc/translator'))
144 function po_extract_marc_marc21 () { return po_extract_marc('MARC21') }
145 function po_extract_marc_unimarc () { return po_extract_marc('UNIMARC') }
147 function po_extract_staff () {
149 'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
150 'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
151 'koha-tmpl/intranet-tmpl/prog/en/xslt/*.xsl',
152 '!koha-tmpl/intranet-tmpl/prog/en/**/*MARC21*',
153 '!koha-tmpl/intranet-tmpl/prog/en/**/*UNIMARC*',
154 '!koha-tmpl/intranet-tmpl/prog/en/**/*marc21*',
155 '!koha-tmpl/intranet-tmpl/prog/en/**/*unimarc*',
158 return src(globs, { read: false, nocase: true })
159 .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', 'Koha-staff-prog.pot'))
160 .pipe(dest('misc/translator'))
163 function po_extract_opac () {
165 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
166 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
167 'koha-tmpl/opac-tmpl/bootstrap/en/xslt/*.xsl',
168 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*MARC21*',
169 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*UNIMARC*',
170 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*marc21*',
171 '!koha-tmpl/opac-tmpl/bootstrap/en/**/*unimarc*',
174 return src(globs, { read: false, nocase: true })
175 .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', 'Koha-opac-bootstrap.pot'))
176 .pipe(dest('misc/translator'))
179 const xgettext_options = '--from-code=UTF-8 --package-name Koha '
180 + '--package-version= -k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 '
181 + '-k__p:1c,2 -k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ '
182 + '-kN__n:1,2 -kN__p:1c,2 -kN__np:1c,2,3 --force-po';
184 function po_extract_messages_js () {
186 'koha-tmpl/intranet-tmpl/prog/js/**/*.js',
187 'koha-tmpl/opac-tmpl/bootstrap/js/**/*.js',
190 return src(globs, { read: false, nocase: true })
191 .pipe(xgettext(`xgettext -L JavaScript ${xgettext_options}`, 'Koha-messages-js.pot'))
192 .pipe(dest('misc/translator'))
195 function po_extract_messages () {
196 const perlStream = src(['**/*.pl', '**/*.pm'], { read: false, nocase: true })
197 .pipe(xgettext(`xgettext -L Perl ${xgettext_options}`, 'Koha-perl.pot'))
199 const ttStream = src([
200 'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
201 'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
202 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
203 'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
204 ], { read: false, nocase: true })
205 .pipe(xgettext('misc/translator/xgettext-tt2 --from-code=UTF-8', 'Koha-tt.pot'))
208 'Project-Id-Version': 'Koha',
209 'Content-Type': 'text/plain; charset=UTF-8',
212 return merge(perlStream, ttStream)
213 .pipe(concatPo('Koha-messages.pot', { headers }))
214 .pipe(dest('misc/translator'))
217 function po_extract_pref () {
218 return src('koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/*.pref', { read: false })
219 .pipe(xgettext('misc/translator/xgettext-pref', 'Koha-pref.pot'))
220 .pipe(dest('misc/translator'))
223 function po_extract_installer () {
225 'installer/data/mysql/en/mandatory/*.yml',
226 'installer/data/mysql/en/optional/*.yml',
229 return src(globs, { read: false, nocase: true })
230 .pipe(xgettext('misc/translator/xgettext-installer', 'Koha-installer.pot'))
231 .pipe(dest('misc/translator'))
234 function po_extract_installer_marc (type) {
235 const globs = `installer/data/mysql/en/marcflavour/${type}/**/*.yml`;
237 return src(globs, { read: false, nocase: true })
238 .pipe(xgettext('misc/translator/xgettext-installer', `Koha-installer-${type}.pot`))
239 .pipe(dest('misc/translator'))
242 function po_extract_installer_marc21 () { return po_extract_installer_marc('MARC21') }
244 function po_create_type (type) {
245 const access = util.promisify(fs.access);
246 const exec = util.promisify(child_process.exec);
248 const languages = getLanguages();
250 for (const language of languages) {
251 const locale = language.split('-').filter(s => s.length !== 4).join('_');
252 const po = `misc/translator/po/${language}-${type}.po`;
253 const pot = `misc/translator/Koha-${type}.pot`;
255 const promise = access(po)
256 .catch(() => exec(`msginit -o ${po} -i ${pot} -l ${locale} --no-translator`))
257 promises.push(promise);
260 return Promise.all(promises);
263 function po_create_marc_marc21 () { return po_create_type('marc-MARC21') }
264 function po_create_marc_unimarc () { return po_create_type('marc-UNIMARC') }
265 function po_create_staff () { return po_create_type('staff-prog') }
266 function po_create_opac () { return po_create_type('opac-bootstrap') }
267 function po_create_pref () { return po_create_type('pref') }
268 function po_create_messages () { return po_create_type('messages') }
269 function po_create_messages_js () { return po_create_type('messages-js') }
270 function po_create_installer () { return po_create_type('installer') }
271 function po_create_installer_marc21 () { return po_create_type('installer-MARC21') }
273 function po_update_type (type) {
274 const msgmerge_opts = '--backup=off --quiet --sort-output --update';
275 const cmd = `msgmerge ${msgmerge_opts} <%= file.path %> misc/translator/Koha-${type}.pot`;
276 const languages = getLanguages();
277 const globs = languages.map(language => `misc/translator/po/${language}-${type}.po`);
280 .pipe(exec(cmd, { continueOnError: true }))
281 .pipe(exec.reporter({ err: false, stdout: false }))
284 function po_update_marc_marc21 () { return po_update_type('marc-MARC21') }
285 function po_update_marc_unimarc () { return po_update_type('marc-UNIMARC') }
286 function po_update_staff () { return po_update_type('staff-prog') }
287 function po_update_opac () { return po_update_type('opac-bootstrap') }
288 function po_update_pref () { return po_update_type('pref') }
289 function po_update_messages () { return po_update_type('messages') }
290 function po_update_messages_js () { return po_update_type('messages-js') }
291 function po_update_installer () { return po_update_type('installer') }
292 function po_update_installer_marc21 () { return po_update_type('installer-MARC21') }
295 * Gulp plugin that executes xgettext-like command `cmd` on all files given as
296 * input, and then outputs the result as a POT file named `filename`.
297 * `cmd` should accept -o and -f options
299 function xgettext (cmd, filename) {
300 const filenames = [];
302 function transform (file, encoding, callback) {
303 filenames.push(path.relative(file.cwd, file.path));
307 function flush (callback) {
308 fs.mkdtemp(path.join(os.tmpdir(), 'koha-'), (err, folder) => {
309 const outputFilename = path.join(folder, filename);
310 const filesFilename = path.join(folder, 'files');
311 fs.writeFile(filesFilename, filenames.join(os.EOL), err => {
312 if (err) return callback(err);
314 const command = `${cmd} -o ${outputFilename} -f ${filesFilename}`;
315 child_process.exec(command, err => {
316 if (err) return callback(err);
318 fs.readFile(outputFilename, (err, data) => {
319 if (err) return callback(err);
321 const file = new Vinyl();
322 file.path = path.join(file.base, filename);
323 file.contents = data;
324 callback(null, file);
331 return through2.obj(transform, flush);
335 * Return languages selected for PO-related tasks
337 * This can be either languages given on command-line with --lang option, or
338 * all the languages found in misc/translator/po otherwise
340 function getLanguages () {
341 if (Array.isArray(args.lang)) {
349 const filenames = fs.readdirSync('misc/translator/po')
350 .filter(filename => filename.endsWith('.po'))
351 .filter(filename => !filename.startsWith('.'))
353 const re = new RegExp('-(' + poTypes.join('|') + ')\.po$');
354 languages = filenames.map(filename => filename.replace(re, ''))
356 return Array.from(new Set(languages));
359 exports.build = build;
362 exports['po:create'] = parallel(...poTypes.map(type => series(poTasks[type].extract, poTasks[type].create)));
363 exports['po:update'] = parallel(...poTypes.map(type => series(poTasks[type].extract, poTasks[type].update)));
364 exports['po:extract'] = parallel(...poTypes.map(type => poTasks[type].extract));
366 exports.default = function () {
367 watch(css_base + "/src/**/*.scss", series('css'));