forked from microsoft/azure-pipelines-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolchains.ts
121 lines (112 loc) · 3.61 KB
/
toolchains.ts
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
import fs = require('fs');
import os = require('os');
import path = require('path');
import taskLib = require('azure-pipelines-task-lib/task');
import toolLib = require('azure-pipelines-tool-lib/tool');
import { create as xmlCreate } from 'xmlbuilder2';
const M2_DIR = '.m2';
const MVN_TOOLCHAINS_FILE: string = 'toolchains.xml';
export async function configureToolchains(version: string, vendor: string, jdkHome: string) {
const id = `${vendor}_${version}`;
const settingsDirectory = path.join(os.homedir(), M2_DIR);
await createToolchainsSettings(
version,
vendor,
id,
jdkHome,
settingsDirectory,
);
}
async function createToolchainsSettings(
version: string,
vendor: string,
id: string,
jdkHome: string,
settingsDirectory: string,
) {
toolLib.debug(`Creating ${MVN_TOOLCHAINS_FILE} for JDK version ${version} from ${vendor}`);
await taskLib.mkdirP(settingsDirectory);
const originalToolchains = await readExisting(settingsDirectory);
const updatedToolchains = generate(originalToolchains, version, vendor, id, jdkHome);
await write(settingsDirectory, updatedToolchains, true);
}
function generate(
original: string,
version: string,
vendor: string,
id: string,
jdkHome: string
) {
let xmlObj;
if (original && original.length > 0) {
xmlObj = xmlCreate(original)
.root()
.ele({
type: 'jdk',
provides: {
version: `${version}`,
vendor: `${vendor}`,
id: `${id}`
},
configuration: {
jdkHome: `${jdkHome}`
}
});
} else
xmlObj = xmlCreate({
toolchains: {
'@xmlns': 'https://maven.apache.org/TOOLCHAINS/1.1.0',
'@xmlns:xsi': 'https://www.w3.org/2001/XMLSchema-instance',
'@xsi:schemaLocation':
'https://maven.apache.org/TOOLCHAINS/1.1.0 https://maven.apache.org/xsd/toolchains-1.1.0.xsd',
toolchain: [
{
type: 'jdk',
provides: {
version: `${version}`,
vendor: `${vendor}`,
id: `${id}`
},
configuration: {
jdkHome: `${jdkHome}`
}
}
]
}
});
return xmlObj.end({
format: 'xml',
wellFormed: false,
headless: false,
prettyPrint: true,
width: 80
});
}
async function readExisting(directory: string) {
const location = path.join(directory, MVN_TOOLCHAINS_FILE);
if (fs.existsSync(location)) {
return fs.readFileSync(location, {
encoding: 'utf-8',
flag: 'r'
});
}
return '';
}
async function write(directory: string, settings: string, overwriteSettings: boolean) {
const location = path.join(directory, MVN_TOOLCHAINS_FILE);
const settingsExists = fs.existsSync(location);
if (settingsExists && overwriteSettings) {
toolLib.debug(`Overwriting existing file ${location}`);
} else if (!settingsExists) {
toolLib.debug(`Writing to ${location}`);
} else {
toolLib.debug(
`Skipping generation ${location} because file already exists and overwriting is not required`
);
return;
}
return fs.writeFileSync(location, settings, {
encoding: 'utf-8',
flag: 'w'
});
}