-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
226 lines (188 loc) · 5.59 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const core = require('@actions/core');
const exec = require('@actions/exec');
const fs = require('fs');
const path = require('path');
async function main() {
//several things need to happen to make this work
//step 1 is we need a SPEC file that lists everything
// this step needs to do some things:
// - generate the "list" of files
// - create the pre/post scripts we need
// - populate the rest of the template data
//step 2 is making a tar and moving the tar to the source folder
// - for this, we will use the list provided by the mapping
//step 3 is build it
const packageName = core.getInput('package');
const version = core.getInput('version').replace("-", "~");
const release = core.getInput('release');
const architecture = core.getInput('architecture');
const rootDir = '/tmp/rpmbuild';
const sourceDir = `${rootDir}/SOURCES/${packageName}-${version}`;
fs.mkdirSync(`${rootDir}/BUILD`, {recursive: true});
fs.mkdirSync(`${rootDir}/BUILDROOT`, {recursive: true});
fs.mkdirSync(`${rootDir}/RPMS`, {recursive: true});
fs.mkdirSync(`${rootDir}/SOURCES`, {recursive: true});
fs.mkdirSync(`${rootDir}/SPECS`, {recursive: true});
fs.mkdirSync(`${rootDir}/SRPMS`, {recursive: true});
fs.mkdirSync(`${rootDir}/BUILD`, {recursive: true});
fs.mkdirSync(sourceDir, {recursive: true});
//move the files that are specified to the correct path
const files = await getFileList();
const paths = [];
files.forEach((v, k) => {
console.log(`Copying ${k} to ${v}`);
//we need to create the target directory, which is the parent
fs.mkdirSync(path.dirname(path.join(sourceDir, v.substr(1))), {recursive: true});
paths.push([k, path.join(sourceDir, v.substr(1))]);
});
for (let v in paths) {
const val = paths[v];
await exec.exec('bash', ['-c', `cp -r ${val[0]} ${val[1]}`]);
}
await exec.exec('tar', ['-zcf', `${packageName}-${version}.tar.gz`, `${packageName}-${version}`], {cwd: `${rootDir}/SOURCES`});
fs.writeFileSync(`${packageName}-${version}.spec`, await buildSpecFile(version));
await exec.exec('rpmbuild', ['-bb', '--define', `_topdir ${rootDir}`, '--define',
`_rpmfilename ${packageName}-${version}-${release}.${architecture}.rpm`, `${packageName}-${version}.spec`]);
//fs.unlinkSync(`${packageName}-${version}.spec`);
//give them the file back
core.setOutput('file', `${rootDir}/RPMS/${packageName}-${version}-${release}.${architecture}.rpm`);
}
async function buildSpecFile(version) {
const neededFiles = [];
(await getFileList()).forEach(v => neededFiles.push(v));
return `Name: ${core.getInput('package')}
Version: ${version}
Release: ${core.getInput('release')}%{?dist}
Summary: ${core.getInput('summary')}
License: ${core.getInput('license')}
URL: ${core.getInput('website')}
Source0: %{name}-%{version}.tar.gz
BuildArch: ${core.getInput('architecture')}
${getSuggestedPackages()}
%description
${core.getInput('description')}
%global debug_package %{nil}
%prep
%setup -q
%build
%install
#for the files, we just need everything, we can just push it all without concern
rm -rf $RPM_BUILD_ROOT
mkdir $RPM_BUILD_ROOT
cp -r * $RPM_BUILD_ROOT
%clean
rm -rf $RPM_BUILD_ROOT
%files
${neededFiles.join('\n')}
${getConfigFiles()}
%changelog
%pre
upgrade() {
:
${await readFile(core.getInput('before-upgrade'))}
}
_install() {
:
${await readFile(core.getInput('before-install'))}
}
if [ "\${1}" -eq 1 ]
then
_install
elif [ "\${1}" -gt 1 ]
then
upgrade
fi
%post
upgrade() {
:
${await readFile(core.getInput('after-upgrade'))}
}
_install() {
:
${await readFile(core.getInput('after-install'))}
}
if [ "\${1}" -eq 1 ]
then
_install
elif [ "\${1}" -gt 1 ]
then
upgrade
fi
%preun
if [ "\${1}" -eq 0 ]
then
:
${await readFile(core.getInput('before-remove'))}
fi
%postun
if [ "\${1}" -eq 0 ]
then
:
${await readFile(core.getInput('after-remove'))}
fi
`
}
/**
* Parses the "files" input to be an array of files that will be included in the
* %files spec. Key is the source file
*
* @returns {Promise<Map<string,string>>}
*/
async function getFileList() {
const files = getList('files')
const result = new Map();
files.forEach(k => {
const parts = k.split(':');
result.set(parts[0], parts[1]);
});
return result;
}
async function readFile(file) {
if (file && file !== '') {
return fs.readFileSync(file).toString();
}
return '';
}
/**
* Parses the "suggested-packages" input to be a list of packages that will be
* included as the list of suggested packages
*
* @return {String}
*/
function getSuggestedPackages() {
const packages = getList('suggested-packages');
if (packages.length > 0) {
return 'Suggests: ' + packages.join(' ');
}
return '';
}
function getConfigFiles() {
const files = getList('config');
const result = [];
files.forEach(k => {
let parts = k.split(':');
if (parts.length === 1) {
result.push('%config ' + parts[0]);
} else {
result.push('%config(' + parts[1] + ') ' + parts[0]);
}
});
return result.join('\r\n');
}
/**
* Get a list from the input
*
* @param key String
* @returns {string[]}
*/
function getList(key) {
return core.getInput(key).split(/\r?\n/).reduce(
(acc, line) =>
acc
.concat(line.split(","))
.filter(pat => pat)
.map(pat => pat.trim()),
[]
);
}
main().catch(e => core.setFailed(e.message));