Skip to content

Commit

Permalink
refactor(manager/gradle): split extractAllPackageFiles into multiple …
Browse files Browse the repository at this point in the history
…sub-methods
  • Loading branch information
Churro committed Apr 11, 2023
1 parent 7fdb8e2 commit d53c435
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 42 deletions.
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 };
}

0 comments on commit d53c435

Please sign in to comment.