fix(gradle-lite): Inherit variables from parent build.gradle (#8451)

This commit is contained in:
Sergei Zharinov 2021-01-29 13:15:08 +04:00 committed by GitHub
parent 9e09363305
commit 1b2a3f8006
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 6 deletions

View file

@ -66,4 +66,34 @@ describe('manager/gradle-lite/extract', () => {
},
]);
});
it('inherits gradle variables', async () => {
const fsMock = {
'gradle.properties': 'foo=1.0.0',
'build.gradle': 'foo = "1.0.1"',
'aaa/gradle.properties': 'bar = "2.0.0"',
'aaa/build.gradle': 'bar = "2.0.1"',
'aaa/bbb/build.gradle': ['foo:foo:$foo', 'bar:bar:$bar']
.map((x) => `"${x}"`)
.join('\n'),
};
mockFs(fsMock);
const res = await extractAllPackageFiles({} as never, Object.keys(fsMock));
expect(res).toMatchObject([
{ packageFile: 'gradle.properties', deps: [] },
{
packageFile: 'build.gradle',
deps: [{ depName: 'foo:foo', currentValue: '1.0.1' }],
},
{ packageFile: 'aaa/gradle.properties', deps: [] },
{
packageFile: 'aaa/build.gradle',
deps: [{ depName: 'bar:bar', currentValue: '2.0.1' }],
},
{ packageFile: 'aaa/bbb/build.gradle', deps: [] },
]);
});
});

View file

@ -3,7 +3,7 @@ import * as datasourceMaven from '../../datasource/maven';
import { logger } from '../../logger';
import { readLocalFile } from '../../util/fs';
import { ExtractConfig, PackageDependency, PackageFile } from '../common';
import { ManagerData, VariableRegistry } from './common';
import { ManagerData, PackageVariables, VariableRegistry } from './common';
import { parseGradle, parseProps } from './parser';
import {
getVars,
@ -46,18 +46,30 @@ export async function extractAllPackageFiles(
try {
const content = await readLocalFile(packageFile, 'utf8');
const dir = upath.dirname(toAbsolutePath(packageFile));
const updateVars = (newVars: PackageVariables): void => {
const oldVars = registry[dir] || {};
registry[dir] = { ...oldVars, ...newVars };
};
if (isPropsFile(packageFile)) {
const { vars, deps } = parseProps(content, packageFile);
registry[dir] = vars;
updateVars(vars);
extractedDeps.push(...deps);
} else if (isGradleFile(packageFile)) {
const vars = getVars(registry, dir);
const { deps, urls } = parseGradle(content, vars, packageFile);
const { deps, urls, vars: gradleVars } = parseGradle(
content,
vars,
packageFile
);
urls.forEach((url) => {
if (!registryUrls.includes(url)) {
registryUrls.push(url);
}
});
registry[dir] = { ...registry[dir], ...gradleVars };
updateVars(gradleVars);
extractedDeps.push(...deps);
}
} catch (e) {

View file

@ -458,12 +458,18 @@ function tryMatch({
return null;
}
interface ParseGradleResult {
deps: PackageDependency<ManagerData>[];
urls: string[];
vars: PackageVariables;
}
export function parseGradle(
input: string,
initVars: PackageVariables = {},
packageFile?: string
): { deps: PackageDependency<ManagerData>[]; urls: string[] } {
const vars = { ...initVars };
): ParseGradleResult {
const vars: PackageVariables = { ...initVars };
const deps: PackageDependency<ManagerData>[] = [];
const urls = [];
@ -493,7 +499,7 @@ export function parseGradle(
prevTokensLength = tokens.length;
}
return { deps, urls };
return { deps, urls, vars };
}
const propWord = '[a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*';