import { query as q } from '@renovatebot/good-enough-parser'; import type { Ctx } from '../types.ts'; import { cleanupTempVars, qArtifactId, qGroupId, qStringValue, qStringValueAsSymbol, qValueMatcher, storeInTokenMap, storeVarToken, } from './common.ts'; import { handleCatalogDepString, handleCatalogLongFormDep, handlePlugin, } from './handlers.ts'; const qAlias = qStringValue.handler((ctx) => storeInTokenMap(ctx, 'alias')); const qVersionCatalogVersion = q .op('.') .alt( // library("kotlin-reflect", "org.jetbrains.kotlin", "kotlin-reflect").versionRef("kotlin") q.sym('versionRef').tree({ maxDepth: 1, startsWith: '(', endsWith: ')', search: q.begin().join(qStringValueAsSymbol).end(), }), // library("android-gradle", "com.android.tools.build", "gradle").version("${agp}") q.sym('version').tree({ maxDepth: 1, startsWith: '(', endsWith: ')', search: q.begin().join(qValueMatcher).end(), }), ) .handler((ctx) => storeInTokenMap(ctx, 'version')); // library("foo.bar", "foo", "bar") const qVersionCatalogLongFormDependencies = q .sym('library') .tree({ type: 'wrapped-tree', maxDepth: 1, startsWith: '(', endsWith: ')', search: q .begin() .join(qAlias) .op(',') .join(qGroupId) .op(',') .join(qArtifactId) .end(), }) .opt(qVersionCatalogVersion) .handler(handleCatalogLongFormDep) .handler(cleanupTempVars); // library("foo.bar", "foo:bar:1.2.3") const qVersionCatalogShortDependencies = q .sym('library') .tree({ type: 'wrapped-tree', maxDepth: 1, startsWith: '(', endsWith: ')', search: q .begin() .join(qAlias) .op(',') .join(qValueMatcher) .handler((ctx) => storeInTokenMap(ctx, 'templateStringTokens')) .end(), }) .handler(handleCatalogDepString) .handler(cleanupTempVars); // plugin("foo.bar", "foo:bar") const qVersionCatalogPlugins = q .sym('plugin', storeVarToken) .handler((ctx) => storeInTokenMap(ctx, 'methodName')) .tree({ type: 'wrapped-tree', maxDepth: 1, startsWith: '(', endsWith: ')', search: q .begin() .join(qAlias) .op(',') .alt(qValueMatcher) .handler((ctx) => storeInTokenMap(ctx, 'pluginName')) .end(), }) .join(qVersionCatalogVersion) .handler(handlePlugin) .handler(cleanupTempVars); // alias("foo.bar").to("foo", "bar").version("1.2.3") const qVersionCatalogAliasDependencies = q .sym('alias') .tree({ type: 'wrapped-tree', maxDepth: 1, startsWith: '(', endsWith: ')', search: q.begin().join(qAlias).end(), }) .op('.') .sym('to') .tree({ type: 'wrapped-tree', maxDepth: 1, startsWith: '(', endsWith: ')', search: q.begin().join(qGroupId).op(',').join(qArtifactId).end(), }) .opt(qVersionCatalogVersion) .handler(handleCatalogLongFormDep) .handler(cleanupTempVars); export const qVersionCatalogs = q.alt( qVersionCatalogLongFormDependencies, qVersionCatalogShortDependencies, qVersionCatalogPlugins, qVersionCatalogAliasDependencies, );