diff --git a/lib/modules/manager/gradle/extract.ts b/lib/modules/manager/gradle/extract.ts index 31d3355426d211..e0a76398b747fa 100644 --- a/lib/modules/manager/gradle/extract.ts +++ b/lib/modules/manager/gradle/extract.ts @@ -14,7 +14,6 @@ import { REGISTRY_URLS } from './parser/common'; import type { GradleManagerData, PackageRegistry, - PackageVariables, VariableRegistry, } from './types'; import { @@ -24,10 +23,25 @@ import { isTOMLFile, reorderFiles, toAbsolutePath, + updateVars, } from './utils'; const mavenDatasource = MavenDatasource.id; +function updatePackageRegistries( + packageRegistries: PackageRegistry[], + urls: PackageRegistry[] +): void { + for (const url of urls) { + const registryAlreadyKnown = packageRegistries.some( + (item) => item.registryUrl === url.registryUrl && item.scope === url.scope + ); + if (!registryAlreadyKnown) { + packageRegistries.push(url); + } + } +} + function getRegistryUrlsForDep( packageRegistries: PackageRegistry[], dep: PackageDependency @@ -45,18 +59,17 @@ function getRegistryUrlsForDep( return [...new Set(registryUrls)]; } -export async function extractAllPackageFiles( +async function parsePackageFiles( config: ExtractConfig, - packageFiles: string[] -): Promise { - const extractedDeps: PackageDependency[] = []; + packageFiles: string[], + extractedDeps: PackageDependency[], + packageFilesByName: Record, + packageRegistries: PackageRegistry[] +): Promise[]> { const varRegistry: VariableRegistry = {}; - const packageFilesByName: Record = {}; - const packageRegistries: PackageRegistry[] = []; - const reorderedFiles = reorderFiles(packageFiles); const fileContents = await getLocalFiles(packageFiles); - for (const packageFile of reorderedFiles) { + for (const packageFile of packageFiles) { packageFilesByName[packageFile] = { packageFile, datasource: mavenDatasource, @@ -68,24 +81,19 @@ export async function extractAllPackageFiles( const content = fileContents[packageFile]!; const packageFileDir = upath.dirname(toAbsolutePath(packageFile)); - const updateVars = (newVars: PackageVariables): void => { - const oldVars = varRegistry[packageFileDir] || {}; - varRegistry[packageFileDir] = { ...oldVars, ...newVars }; - }; - if (isPropsFile(packageFile)) { const { vars, deps } = parseProps(content, packageFile); - updateVars(vars); + updateVars(varRegistry, packageFileDir, vars); extractedDeps.push(...deps); } else if (isTOMLFile(packageFile)) { - const updatesFromCatalog = parseCatalog(packageFile, content); - extractedDeps.push(...updatesFromCatalog); + const deps = parseCatalog(packageFile, content); + extractedDeps.push(...deps); } else if ( isGcvPropsFile(packageFile) && usesGcv(packageFile, fileContents) ) { - const updatesFromGcv = parseGcv(packageFile, fileContents); - extractedDeps.push(...updatesFromGcv); + const deps = parseGcv(packageFile, fileContents); + extractedDeps.push(...deps); } else if (isGradleScriptFile(packageFile)) { const vars = getVars(varRegistry, packageFileDir); const { @@ -93,20 +101,8 @@ export async function extractAllPackageFiles( urls, vars: gradleVars, } = parseGradle(content, vars, packageFile, fileContents); - for (const url of urls) { - const registryAlreadyKnown = packageRegistries.some( - (item) => - item.registryUrl === url.registryUrl && item.scope === url.scope - ); - if (!registryAlreadyKnown) { - packageRegistries.push(url); - } - } - varRegistry[packageFileDir] = { - ...varRegistry[packageFileDir], - ...gradleVars, - }; - updateVars(gradleVars); + updatePackageRegistries(packageRegistries, urls); + updateVars(varRegistry, packageFileDir, gradleVars); extractedDeps.push(...deps); } } catch (err) { @@ -117,6 +113,26 @@ export async function extractAllPackageFiles( } } + return extractedDeps; +} + +export async function extractAllPackageFiles( + config: ExtractConfig, + packageFiles: string[] +): Promise { + const packageFilesByName: Record = {}; + const packageRegistries: PackageRegistry[] = []; + const extractedDeps: PackageDependency[] = []; + const gradleFiles = reorderFiles(packageFiles); + + await parsePackageFiles( + config, + gradleFiles, + extractedDeps, + packageFilesByName, + packageRegistries + ); + if (!extractedDeps.length) { return null; } diff --git a/lib/modules/manager/gradle/parser.ts b/lib/modules/manager/gradle/parser.ts index edc00409118741..1d4d3914b989eb 100644 --- a/lib/modules/manager/gradle/parser.ts +++ b/lib/modules/manager/gradle/parser.ts @@ -18,6 +18,20 @@ import type { import { isDependencyString, parseDependencyString } from './utils'; const groovy = lang.createLang('groovy'); +const ctx: Ctx = { + packageFile: '', + fileContents: {}, + recursionDepth: 0, + + globalVars: {}, + deps: [], + registryUrls: [], + + varTokens: [], + tmpNestingDepth: [], + tmpTokenStore: {}, + tokenMap: {}, +}; setParseGradleFunc(parseGradle); @@ -47,18 +61,11 @@ export function parseGradle( }); const parsedResult = groovy.query(input, query, { + ...ctx, packageFile, fileContents, recursionDepth, - - globalVars: initVars, - deps: [], - registryUrls: [], - - varTokens: [], - tmpNestingDepth: [], - tmpTokenStore: {}, - tokenMap: {}, + globalVars: vars, }); if (parsedResult) { diff --git a/lib/modules/manager/gradle/utils.spec.ts b/lib/modules/manager/gradle/utils.spec.ts index dde376e318fb25..0a3ee6c05ef0d3 100644 --- a/lib/modules/manager/gradle/utils.spec.ts +++ b/lib/modules/manager/gradle/utils.spec.ts @@ -5,6 +5,7 @@ import { parseDependencyString, reorderFiles, toAbsolutePath, + updateVars, versionLikeSubstring, } from './utils'; @@ -176,4 +177,21 @@ describe('modules/manager/gradle/utils', () => { qux: { key: 'qux', value: 'QUX' }, }); }); + + it('updateVars', () => { + const registry: VariableRegistry = { + [toAbsolutePath('/foo/bar/baz')]: { + bar: { key: 'bar', value: 'bar' } as never, + baz: { key: 'baz', value: 'baz' } as never, + }, + }; + + updateVars(registry, '/foo/bar/baz', { qux: { key: 'qux', value: 'qux' } }); + const res = getVars(registry, '/foo/bar/baz/build.gradle'); + expect(res).toStrictEqual({ + bar: { key: 'bar', value: 'bar' }, + baz: { key: 'baz', value: 'baz' }, + qux: { key: 'qux', value: 'qux' }, + }); + }); }); diff --git a/lib/modules/manager/gradle/utils.ts b/lib/modules/manager/gradle/utils.ts index 13f9a3557bf987..c2c898b3806455 100644 --- a/lib/modules/manager/gradle/utils.ts +++ b/lib/modules/manager/gradle/utils.ts @@ -172,3 +172,12 @@ export function getVars( const parentVars = registry[parentDir] || {}; return getVars(registry, parentDir, { ...parentVars, ...vars }); } + +export function updateVars( + registry: VariableRegistry, + dir: string, + newVars: PackageVariables +): void { + const oldVars = registry[dir] ?? {}; + registry[dir] = { ...oldVars, ...newVars }; +}