-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
extract.ts
321 lines (288 loc) · 8.88 KB
/
extract.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
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
import { basename, dirname, join, normalize } from 'path';
import is from '@sindresorhus/is';
import { XmlDocument, XmlElement } from 'xmldoc';
import * as datasourceMaven from '../../datasource/maven';
import { MAVEN_REPO } from '../../datasource/maven/common';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import { readLocalFile } from '../../util/fs';
import { ExtractConfig, PackageDependency, PackageFile } from '../common';
export function parsePom(raw: string): XmlDocument | null {
let project: XmlDocument;
try {
project = new XmlDocument(raw);
} catch (e) {
return null;
}
const { name, attr, children } = project;
if (name !== 'project') {
return null;
}
if (attr.xmlns === 'http://maven.apache.org/POM/4.0.0') {
return project;
}
if (
is.nonEmptyArray(children) &&
children.some((c: any) => c.name === 'modelVersion' && c.val === '4.0.0')
) {
return project;
}
return null;
}
function containsPlaceholder(str: string): boolean {
return /\${.*?}/g.test(str);
}
interface MavenProp {
val: string;
fileReplacePosition: number;
packageFile: string;
}
function depFromNode(node: XmlElement): PackageDependency | null {
if (!('valueWithPath' in node)) {
return null;
}
let groupId = node.valueWithPath('groupId');
const artifactId = node.valueWithPath('artifactId');
const currentValue = node.valueWithPath('version');
if (!groupId && node.name === 'plugin') {
groupId = 'org.apache.maven.plugins';
}
if (groupId && artifactId && currentValue) {
const depName = `${groupId}:${artifactId}`;
const versionNode = node.descendantWithPath('version');
const fileReplacePosition = versionNode.position;
const datasource = datasourceMaven.id;
const registryUrls = [MAVEN_REPO];
return {
datasource,
depName,
currentValue,
fileReplacePosition,
registryUrls,
};
}
return null;
}
function deepExtract(
node: XmlElement,
result: PackageDependency[] = [],
isRoot = true
): PackageDependency[] {
const dep = depFromNode(node);
if (dep && !isRoot) {
result.push(dep);
}
if (node.children) {
for (const child of node.children) {
deepExtract(child as XmlElement, result, false);
}
}
return result;
}
function applyProps(
dep: PackageDependency<Record<string, any>>,
depPackageFile: string,
props: MavenProp
): PackageDependency<Record<string, any>> {
const replaceAll = (str: string): string =>
str.replace(/\${.*?}/g, (substr) => {
const propKey = substr.slice(2, -1).trim();
const propValue = props[propKey];
return propValue ? propValue.val : substr;
});
const depName = replaceAll(dep.depName);
const registryUrls = dep.registryUrls.map((url) => replaceAll(url));
let fileReplacePosition = dep.fileReplacePosition;
let propSource = null;
let groupName = null;
const currentValue = dep.currentValue.replace(/^\${.*?}$/, (substr) => {
const propKey = substr.slice(2, -1).trim();
const propValue = props[propKey];
if (propValue) {
if (!groupName) {
groupName = propKey;
}
fileReplacePosition = propValue.fileReplacePosition;
propSource = propValue.packageFile;
return propValue.val;
}
return substr;
});
const result: PackageDependency = {
...dep,
depName,
registryUrls,
fileReplacePosition,
propSource,
currentValue,
};
if (groupName) {
result.groupName = groupName;
}
if (containsPlaceholder(depName)) {
result.skipReason = SkipReason.NamePlaceholder;
} else if (containsPlaceholder(currentValue)) {
result.skipReason = SkipReason.VersionPlaceholder;
}
if (propSource && depPackageFile !== propSource) {
result.editFile = propSource;
}
return result;
}
function resolveParentFile(packageFile: string, parentPath: string): string {
let parentFile = 'pom.xml';
let parentDir = parentPath;
const parentBasename = basename(parentPath);
if (parentBasename === 'pom.xml' || parentBasename.endsWith('.pom.xml')) {
parentFile = parentBasename;
parentDir = dirname(parentPath);
}
const dir = dirname(packageFile);
return normalize(join(dir, parentDir, parentFile));
}
export function extractPackage(
rawContent: string,
packageFile: string | null = null
): PackageFile<Record<string, any>> | null {
if (!rawContent) {
return null;
}
const project = parsePom(rawContent);
if (!project) {
return null;
}
const result: PackageFile = {
datasource: datasourceMaven.id,
packageFile,
deps: [],
};
result.deps = deepExtract(project);
const propsNode = project.childNamed('properties');
const props: Record<string, MavenProp> = {};
if (propsNode && propsNode.children) {
for (const propNode of propsNode.children as XmlElement[]) {
const key = propNode.name;
const val = propNode.val && propNode.val.trim();
if (key && val) {
const fileReplacePosition = propNode.position;
props[key] = { val, fileReplacePosition, packageFile };
}
}
}
result.mavenProps = props;
const repositories = project.childNamed('repositories');
if (repositories && repositories.children) {
const repoUrls = [];
for (const repo of repositories.childrenNamed('repository')) {
const repoUrl = repo.valueWithPath('url');
if (repoUrl) {
repoUrls.push(repoUrl);
}
}
result.deps.forEach((dep) => {
if (dep.registryUrls) {
repoUrls.forEach((url) => dep.registryUrls.push(url));
}
});
}
if (packageFile && project.childNamed('parent')) {
const parentPath =
project.valueWithPath('parent.relativePath') || '../pom.xml';
result.parent = resolveParentFile(packageFile, parentPath);
}
return result;
}
export function resolveParents(packages: PackageFile[]): PackageFile[] {
const packageFileNames: string[] = [];
const extractedPackages: Record<string, PackageFile> = {};
const extractedDeps: Record<string, PackageDependency[]> = {};
const extractedProps: Record<string, MavenProp> = {};
const registryUrls: Record<string, Set<string>> = {};
packages.forEach((pkg) => {
const name = pkg.packageFile;
packageFileNames.push(name);
extractedPackages[name] = pkg;
extractedDeps[name] = [];
});
// Construct package-specific prop scopes
// and merge them in reverse order,
// which allows inheritance/overriding.
packageFileNames.forEach((name) => {
registryUrls[name] = new Set();
const propsHierarchy: Record<string, MavenProp>[] = [];
const visitedPackages: Set<string> = new Set();
let pkg = extractedPackages[name];
while (pkg) {
propsHierarchy.unshift(pkg.mavenProps);
if (pkg.deps) {
pkg.deps.forEach((dep) => {
if (dep.registryUrls) {
dep.registryUrls.forEach((url) => {
registryUrls[name].add(url);
});
}
});
}
if (pkg.parent && !visitedPackages.has(pkg.parent)) {
visitedPackages.add(pkg.parent);
pkg = extractedPackages[pkg.parent];
} else {
pkg = null;
}
}
propsHierarchy.unshift({});
extractedProps[name] = Object.assign.apply(null, propsHierarchy as any);
});
// Resolve registryUrls
packageFileNames.forEach((name) => {
const pkg = extractedPackages[name];
pkg.deps.forEach((rawDep) => {
const urlsSet = new Set([...rawDep.registryUrls, ...registryUrls[name]]);
rawDep.registryUrls = [...urlsSet]; // eslint-disable-line no-param-reassign
});
});
// Resolve placeholders
packageFileNames.forEach((name) => {
const pkg = extractedPackages[name];
pkg.deps.forEach((rawDep) => {
const dep = applyProps(rawDep, name, extractedProps[name]);
const sourceName = dep.propSource || name;
extractedDeps[sourceName].push(dep);
});
});
return packageFileNames.map((name) => ({
...extractedPackages[name],
deps: extractedDeps[name],
}));
}
function cleanResult(
packageFiles: PackageFile<Record<string, any>>[]
): PackageFile<Record<string, any>>[] {
packageFiles.forEach((packageFile) => {
delete packageFile.mavenProps; // eslint-disable-line no-param-reassign
packageFile.deps.forEach((dep) => {
delete dep.propSource; // eslint-disable-line no-param-reassign
});
});
return packageFiles;
}
export async function extractAllPackageFiles(
_config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFile[]> {
const packages: PackageFile[] = [];
for (const packageFile of packageFiles) {
const content = await readLocalFile(packageFile, 'utf8');
if (content) {
const pkg = extractPackage(content, packageFile);
if (pkg) {
packages.push(pkg);
} else {
logger.debug({ packageFile }, 'can not read dependencies');
}
} else {
logger.debug({ packageFile }, 'packageFile has no content');
}
}
return cleanResult(resolveParents(packages));
}