|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const gutil = require('gulp-util') |
| 4 | +const through = require('through2') |
| 5 | +const titleCase = require('title-case') |
| 6 | +const fs = require('fs-extra') |
| 7 | +const find = require('find') |
| 8 | +const _exec = require('child_process').exec |
| 9 | + |
| 10 | +const P = 'gulp-debian' |
| 11 | +const dirMode = 755 /* chmod param for directory */ |
| 12 | +const fileMode = 644 /* chmod param for ordinary file */ |
| 13 | + |
| 14 | +function deb (files, pkg, cb) { |
| 15 | + let ctrl = [] |
| 16 | + for (let key in pkg) { |
| 17 | + ctrl.push(`${titleCase(key)}: ${pkg[key]}`) |
| 18 | + } |
| 19 | + ctrl.push(' ') |
| 20 | + return cb(null, ctrl) |
| 21 | +} |
| 22 | + |
| 23 | +function changelog (pkg) { |
| 24 | + let log = [] |
| 25 | + for (let i = 0; i < pkg.changelog.length; i++) { |
| 26 | + let header = `${pkg.package} (${pkg.changelog[i].version}) ` |
| 27 | + header += `${pkg.changelog[i].distribution}; urgency=${pkg.changelog[i].urgency}` |
| 28 | + log.push(header + '\n') |
| 29 | + for (let x = 0; x < pkg.changelog[i].changes.length; x++) { |
| 30 | + log.push(` * ${pkg.changelog[i].changes[x]}`) |
| 31 | + } |
| 32 | + const ts = Date.parse(pkg.changelog[i].date) |
| 33 | + var d = new Date(ts) |
| 34 | + d = d.toString().replace(/([a-zA-Z]*) ([a-zA-Z]*) ([0-9]*) ([0-9]*) ([0-9:]*) GMT(.....) .*/, '$1, $3 $2 $4 $5 $6') |
| 35 | + log.push(`\n -- ${pkg.maintainer} ${d}\n`) |
| 36 | + } |
| 37 | + log.push('') |
| 38 | + return log |
| 39 | +} |
| 40 | + |
| 41 | +function writeChangelog (pkg, out, cb) { |
| 42 | + const logf = changelog(pkg) |
| 43 | + if (logf.length > 0) { |
| 44 | + const logp = `${out}/usr/share/doc/${pkg.package}` |
| 45 | + const logo = `${logp}/changelog.Debian` |
| 46 | + fs.mkdirpSync(logp) |
| 47 | + fs.outputFile(logo, logf.join('\n'), |
| 48 | + function (err) { |
| 49 | + if (err) { |
| 50 | + cb(new gutil.PluginError(P, err)) |
| 51 | + return |
| 52 | + } |
| 53 | + _exec(`gzip -fn9 ${logo}; chmod ${fileMode} ${logo}.gz`, |
| 54 | + function (err, stdout, stderr) { |
| 55 | + if (stderr) { |
| 56 | + gutil.log(gutil.colors.red(stderr.trim())) |
| 57 | + cb(err) |
| 58 | + } |
| 59 | + }) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function installScript (fn, script, out, cb) { |
| 65 | + if (script !== undefined && script.length > 0) { |
| 66 | + const o = `${out}/DEBIAN/${fn}` |
| 67 | + if (typeof script === 'string') { |
| 68 | + if (fs.existsSync(script)) { |
| 69 | + fs.copySync(script, o) |
| 70 | + fs.chmodSync(o, parseInt('0755', 8)) |
| 71 | + } else { |
| 72 | + cb(new gutil.PluginError(P, `File ${script} not exist!`)) |
| 73 | + // return |
| 74 | + } |
| 75 | + } else { |
| 76 | + script.push('') |
| 77 | + fs.outputFile(o, script.join('\n'), function (err) { |
| 78 | + if (err) { |
| 79 | + cb(new gutil.PluginError(P, err)) |
| 80 | + // return |
| 81 | + } |
| 82 | + fs.chmodSync(o, parseInt('0755', 8)) |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function installCopyright (pn, path, out, cb) { |
| 89 | + if (fs.existsSync(path)) { |
| 90 | + const o = `${out}/usr/share/doc/${pn}/copyright` |
| 91 | + fs.copySync(path, o) |
| 92 | + fs.chmodSync(o, parseInt('0644', 8)) |
| 93 | + } else { |
| 94 | + gutil.log(gutil.colors.red(`Error reading copyright file!`)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function installConffiles (path, out, cb) { |
| 99 | + var conffiles = [] |
| 100 | + var files = find.fileSync(path) |
| 101 | + path = path.replace(/\/$/, '') |
| 102 | + files.forEach(function (item) { |
| 103 | + let pathDest = item.split('/').slice(path.split('/').length) |
| 104 | + fs.copySync(item, `${out}/${pathDest.join('/')}`) |
| 105 | + fs.chmodSync(`${out}/${pathDest.join('/')}`, parseInt(`0${fileMode}`, 8)) |
| 106 | + pathDest = '/' + pathDest.join('/') |
| 107 | + conffiles.push(pathDest) |
| 108 | + }) |
| 109 | + conffiles.push('') |
| 110 | + fs.outputFileSync(`${out}/DEBIAN/conffiles`, conffiles.join('\n')) |
| 111 | + fs.chmodSync(`${out}/DEBIAN/conffiles`, parseInt(`0${fileMode}`, 8)) |
| 112 | +} |
| 113 | + |
| 114 | +function chmodRegularFile (path, cb) { |
| 115 | + if (fs.statSync(path).isFile()) { |
| 116 | + fs.chmodSync(path, parseInt(`0${fileMode}`, 8)) |
| 117 | + } else { |
| 118 | + fs.readdirSync(path).forEach(file => { |
| 119 | + chmodRegularFile(`${path}/${file}`, cb) |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = function (pkg) { |
| 125 | + let files = [] |
| 126 | + return through.obj(function (file, enc, cb) { |
| 127 | + if (file.isStream()) { |
| 128 | + cb(new gutil.PluginError(P, 'Streaming not supported.')) |
| 129 | + // return |
| 130 | + } |
| 131 | + files.push(file) |
| 132 | + cb(null) |
| 133 | + }, function (cb) { |
| 134 | + if (typeof pkg === 'string') { |
| 135 | + pkg = fs.readJSONSync(pkg) |
| 136 | + } |
| 137 | + deb(files, pkg, function (err, ctrl) { |
| 138 | + if (pkg._verbose === undefined) { |
| 139 | + pkg._verbose = true |
| 140 | + } |
| 141 | + if (pkg._target === undefined || pkg._out === undefined) { |
| 142 | + cb(new gutil.PluginError(P, '_target and/or _out undefined.')) |
| 143 | + // return |
| 144 | + } |
| 145 | + if (pkg._copyright === undefined) { |
| 146 | + gutil.log(gutil.colors.cyan('_copyright may be omitted, but it is highly recommended to define.')) |
| 147 | + // cb(new gutil.PluginError(P, '_copyright undefined!')) |
| 148 | + // return |
| 149 | + } |
| 150 | + if (err) { |
| 151 | + cb(new gutil.PluginError(P, err, {filename: files[0].path})) |
| 152 | + // return |
| 153 | + } |
| 154 | + let out = `${pkg._out}/${pkg.package}_${pkg.version}_${pkg.architecture}` |
| 155 | + installScript('preinst', pkg.preinst, out, cb) |
| 156 | + installScript('postinst', pkg.postinst, out, cb) |
| 157 | + installScript('prerm', pkg.prerm, out, cb) |
| 158 | + installScript('postrm', pkg.postrm, out, cb) |
| 159 | + installScript('triggers', pkg.triggers, out, cb) |
| 160 | + installCopyright(pkg.package, pkg._copyright, out, cb) |
| 161 | + installConffiles(pkg.conffiles, out, cb) |
| 162 | + ctrl = ctrl.filter(function (line) { |
| 163 | + if (!/Out|Target|Verbose|Changelog|Preinst|Postinst|Prerm|Postrm|Clean|Copyright|Conffiles/.test(line)) { |
| 164 | + return line |
| 165 | + } |
| 166 | + }) |
| 167 | + |
| 168 | + writeChangelog(pkg, out, cb) |
| 169 | + |
| 170 | + fs.mkdir(`${out}/DEBIAN`, '0775', function (err) { |
| 171 | + if (err) { |
| 172 | + cb(new gutil.PluginError(P, err)) |
| 173 | + // return |
| 174 | + } |
| 175 | + |
| 176 | + files.map(function (f) { |
| 177 | + let t = f.path.split('/') |
| 178 | + t = t[t.length - 1] |
| 179 | + fs.copySync(f.path, `${out}/${pkg._target}/${t}`) |
| 180 | + chmodRegularFile(`${out}/${pkg._target}/${t}`) |
| 181 | + }) |
| 182 | + _exec(`chmod ${dirMode} $(find ${pkg._out} -type d)`) |
| 183 | + _exec(`dpkg-deb -Zxz --build ${pkg._out}/${pkg.package}_${pkg.version}_${pkg.architecture}`, |
| 184 | + function (err, stdout, stderr) { |
| 185 | + if (pkg._clean) { |
| 186 | + fs.removeSync(`${pkg._out}/${pkg.package}_${pkg.version}_${pkg.architecture}`) |
| 187 | + } |
| 188 | + if (pkg._verbose && stdout.length > 1) { |
| 189 | + gutil.log(stdout.trim() + '\n') |
| 190 | + } |
| 191 | + if (stderr) { |
| 192 | + gutil.log(gutil.colors.red(stderr.trim())) |
| 193 | + } |
| 194 | + const ctrlf = ctrl.join('\n') |
| 195 | + fs.outputFile(`${out}/DEBIAN/control`, ctrlf.substr(0, ctrlf.length - 1), |
| 196 | + function (err) { |
| 197 | + if (err) { |
| 198 | + cb(new gutil.PluginError(P, err)) |
| 199 | + // return |
| 200 | + }}) |
| 201 | + }) |
| 202 | + }) |
| 203 | + }) |
| 204 | + }) |
| 205 | +} |
0 commit comments