-
Notifications
You must be signed in to change notification settings - Fork 16
/
makerpm.js
executable file
·106 lines (85 loc) · 2.76 KB
/
makerpm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const rimraf = require('rimraf');
const which = require('which');
const validator = require('specit/lib/validator');
const generate = require('specit/lib/generate');
const clean = require('specit/lib/clean');
const program = require('commander');
const spawn = require('child_process').spawnSync;
const cwd = process.cwd();
const pkginfo = require('./package.json');
const plugininfo = require('./src/plugin.json');
let version = pkginfo.version;
let release = 1;
if (version.indexOf('-SNAPSHOT') > 0) {
version = version.replace('-SNAPSHOT', '');
release = '0';
}
try {
which.sync('rpmbuild');
} catch (err) {
console.log('rpmbuild executable not found');
process.exit(1);
}
program
.version(pkginfo.version)
.option('-r --release <release>', 'Specify release number of package')
.parse(process.argv);
const options = program.opts();
if (options.release === undefined) {
options.release = release;
}
pkginfo.version = version;
pkginfo.release = release;
release = options.release;
const props = Object.assign({}, plugininfo, pkginfo);
console.log('Cleaning up after any previous RPM builds');
clean('.', pkginfo);
console.log('Generating RPM spec for ' + props.name + ' (' + props.id + ') ' + props.version + '-' + options.release);
generate(cwd, props, options, pkginfo.name, function (err, generated) {
if (err) {
console.error('Error:', err.message);
process.exit(1);
}
generated.forEach(function (file) {
console.log('Created ./%s', file);
});
const rpmbuilddir = path.join(os.tmpdir(), 'rpmbuild');
rimraf.sync(rpmbuilddir);
fs.mkdirSync(rpmbuilddir);
fs.mkdirSync(path.join(rpmbuilddir, 'SOURCES'));
fs.mkdirSync(path.join(rpmbuilddir, 'RPMS'));
fs.mkdirSync(path.join(rpmbuilddir, 'BUILD'));
fs.copySync(path.join('SOURCES', pkginfo.name + '.tar.gz'), path.join(rpmbuilddir, 'SOURCES', pkginfo.name + '.tar.gz'));
console.log('Running rpmbuild');
const ret = spawn(
'rpmbuild',
[
'--define', '_topdir ' + path.join(os.tmpdir(), 'rpmbuild'),
'--define', 'pluginid ' + plugininfo.id,
'-ba',
'SPECS/opennms-grafana-plugin.spec'
],
{
stdio: ['inherit', 'inherit', 'inherit']
}
);
if (ret.error) {
console.log('rpmbuild failed');
process.exit(1);
}
const targetdir = path.join('artifacts');
if (!fs.existsSync(targetdir)) {
fs.mkdirSync(targetdir);
}
const rpm = pkginfo.name + '-' + version + '-' + release + '.noarch.rpm';
if (fs.existsSync(path.join(targetdir, rpm))) {
fs.unlinkSync(path.join(targetdir, rpm));
}
fs.copySync(path.join(rpmbuilddir, 'RPMS', 'noarch', rpm), path.join(targetdir, rpm));
rimraf.sync(rpmbuilddir);
process.exit(0);
});