View | Details | Raw Unified | Return to bug 37472
Collapse All | Expand All

(-)a/gulpfile.js (-2 / +167 lines)
Lines 163-169 function getPoTasks () { Link Here
163
163
164
    return tasks;
164
    return tasks;
165
}
165
}
166
const poTypes = getPoTasks();
167
166
168
function po_extract_marc (type) {
167
function po_extract_marc (type) {
169
    return src(`koha-tmpl/*-tmpl/*/en/**/*${type}*`, { read: false, nocase: true })
168
    return src(`koha-tmpl/*-tmpl/*/en/**/*${type}*`, { read: false, nocase: true })
Lines 354-359 function po_update_installer () { return po_update_type('installer') } Link Here
354
function po_update_installer_marc21 ()  { return po_update_type('installer-MARC21') }
353
function po_update_installer_marc21 ()  { return po_update_type('installer-MARC21') }
355
function po_update_installer_unimarc () { return po_update_type('installer-UNIMARC') }
354
function po_update_installer_unimarc () { return po_update_type('installer-UNIMARC') }
356
355
356
const PLUGINS_BASE = "/kohadevbox/plugins";
357
const PLUGINS = []
358
359
if (args.plugins) {
360
    const identifyPluginFile = (file) => {
361
        const pluginFile = fs.readFileSync(file, 'utf8')
362
        const fileByLine = pluginFile.split(/\r?\n/)
363
364
        let pluginIdentified = false
365
        fileByLine.forEach(line => {
366
            if (line.includes("Koha::Plugins::Base")) {
367
                pluginIdentified = true
368
            }
369
        })
370
        return pluginIdentified
371
    }
372
373
    const collectPluginFiles = (fullPath) => {
374
        let files = []
375
        fs.readdirSync(fullPath).forEach(file => {
376
            const absolutePath = path.join(fullPath, file)
377
            if (fs.statSync(absolutePath).isDirectory()) {
378
                const filesFromNestedFolder = collectPluginFiles(absolutePath)
379
                filesFromNestedFolder && filesFromNestedFolder.forEach(file => {
380
                    files.push(file)
381
                })
382
            } else {
383
                return files.push(absolutePath)
384
            }
385
        })
386
        return files
387
    }
388
389
    function po_create_plugins(pluginData, type) {
390
        const access = util.promisify(fs.access);
391
        const exec = util.promisify(child_process.exec);
392
393
        const translatorDirCheck = fs.readdirSync(pluginData.bundlePath).includes('translator')
394
        if (!translatorDirCheck) {
395
            fs.mkdirSync(`${pluginData.bundlePath}/translator`)
396
        }
397
        const poDirCheck = fs.readdirSync(pluginData.bundlePath + '/translator').includes('po')
398
        if (!poDirCheck) {
399
            fs.mkdirSync(`${pluginData.bundlePath}/translator/po`)
400
        }
401
402
        const pot = `${pluginData.bundlePath}/translator/${pluginData.name}-${type}.pot`;
403
404
        // Generate .pot only if it doesn't exist or --force-extract is given
405
        const extract = () => stream.finished(poTasks[`${pluginData.name}-${type}`].extract());
406
407
        const p =
408
            args['generate-pot'] === 'always' ? extract() :
409
                args['generate-pot'] === 'auto' ? access(pot).catch(extract) :
410
                    args['generate-pot'] === 'never' ? Promise.resolve(0) :
411
                        Promise.reject(new Error('Invalid value for option --generate-pot: ' + args['generate-pot']))
412
413
        return p.then(function () {
414
            const languages = getLanguages();
415
            const promises = [];
416
            languages.forEach(language => {
417
                const locale = language.split('-').filter(s => s.length !== 4).join('_');
418
                const po = `${pluginData.bundlePath}/translator/po/${language}-${pluginData.name}-${type}.po`;
419
420
                const promise = access(po)
421
                    .catch(() => exec(`msginit -o ${po} -i ${pot} -l ${locale} --no-translator`))
422
                promises.push(promise);
423
            })
424
            return Promise.all(promises);
425
        })
426
    }
427
428
    function po_extract_plugins_js(pluginData) {
429
        const globs = [
430
            `${pluginData.directory}/**/*.js`,
431
            `${pluginData.directory}/**/*.vue`,
432
            `!${pluginData.directory}/**/node_modules/**/*`,
433
        ];
434
435
        return src(globs, { read: false, nocase: true })
436
            .pipe(xgettext(`xgettext -L JavaScript ${xgettext_options}`, `${pluginData.name}-js.pot`))
437
            .pipe(dest(`${pluginData.bundlePath}/translator`))
438
    }
439
440
    function po_extract_plugins_template(pluginData) {
441
        const globs = [
442
            `${pluginData.directory}/**/*.tt`,
443
            `${pluginData.directory}/**/*.inc`,
444
            `!${pluginData.directory}/**/node_modules/**/*`,
445
        ];
446
447
        return src(globs, { read: false, nocase: true })
448
            .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -F', `${pluginData.name}-template.pot`))
449
            .pipe(dest(`${pluginData.bundlePath}/translator`))
450
    }
451
452
    function po_update_plugins(pluginData, type) {
453
        const access = util.promisify(fs.access);
454
        const exec = util.promisify(child_process.exec);
455
456
        const pot = `${pluginData.bundlePath}/translator/${pluginData.name}-${type}.pot`;
457
458
        // Generate .pot only if it doesn't exist or --force-extract is given
459
        const extract = () => stream.finished(poTasks[`${pluginData.name}-${type}`].extract());
460
        const p =
461
            args['generate-pot'] === 'always' ? extract() :
462
                args['generate-pot'] === 'auto' ? access(pot).catch(extract) :
463
                    args['generate-pot'] === 'never' ? Promise.resolve(0) :
464
                        Promise.reject(new Error('Invalid value for option --generate-pot: ' + args['generate-pot']))
465
466
        return p.then(function () {
467
            const languages = getLanguages();
468
            const promises = [];
469
            languages.forEach(language => {
470
                const po = `${pluginData.bundlePath}/translator/po/${language}-${pluginData.name}-${type}.po`;
471
                promises.push(exec(`msgmerge --backup=off --no-wrap --quiet -F --update ${po} ${pot}`));
472
            })
473
474
            return Promise.all(promises);
475
        });
476
    }
477
478
    // Remove all tasks except for plugins
479
    Object.keys(poTasks).forEach(task => {
480
        delete poTasks[task]
481
    })
482
483
    const pluginNames = fs.readdirSync(PLUGINS_BASE);
484
    pluginNames.forEach(plugin => {
485
        const pluginFiles = collectPluginFiles(`${PLUGINS_BASE}/${plugin}/Koha`)
486
        let pluginFilePath
487
        pluginFiles.forEach(file => {
488
            const pluginFile = identifyPluginFile(file)
489
            if (pluginFile) {
490
                pluginFilePath = file.split('.')[0]
491
            }
492
        })
493
        const name = pluginFilePath.split('/').pop()
494
        const pluginData = {
495
            name,
496
            bundlePath: pluginFilePath,
497
            directory: `${PLUGINS_BASE}/${plugin}`,
498
        }
499
        PLUGINS.push(pluginData)
500
501
        function po_extract_js () { return po_extract_plugins_js(pluginData) }
502
        function po_create_js () { return po_create_plugins(pluginData, 'js') }
503
        function po_update_js () { return po_update_plugins(pluginData, 'js') }
504
        function po_extract_template () { return po_extract_plugins_template(pluginData) }
505
        function po_create_template () { return po_create_plugins(pluginData, 'template') }
506
        function po_update_template () { return po_update_plugins(pluginData, 'template') }
507
        
508
        poTasks[`${name}-js`] = {
509
            extract: po_extract_js,
510
            create: po_create_js,
511
            update: po_update_js,
512
        }
513
        poTasks[`${name}-template`] = {
514
            extract: po_extract_template,
515
            create: po_create_template,
516
            update: po_update_template,
517
        }
518
    })
519
}
520
357
/**
521
/**
358
 * Gulp plugin that executes xgettext-like command `cmd` on all files given as
522
 * Gulp plugin that executes xgettext-like command `cmd` on all files given as
359
 * input, and then outputs the result as a POT file named `filename`.
523
 * input, and then outputs the result as a POT file named `filename`.
Lines 434-439 if (args['_'][0].match("po:") && !fs.existsSync('misc/translator/po')) { Link Here
434
    process.exit(1);
598
    process.exit(1);
435
}
599
}
436
600
601
const poTypes = getPoTasks();
602
437
exports['po:create'] = parallel(...poTypes.map(type => poTasks[type].create));
603
exports['po:create'] = parallel(...poTypes.map(type => poTasks[type].create));
438
exports['po:update'] = parallel(...poTypes.map(type => poTasks[type].update));
604
exports['po:update'] = parallel(...poTypes.map(type => poTasks[type].update));
439
exports['po:extract'] = parallel(...poTypes.map(type => poTasks[type].extract));
605
exports['po:extract'] = parallel(...poTypes.map(type => poTasks[type].extract));
440
- 

Return to bug 37472