-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
handlers.ts
431 lines (370 loc) · 11.8 KB
/
handlers.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import URL from 'node:url';
import upath from 'upath';
import { logger } from '../../../../logger';
import { getSiblingFileName } from '../../../../util/fs';
import { regEx } from '../../../../util/regex';
import type { PackageDependency } from '../../types';
import type { parseGradle as parseGradleCallback } from '../parser';
import type { Ctx, GradleManagerData } from '../types';
import { parseDependencyString } from '../utils';
import {
ANNOYING_METHODS,
GRADLE_PLUGINS,
REGISTRY_URLS,
interpolateString,
loadFromTokenMap,
} from './common';
// needed to break circular dependency
let parseGradle: typeof parseGradleCallback;
export function setParseGradleFunc(func: typeof parseGradleCallback): void {
parseGradle = func;
}
export function handleAssignment(ctx: Ctx): Ctx {
const key = loadFromTokenMap(ctx, 'keyToken')[0].value;
const valTokens = loadFromTokenMap(ctx, 'valToken');
if (valTokens.length > 1) {
// = template string with multiple variables
ctx.tokenMap.templateStringTokens = valTokens;
handleDepString(ctx);
delete ctx.tokenMap.templateStringTokens;
} else {
// = string value
const dep = parseDependencyString(valTokens[0].value);
if (dep) {
dep.groupName = key;
dep.managerData = {
fileReplacePosition: valTokens[0].offset + dep.depName!.length + 1,
packageFile: ctx.packageFile,
};
ctx.deps.push(dep);
}
ctx.globalVars[key] = {
key,
value: valTokens[0].value,
fileReplacePosition: valTokens[0].offset,
packageFile: ctx.packageFile,
};
}
return ctx;
}
export function handleDepString(ctx: Ctx): Ctx {
const stringTokens = loadFromTokenMap(ctx, 'templateStringTokens');
const templateString = interpolateString(stringTokens, ctx.globalVars);
if (!templateString) {
return ctx;
}
const dep = parseDependencyString(templateString);
if (!dep) {
return ctx;
}
let packageFile: string | undefined;
let fileReplacePosition: number | undefined;
for (const token of stringTokens) {
if (token.type === 'symbol') {
const varData = ctx.globalVars[token.value];
if (varData) {
packageFile = varData.packageFile;
fileReplacePosition = varData.fileReplacePosition;
if (varData.value === dep.currentValue) {
dep.managerData = { fileReplacePosition, packageFile };
dep.groupName = varData.key;
}
}
}
}
if (!dep.managerData) {
const lastToken = stringTokens[stringTokens.length - 1];
if (
lastToken?.type === 'string-value' &&
dep.currentValue &&
lastToken.value.includes(dep.currentValue)
) {
packageFile = ctx.packageFile;
if (stringTokens.length === 1) {
fileReplacePosition = lastToken.offset + dep.depName!.length + 1;
} else {
fileReplacePosition =
lastToken.offset + lastToken.value.lastIndexOf(dep.currentValue);
}
delete dep.groupName;
} else {
dep.skipReason = 'contains-variable';
}
dep.managerData = { fileReplacePosition, packageFile };
}
ctx.deps.push(dep);
return ctx;
}
export function handleKotlinShortNotationDep(ctx: Ctx): Ctx {
const moduleNameTokens = loadFromTokenMap(ctx, 'artifactId');
const versionTokens = loadFromTokenMap(ctx, 'version');
const moduleName = interpolateString(moduleNameTokens, ctx.globalVars);
const versionValue = interpolateString(versionTokens, ctx.globalVars);
if (!moduleName || !versionValue) {
return ctx;
}
const groupIdArtifactId = `org.jetbrains.kotlin:kotlin-${moduleName}`;
const dep = parseDependencyString(`${groupIdArtifactId}:${versionValue}`);
if (!dep) {
return ctx;
}
dep.depName = moduleName;
dep.packageName = groupIdArtifactId;
dep.managerData = {
fileReplacePosition: versionTokens[0].offset,
packageFile: ctx.packageFile,
};
if (versionTokens.length > 1) {
// = template string with multiple variables
dep.skipReason = 'unknown-version';
} else if (versionTokens[0].type === 'symbol') {
const varData = ctx.globalVars[versionTokens[0].value];
if (varData) {
dep.groupName = varData.key;
dep.currentValue = varData.value;
dep.managerData = {
fileReplacePosition: varData.fileReplacePosition,
packageFile: varData.packageFile,
};
}
}
ctx.deps.push(dep);
return ctx;
}
export function handleLongFormDep(ctx: Ctx): Ctx {
const groupIdTokens = loadFromTokenMap(ctx, 'groupId');
const artifactIdTokens = loadFromTokenMap(ctx, 'artifactId');
const versionTokens = loadFromTokenMap(ctx, 'version');
const groupId = interpolateString(groupIdTokens, ctx.globalVars);
const artifactId = interpolateString(artifactIdTokens, ctx.globalVars);
const version = interpolateString(versionTokens, ctx.globalVars);
if (!groupId || !artifactId || !version) {
return ctx;
}
const dep = parseDependencyString([groupId, artifactId, version].join(':'));
if (!dep) {
return ctx;
}
const methodName = ctx.tokenMap.methodName ?? null;
if (versionTokens.length > 1) {
// = template string with multiple variables
dep.skipReason = 'unknown-version';
} else if (versionTokens[0].type === 'symbol') {
const varData = ctx.globalVars[versionTokens[0].value];
if (varData) {
dep.groupName = varData.key;
dep.managerData = {
fileReplacePosition: varData.fileReplacePosition,
packageFile: varData.packageFile,
};
}
} else {
// = string value
if (methodName?.[0]?.value === 'dependencySet') {
dep.groupName = `${groupId}:${version}`;
}
dep.managerData = {
fileReplacePosition: versionTokens[0].offset,
packageFile: ctx.packageFile,
};
}
if (methodName?.[0] && ANNOYING_METHODS.has(methodName[0].value)) {
dep.skipReason = 'ignored';
}
ctx.deps.push(dep);
return ctx;
}
export function handlePlugin(ctx: Ctx): Ctx {
const methodName = loadFromTokenMap(ctx, 'methodName')[0];
const pluginName = loadFromTokenMap(ctx, 'pluginName')[0];
const pluginVersion = loadFromTokenMap(ctx, 'version');
const plugin = pluginName.value;
const depName =
methodName.value === 'kotlin' ? `org.jetbrains.kotlin.${plugin}` : plugin;
const packageName = `${depName}:${depName}.gradle.plugin`;
const dep: PackageDependency<GradleManagerData> = {
depType: 'plugin',
depName,
packageName,
commitMessageTopic: `plugin ${depName}`,
currentValue: pluginVersion[0].value,
managerData: {
fileReplacePosition: pluginVersion[0].offset,
packageFile: ctx.packageFile,
},
};
if (pluginVersion.length > 1) {
// = template string with multiple variables
dep.skipReason = 'unknown-version';
} else if (pluginVersion[0].type === 'symbol') {
const varData = ctx.globalVars[pluginVersion[0].value];
if (varData) {
dep.groupName = varData.key;
dep.currentValue = varData.value;
dep.managerData = {
fileReplacePosition: varData.fileReplacePosition,
packageFile: varData.packageFile,
};
} else {
dep.skipReason = 'unknown-version';
}
}
ctx.deps.push(dep);
return ctx;
}
function isPluginRegistry(ctx: Ctx): boolean {
if (ctx.tokenMap.registryScope) {
const registryScope = loadFromTokenMap(ctx, 'registryScope')[0].value;
return registryScope === 'pluginManagement';
}
return false;
}
export function handlePredefinedRegistryUrl(ctx: Ctx): Ctx {
const registryName = loadFromTokenMap(ctx, 'registryUrl')[0].value;
ctx.registryUrls.push({
registryUrl: REGISTRY_URLS[registryName as keyof typeof REGISTRY_URLS],
scope: isPluginRegistry(ctx) ? 'plugin' : 'dep',
});
return ctx;
}
export function handleCustomRegistryUrl(ctx: Ctx): Ctx {
let localVariables = ctx.globalVars;
if (ctx.tokenMap.name) {
const nameTokens = loadFromTokenMap(ctx, 'name');
const nameValue = interpolateString(nameTokens, localVariables);
if (nameValue) {
localVariables = {
...localVariables,
name: {
key: 'name',
value: nameValue,
},
};
}
}
let registryUrl = interpolateString(
loadFromTokenMap(ctx, 'registryUrl'),
localVariables
);
if (registryUrl) {
registryUrl = registryUrl.replace(regEx(/\\/g), '');
try {
const { host, protocol } = URL.parse(registryUrl);
if (host && protocol) {
ctx.registryUrls.push({
registryUrl,
scope: isPluginRegistry(ctx) ? 'plugin' : 'dep',
});
}
} catch (e) {
// no-op
}
}
return ctx;
}
export function handleLibraryDep(ctx: Ctx): Ctx {
const groupIdTokens = loadFromTokenMap(ctx, 'groupId');
const artifactIdTokens = loadFromTokenMap(ctx, 'artifactId');
const groupId = interpolateString(groupIdTokens, ctx.globalVars);
const artifactId = interpolateString(artifactIdTokens, ctx.globalVars);
if (!groupId || !artifactId) {
return ctx;
}
const aliasToken = loadFromTokenMap(ctx, 'alias')[0];
const key = `libs.${aliasToken.value.replace(regEx(/[-_]/g), '.')}`;
ctx.globalVars[key] = {
key,
value: `${groupId}:${artifactId}`,
fileReplacePosition: aliasToken.offset,
packageFile: ctx.packageFile,
};
if (ctx.tokenMap.version) {
const version = interpolateString(
loadFromTokenMap(ctx, 'version'),
ctx.globalVars
);
if (version) {
handleLongFormDep(ctx);
}
}
return ctx;
}
export function handleApplyFrom(ctx: Ctx): Ctx {
let scriptFile = interpolateString(
loadFromTokenMap(ctx, 'scriptFile'),
ctx.globalVars
);
if (!scriptFile) {
return ctx;
}
if (ctx.tokenMap.parentPath) {
const parentPath = interpolateString(
loadFromTokenMap(ctx, 'parentPath'),
ctx.globalVars
);
if (parentPath && scriptFile) {
scriptFile = upath.join(parentPath, scriptFile);
}
}
if (ctx.recursionDepth > 2) {
logger.debug(`Max recursion depth reached in script file: ${scriptFile}`);
return ctx;
}
if (!regEx(/\.gradle(\.kts)?$/).test(scriptFile)) {
logger.debug({ scriptFile }, `Only Gradle files can be included`);
return ctx;
}
const scriptFilePath = getSiblingFileName(ctx.packageFile, scriptFile);
if (!ctx.fileContents[scriptFilePath]) {
logger.debug(`Failed to process included Gradle file ${scriptFilePath}`);
return ctx;
}
const matchResult = parseGradle(
// TODO #7154
ctx.fileContents[scriptFilePath]!,
ctx.globalVars,
scriptFilePath,
ctx.fileContents,
ctx.recursionDepth + 1
);
ctx.deps.push(...matchResult.deps);
ctx.globalVars = { ...ctx.globalVars, ...matchResult.vars };
ctx.registryUrls.push(...matchResult.urls);
return ctx;
}
export function handleImplicitGradlePlugin(ctx: Ctx): Ctx {
const pluginName = loadFromTokenMap(ctx, 'pluginName')[0].value;
const versionTokens = loadFromTokenMap(ctx, 'version');
const versionValue = interpolateString(versionTokens, ctx.globalVars);
if (!versionValue) {
return ctx;
}
const groupIdArtifactId =
GRADLE_PLUGINS[pluginName as keyof typeof GRADLE_PLUGINS][1];
const dep = parseDependencyString(`${groupIdArtifactId}:${versionValue}`);
if (!dep) {
return ctx;
}
dep.depName = pluginName;
dep.packageName = groupIdArtifactId;
dep.managerData = {
fileReplacePosition: versionTokens[0].offset,
packageFile: ctx.packageFile,
};
if (versionTokens.length > 1) {
// = template string with multiple variables
dep.skipReason = 'unknown-version';
} else if (versionTokens[0].type === 'symbol') {
const varData = ctx.globalVars[versionTokens[0].value];
if (varData) {
dep.groupName = varData.key;
dep.currentValue = varData.value;
dep.managerData = {
fileReplacePosition: varData.fileReplacePosition,
packageFile: varData.packageFile,
};
}
}
ctx.deps.push(dep);
return ctx;
}