Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(manager/gradle): split extractAllPackageFiles into multiple sub-methods #21429

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 49 additions & 33 deletions lib/modules/manager/gradle/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { REGISTRY_URLS } from './parser/common';
import type {
GradleManagerData,
PackageRegistry,
PackageVariables,
VariableRegistry,
} from './types';
import {
Expand All @@ -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<GradleManagerData>
Expand All @@ -45,18 +59,17 @@ function getRegistryUrlsForDep(
return [...new Set(registryUrls)];
}

export async function extractAllPackageFiles(
async function parsePackageFiles(
config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFile[] | null> {
const extractedDeps: PackageDependency<GradleManagerData>[] = [];
packageFiles: string[],
extractedDeps: PackageDependency<GradleManagerData>[],
packageFilesByName: Record<string, PackageFile>,
packageRegistries: PackageRegistry[]
): Promise<PackageDependency<GradleManagerData>[]> {
const varRegistry: VariableRegistry = {};
const packageFilesByName: Record<string, PackageFile> = {};
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,
Expand All @@ -68,45 +81,28 @@ 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 {
deps,
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) {
Expand All @@ -117,6 +113,26 @@ export async function extractAllPackageFiles(
}
}

return extractedDeps;
}

export async function extractAllPackageFiles(
config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFile[] | null> {
const packageFilesByName: Record<string, PackageFile> = {};
const packageRegistries: PackageRegistry[] = [];
const extractedDeps: PackageDependency<GradleManagerData>[] = [];
const gradleFiles = reorderFiles(packageFiles);

await parsePackageFiles(
config,
gradleFiles,
extractedDeps,
packageFilesByName,
packageRegistries
);

if (!extractedDeps.length) {
return null;
}
Expand Down
25 changes: 16 additions & 9 deletions lib/modules/manager/gradle/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions lib/modules/manager/gradle/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
parseDependencyString,
reorderFiles,
toAbsolutePath,
updateVars,
versionLikeSubstring,
} from './utils';

Expand Down Expand Up @@ -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' },
});
});
});
9 changes: 9 additions & 0 deletions lib/modules/manager/gradle/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}