/
catalog.ts
303 lines (282 loc) · 8.74 KB
/
catalog.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
import { parse } from '@iarna/toml';
import is from '@sindresorhus/is';
import deepmerge from 'deepmerge';
import type { SkipReason } from '../../../../types';
import { hasKey } from '../../../../util/object';
import { escapeRegExp, regEx } from '../../../../util/regex';
import type { PackageDependency } from '../../types';
import type {
GradleCatalog,
GradleCatalogArtifactDescriptor,
GradleCatalogModuleDescriptor,
GradleManagerData,
GradleVersionCatalogVersion,
GradleVersionPointerTarget,
VersionPointer,
} from '../types';
function findVersionIndex(
content: string,
depName: string,
version: string
): number {
const eDn = escapeRegExp(depName);
const eVer = escapeRegExp(version);
const re = regEx(
`(?:id\\s*=\\s*)?['"]?${eDn}["']?(?:(?:\\s*=\\s*)|:|,\\s*)(?:.*version(?:\\.ref)?(?:\\s*\\=\\s*))?["']?${eVer}['"]?`
);
const match = re.exec(content);
if (match) {
return match.index + content.slice(match.index).indexOf(version);
}
// ignoring Fallback because I can't reach it in tests, and code is not supposed to reach it but just in case.
/* istanbul ignore next */
return findIndexAfter(content, depName, version);
}
function findIndexAfter(
content: string,
sliceAfter: string,
find: string
): number {
const slicePoint = content.indexOf(sliceAfter) + sliceAfter.length;
return slicePoint + content.slice(slicePoint).indexOf(find);
}
function isArtifactDescriptor(
obj: GradleCatalogArtifactDescriptor | GradleCatalogModuleDescriptor
): obj is GradleCatalogArtifactDescriptor {
return hasKey('group', obj);
}
function isVersionPointer(
obj: GradleVersionCatalogVersion | undefined
): obj is VersionPointer {
return hasKey('ref', obj);
}
function normalizeVersionPointer(versionPointer: string): string {
return versionPointer.replace(regEx(/[._]/g), '-');
}
interface VersionExtract {
currentValue?: string;
fileReplacePosition?: number;
skipReason?: SkipReason;
}
function extractVersion({
version,
versions,
depStartIndex,
depSubContent,
depName,
versionStartIndex,
versionSubContent,
}: {
version: GradleVersionCatalogVersion | undefined;
versions: Record<string, GradleVersionPointerTarget>;
depStartIndex: number;
depSubContent: string;
depName: string;
versionStartIndex: number;
versionSubContent: string;
}): VersionExtract {
if (isVersionPointer(version)) {
const parsedVersion = normalizeVersionPointer(version.ref);
// everything else is ignored
return extractLiteralVersion({
version: versions[parsedVersion],
depStartIndex: versionStartIndex,
depSubContent: versionSubContent,
sectionKey: parsedVersion,
});
} else {
return extractLiteralVersion({
version,
depStartIndex,
depSubContent,
sectionKey: depName,
});
}
}
function extractLiteralVersion({
version,
depStartIndex,
depSubContent,
sectionKey,
}: {
version: GradleVersionPointerTarget | undefined;
depStartIndex: number;
depSubContent: string;
sectionKey: string;
}): VersionExtract {
if (!version) {
return { skipReason: 'unspecified-version' };
} else if (is.string(version)) {
const fileReplacePosition =
depStartIndex + findVersionIndex(depSubContent, sectionKey, version);
return { currentValue: version, fileReplacePosition };
} else if (is.plainObject(version)) {
// https://github.com/gradle/gradle/blob/d9adf33a57925582988fc512002dcc0e8ce4db95/subprojects/core/src/main/java/org/gradle/api/internal/catalog/parser/TomlCatalogFileParser.java#L368
// https://docs.gradle.org/current/userguide/rich_versions.html
// https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
const versionKeys = ['require', 'prefer', 'strictly'];
let found = false;
let currentValue: string | undefined;
let fileReplacePosition: number | undefined;
if (version.reject || version.rejectAll) {
return { skipReason: 'unsupported-version' };
}
for (const key of versionKeys) {
if (key in version) {
if (found) {
// Currently, we only support one version constraint at a time
return { skipReason: 'multiple-constraint-dep' };
}
found = true;
currentValue = version[key] as string;
fileReplacePosition =
depStartIndex +
findIndexAfter(depSubContent, sectionKey, currentValue);
}
}
if (found) {
return { currentValue, fileReplacePosition };
}
}
return { skipReason: 'unspecified-version' };
}
function extractDependency({
descriptor,
versions,
depStartIndex,
depSubContent,
depName,
versionStartIndex,
versionSubContent,
}: {
descriptor:
| string
| GradleCatalogModuleDescriptor
| GradleCatalogArtifactDescriptor;
versions: Record<string, GradleVersionPointerTarget>;
depStartIndex: number;
depSubContent: string;
depName: string;
versionStartIndex: number;
versionSubContent: string;
}): PackageDependency<GradleManagerData> {
if (is.string(descriptor)) {
const [groupName, name, currentValue] = descriptor.split(':');
if (!currentValue) {
return {
depName,
skipReason: 'unspecified-version',
};
}
return {
depName: `${groupName}:${name}`,
groupName,
currentValue,
managerData: {
fileReplacePosition:
depStartIndex + findIndexAfter(depSubContent, depName, currentValue),
},
};
}
const { currentValue, fileReplacePosition, skipReason } = extractVersion({
version: descriptor.version,
versions,
depStartIndex,
depSubContent,
depName,
versionStartIndex,
versionSubContent,
});
if (skipReason) {
return {
depName,
skipReason,
};
}
const versionRef = isVersionPointer(descriptor.version)
? normalizeVersionPointer(descriptor.version.ref)
: null;
if (isArtifactDescriptor(descriptor)) {
const { group, name } = descriptor;
const groupName = is.nullOrUndefined(versionRef) ? group : versionRef; // usage of common variable should have higher priority than other values
return {
depName: `${group}:${name}`,
groupName,
currentValue,
managerData: { fileReplacePosition },
};
}
const [depGroupName, name] = descriptor.module.split(':');
const groupName = is.nullOrUndefined(versionRef) ? depGroupName : versionRef;
const dependency = {
depName: `${depGroupName}:${name}`,
groupName,
currentValue,
managerData: { fileReplacePosition },
};
return dependency;
}
export function parseCatalog(
packageFile: string,
content: string
): PackageDependency<GradleManagerData>[] {
const tomlContent = parse(content) as GradleCatalog;
const versions = tomlContent.versions ?? {};
const libs = tomlContent.libraries ?? {};
const libStartIndex = content.indexOf('libraries');
const libSubContent = content.slice(libStartIndex);
const versionStartIndex = content.indexOf('versions');
const versionSubContent = content.slice(versionStartIndex);
const extractedDeps: PackageDependency<GradleManagerData>[] = [];
for (const libraryName of Object.keys(libs)) {
const libDescriptor = libs[libraryName];
const dependency = extractDependency({
descriptor: libDescriptor,
versions,
depStartIndex: libStartIndex,
depSubContent: libSubContent,
depName: libraryName,
versionStartIndex,
versionSubContent,
});
extractedDeps.push(dependency);
}
const plugins = tomlContent.plugins ?? {};
const pluginsStartIndex = content.indexOf('[plugins]');
const pluginsSubContent = content.slice(pluginsStartIndex);
for (const pluginName of Object.keys(plugins)) {
const pluginDescriptor = plugins[pluginName];
const [depName, version] = is.string(pluginDescriptor)
? pluginDescriptor.split(':')
: [pluginDescriptor.id, pluginDescriptor.version];
const { currentValue, fileReplacePosition, skipReason } = extractVersion({
version,
versions,
depStartIndex: pluginsStartIndex,
depSubContent: pluginsSubContent,
depName,
versionStartIndex,
versionSubContent,
});
const dependency: PackageDependency<GradleManagerData> = {
depType: 'plugin',
depName,
packageName: `${depName}:${depName}.gradle.plugin`,
currentValue,
commitMessageTopic: `plugin ${pluginName}`,
managerData: { fileReplacePosition },
};
if (skipReason) {
dependency.skipReason = skipReason;
}
if (isVersionPointer(version) && dependency.commitMessageTopic) {
dependency.groupName = normalizeVersionPointer(version.ref);
delete dependency.commitMessageTopic;
}
extractedDeps.push(dependency);
}
const deps = extractedDeps.map((dep) => {
return deepmerge(dep, { managerData: { packageFile } });
});
return deps;
}