| 1 |
"use strict"; |
| 2 |
|
| 3 |
var path = require('path'), |
| 4 |
execFile = require('child_process').execFile; |
| 5 |
|
| 6 |
module.exports = function (grunt) { |
| 7 |
grunt.registerTask('data-zdump', '4. Dump data with zdump(8).', function (version) { |
| 8 |
version = version || 'latest'; |
| 9 |
|
| 10 |
var done = this.async(), |
| 11 |
zicBase = path.resolve('temp/zic', version), |
| 12 |
zdumpBase = path.resolve('temp/zdump', version), |
| 13 |
files = grunt.file.expand({ filter : 'isFile', cwd : 'temp/zic/' + version }, '**/*'); |
| 14 |
|
| 15 |
var zicBaseRegex = new RegExp((zicBase + path.sep).replace(/\\/g,'\\\\'), 'g'); |
| 16 |
var pathSepRegex = new RegExp(path.sep.replace('\\','\\\\'), 'g'); |
| 17 |
function normalizePaths(output) { |
| 18 |
var result = output.replace(zicBaseRegex, ''); |
| 19 |
if (path.sep !== '/') { |
| 20 |
result = result.replace(pathSepRegex, '/') |
| 21 |
} |
| 22 |
return result; |
| 23 |
} |
| 24 |
|
| 25 |
// Allow using custom zdump binaries |
| 26 |
var zdumpBinary = 'zdump', |
| 27 |
zdumpPathOption = grunt.option('zdump-path'); |
| 28 |
if (zdumpPathOption && grunt.file.exists(zdumpPathOption)) { |
| 29 |
zdumpBinary = zdumpPathOption; |
| 30 |
} |
| 31 |
|
| 32 |
var zdumpVerboseArg = '-V'; |
| 33 |
// Do a test of `zdump` to make sure it can provide the format we want. |
| 34 |
// The `-V` flag (different from `-v`) was introduced in tzcode version 2013d, |
| 35 |
// but still wasn't available on macOS until 14.0 (Sonoma). |
| 36 |
execFile(zdumpBinary, ['-V', 'UTC'], { maxBuffer: 20*1024*1024 }, function (err, stdout, stderr) { |
| 37 |
if (stdout === '' && stderr.includes('illegal option')) { |
| 38 |
zdumpVerboseArg = '-v'; |
| 39 |
grunt.log.warn('WARNING: The version of `zdump` on this machine is very old and might produce incorrect values'); |
| 40 |
|
| 41 |
// Do a separate test to make sure 64-bit data is returned. |
| 42 |
// 32-bit `zdump` returns 1901 & 2038 boundaries even for the UTC zone. |
| 43 |
} else if (stdout.includes('1901') && stdout.includes('2038')) { |
| 44 |
grunt.log.warn("WARNING: The version of `zdump` on this machine can't handle 64-bit data and will produce incorrect values"); |
| 45 |
} |
| 46 |
|
| 47 |
next(); |
| 48 |
}); |
| 49 |
|
| 50 |
function next () { |
| 51 |
if (!files.length) { |
| 52 |
grunt.log.ok('Dumped data for ' + version); |
| 53 |
return done(); |
| 54 |
} |
| 55 |
|
| 56 |
var file = files.pop(), |
| 57 |
src = path.join(zicBase, file), |
| 58 |
dest = path.join(zdumpBase, file); |
| 59 |
|
| 60 |
execFile(zdumpBinary, [zdumpVerboseArg, src], { maxBuffer: 20*1024*1024 }, function (err, stdout) { |
| 61 |
if (err) { throw err; } |
| 62 |
|
| 63 |
if (stdout.length === 0) { |
| 64 |
// on some systems, when there are no transitions then we have |
| 65 |
// to get creative to learn the offset and abbreviation |
| 66 |
execFile(zdumpBinary, ['UTC', src], { maxBuffer: 20*1024*1024 }, function (_err, _stdout) { |
| 67 |
if (_err) { throw _err; } |
| 68 |
|
| 69 |
grunt.file.write(dest + '.zdump', normalizePaths(_stdout)); |
| 70 |
grunt.verbose.ok('Dumped data for ' + version + ':' + file); |
| 71 |
|
| 72 |
next(); |
| 73 |
}); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
grunt.file.write(dest + '.zdump', normalizePaths(stdout)); |
| 78 |
grunt.verbose.ok('Dumped data for ' + version + ':' + file); |
| 79 |
|
| 80 |
next(); |
| 81 |
}); |
| 82 |
} |
| 83 |
}); |
| 84 |
}; |