-
Notifications
You must be signed in to change notification settings - Fork 867
Expand file tree
/
Copy pathtoolchains.ts
More file actions
347 lines (317 loc) · 10.1 KB
/
Copy pathtoolchains.ts
File metadata and controls
347 lines (317 loc) · 10.1 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as constants from './constants.js';
export {validateToolchainIds} from './toolchain-ids.js';
import {escapeXmlAttribute, escapeXmlText} from './xml.js';
interface JdkInfo {
version: string;
vendor: string;
id: string;
jdkHome: string;
}
export async function configureToolchains(
version: string,
distributionName: string,
jdkHome: string,
toolchainId?: string
) {
const vendor =
core.getInput(constants.INPUT_MVN_TOOLCHAIN_VENDOR) || distributionName;
const id = toolchainId || `${vendor}_${version}`;
const settingsDirectory =
core.getInput(constants.INPUT_SETTINGS_PATH) ||
path.join(os.homedir(), constants.M2_DIR);
await createToolchainsSettings({
jdkInfo: {
version,
vendor,
id,
jdkHome
},
settingsDirectory
});
}
export async function createToolchainsSettings({
jdkInfo,
settingsDirectory
}: {
jdkInfo: JdkInfo;
settingsDirectory: string;
}) {
core.info(
`Creating ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}`
);
// when an alternate m2 location is specified use only that location (no .m2 directory)
// otherwise use the home/.m2/ path
await io.mkdirP(settingsDirectory);
const originalToolchains =
await readExistingToolchainsFile(settingsDirectory);
const updatedToolchains = await generateToolchainDefinition(
originalToolchains,
jdkInfo.version,
jdkInfo.vendor,
jdkInfo.id,
jdkInfo.jdkHome
);
await writeToolchainsFileToDisk(settingsDirectory, updatedToolchains);
}
// only exported for testing purposes
export async function generateToolchainDefinition(
original: string,
version: string,
vendor: string,
id: string,
jdkHome: string
) {
if (!original?.length) {
return generateNewToolchainDefinition(version, vendor, id, jdkHome);
}
return generateMergedToolchainDefinition(
original,
version,
vendor,
id,
jdkHome
);
}
async function generateMergedToolchainDefinition(
original: string,
version: string,
vendor: string,
id: string,
jdkHome: string
) {
let jsToolchains: Toolchain[] = [
{
type: 'jdk',
provides: {
version: `${version}`,
vendor: `${vendor}`,
id: `${id}`
},
configuration: {
jdkHome: `${jdkHome}`
}
}
];
// default root attributes, used when the existing file does not declare its own
let rootAttributes: Record<string, string> = {
'@xmlns': 'http://maven.apache.org/TOOLCHAINS/1.1.0',
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'@xsi:schemaLocation':
'http://maven.apache.org/TOOLCHAINS/1.1.0 https://maven.apache.org/xsd/toolchains-1.1.0.xsd'
};
const {XMLParser} = await import('fast-xml-parser');
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@',
parseAttributeValue: false,
parseTagValue: false,
trimValues: true,
isArray: tagName => tagName === 'toolchain'
});
const jsObj = parser.parse(original) as ExtractedToolchains;
if (isToolchainsRoot(jsObj.toolchains)) {
// preserve the existing root attributes (xmlns, schemaLocation, …) so we don't
// silently rewrite user-managed metadata or change the effective XML namespace;
// fast-xml-parser exposes attributes as `@`-prefixed keys on the element object
const existingAttributes = Object.fromEntries(
Object.entries(jsObj.toolchains).filter(
([key, value]) => key.startsWith('@') && typeof value === 'string'
)
) as Record<string, string>;
// fall back to the defaults only for attributes the existing file is missing
rootAttributes = {...rootAttributes, ...existingAttributes};
if (jsObj.toolchains.toolchain) {
jsToolchains.push(...jsObj.toolchains.toolchain);
}
}
// remove potential duplicates based on type & id (which should be a unique combination);
// self.findIndex will only return the first occurrence, ensuring duplicates are skipped
jsToolchains = jsToolchains.filter(
(value, index, self) =>
// ensure non-jdk toolchains are kept in the results, we must not touch them because they belong to the user
value.type !== 'jdk' ||
// keep toolchains that lack a usable string id (e.g. partially-formed user files);
// we cannot safely deduplicate them and must not crash while reading them
typeof value.provides?.id !== 'string' ||
index ===
self.findIndex(
t => t.type === value.type && t.provides?.id === value.provides?.id
)
);
return serializeToolchains(rootAttributes, jsToolchains);
}
export function generateNewToolchainDefinition(
version: string,
vendor: string,
id: string,
jdkHome: string
) {
return [
'<?xml version="1.0"?>',
'<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0"',
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
' xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 https://maven.apache.org/xsd/toolchains-1.1.0.xsd">',
' <toolchain>',
' <type>jdk</type>',
' <provides>',
` <version>${escapeXmlText(version)}</version>`,
` <vendor>${escapeXmlText(vendor)}</vendor>`,
` <id>${escapeXmlText(id)}</id>`,
' </provides>',
' <configuration>',
` <jdkHome>${escapeXmlText(jdkHome)}</jdkHome>`,
' </configuration>',
' </toolchain>',
'</toolchains>'
].join('\n');
}
async function readExistingToolchainsFile(directory: string) {
const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE);
if (fs.existsSync(location)) {
return fs.readFileSync(location, {
encoding: 'utf-8',
flag: 'r'
});
}
return '';
}
async function writeToolchainsFileToDisk(directory: string, settings: string) {
const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE);
const settingsExists = fs.existsSync(location);
// The toolchains file is produced by a non-destructive merge (existing JDK,
// custom, and non-jdk toolchains are preserved – see generateToolchainDefinition),
// so it is always safe to write it. Unlike settings.xml, it is therefore not
// gated behind the `overwrite-settings` input; that would prevent subsequent
// setup-java runs from registering additional JDKs and silently drop the
// toolchain entries created by earlier runs.
if (settingsExists) {
core.info(`Updating existing file ${location}`);
} else {
core.info(`Writing to ${location}`);
}
return fs.writeFileSync(location, settings, {
encoding: 'utf-8',
flag: 'w'
});
}
function serializeToolchains(
rootAttributes: Record<string, string>,
toolchains: Toolchain[]
) {
return [
'<?xml version="1.0"?>',
serializeOpeningTag('toolchains', rootAttributes, 0),
...toolchains.flatMap(toolchain =>
serializeXmlElement('toolchain', toolchain, 1)
),
'</toolchains>'
].join('\n');
}
function serializeOpeningTag(
name: string,
attributes: Record<string, string>,
depth: number
) {
const indent = ' '.repeat(depth);
const attributeEntries = Object.entries(attributes);
if (!attributeEntries.length) {
return `${indent}<${name}>`;
}
const [firstAttribute, ...restAttributes] = attributeEntries;
const lines = [
`${indent}<${name} ${formatXmlAttribute(firstAttribute)}`,
...restAttributes.map(([attributeName, value]) => {
return `${indent} ${formatXmlAttribute([attributeName, value])}`;
})
];
lines[lines.length - 1] += '>';
return lines.join('\n');
}
function serializeXmlElement(
name: string,
value: XmlElementValue,
depth: number
): string[] {
const indent = ' '.repeat(depth);
if (Array.isArray(value)) {
return value.flatMap(item => serializeXmlElement(name, item, depth));
}
if (!isXmlElementObject(value)) {
return [
`${indent}<${name}>${escapeXmlText(String(value ?? ''))}</${name}>`
];
}
const attributes = Object.fromEntries(
Object.entries(value)
.filter(([key, attributeValue]) => {
return key.startsWith('@') && typeof attributeValue === 'string';
})
.map(([key, attributeValue]) => [key, attributeValue as string])
);
const childEntries = Object.entries(value).filter(
([key]) => !key.startsWith('@') && key !== '#text'
);
const textValue = value['#text'];
if (!childEntries.length) {
if (textValue !== undefined) {
return [
`${serializeOpeningTag(name, attributes, depth)}${escapeXmlText(
String(textValue ?? '')
)}</${name}>`
];
}
return [`${serializeOpeningTag(name, attributes, depth)}</${name}>`];
}
return [
serializeOpeningTag(name, attributes, depth),
...(textValue === undefined
? []
: [`${' '.repeat(depth + 1)}${escapeXmlText(String(textValue ?? ''))}`]),
...childEntries.flatMap(([childName, childValue]) =>
serializeXmlElement(childName, childValue, depth + 1)
),
`${indent}</${name}>`
];
}
function formatXmlAttribute([name, value]: [string, string]) {
return `${name.slice(1)}="${escapeXmlAttribute(value)}"`;
}
function isToolchainsRoot(value: ExtractedToolchains['toolchains']) {
return isXmlElementObject(value);
}
function isXmlElementObject(
value: unknown
): value is Record<string, XmlElementValue> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
interface ExtractedToolchains {
toolchains: ToolchainsRoot | string;
}
interface ToolchainsRoot extends XmlElementObject {
// root attributes such as xmlns / schemaLocation are exposed as `@`-prefixed keys
[attribute: `@${string}`]: string;
toolchain?: Toolchain[];
}
// Toolchain type definition according to Maven Toolchains XSD 1.1.0
interface Toolchain {
type: string;
provides?: XmlElementObject;
configuration?: XmlElementObject;
[customElement: string]: XmlElementValue;
}
interface XmlElementObject {
[name: string]: XmlElementValue;
}
type XmlElementValue =
| string
| number
| boolean
| null
| undefined
| XmlElementObject
| XmlElementValue[];