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(gradle): extract redundant functions and remove snapshot #33430

Merged
merged 4 commits into from
Jan 9, 2025
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
103 changes: 0 additions & 103 deletions lib/modules/manager/gradle/__snapshots__/parser.spec.ts.snap

This file was deleted.

101 changes: 100 additions & 1 deletion lib/modules/manager/gradle/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,106 @@ describe('modules/manager/gradle/parser', () => {
content.slice(managerData!.fileReplacePosition).indexOf(currentValue!),
);
expect(replacementIndices.every((idx) => idx === 0)).toBeTrue();
expect(deps).toMatchSnapshot();
expect(deps).toMatchObject([
{
currentValue: '1.5.2.RELEASE',
depName: 'org.springframework.boot:spring-boot-gradle-plugin',
groupName: 'springBootVersion',
managerData: {
fileReplacePosition: 53,
packageFile: 'build.gradle',
},
},
{
currentValue: '1.2.3',
depName: 'com.github.jengelman.gradle.plugins:shadow',
managerData: {
fileReplacePosition: 417,
packageFile: 'build.gradle',
},
},
{
currentValue: '0.1',
depName: 'com.fkorotkov:gradle-libraries-plugin',
managerData: {
fileReplacePosition: 481,
packageFile: 'build.gradle',
},
},
{
currentValue: '0.2.3',
depName:
'gradle.plugin.se.patrikerdes:gradle-use-latest-versions-plugin',
managerData: {
fileReplacePosition: 568,
packageFile: 'build.gradle',
},
},
{
currentValue: '3.1.1',
depName: 'org.apache.openjpa:openjpa',
managerData: {
fileReplacePosition: 621,
packageFile: 'build.gradle',
},
},
{
currentValue: '0.13.0',
depName: 'com.gradle.publish:plugin-publish-plugin',
managerData: {
fileReplacePosition: 688,
packageFile: 'build.gradle',
},
},
{
currentValue: '6.0.9.RELEASE',
depName: 'org.grails:gorm-hibernate5-spring-boot',
managerData: {
fileReplacePosition: 1882,
packageFile: 'build.gradle',
},
},
{
currentValue: '6.0.5',
depName: 'mysql:mysql-connector-java',
managerData: {
fileReplacePosition: 1938,
packageFile: 'build.gradle',
},
},
{
currentValue: '1.0-groovy-2.4',
depName: 'org.spockframework:spock-spring',
managerData: {
fileReplacePosition: 1996,
packageFile: 'build.gradle',
},
},
{
currentValue: '1.3',
depName: 'org.hamcrest:hamcrest-core',
managerData: {
fileReplacePosition: 2101,
packageFile: 'build.gradle',
},
},
{
currentValue: '3.1',
depName: 'cglib:cglib-nodep',
managerData: {
fileReplacePosition: 2189,
packageFile: 'build.gradle',
},
},
{
currentValue: '3.1.1',
depName: 'org.apache.openjpa:openjpa',
managerData: {
fileReplacePosition: 2295,
packageFile: 'build.gradle',
},
},
]);
});
});

Expand Down
30 changes: 20 additions & 10 deletions lib/modules/manager/gradle/parser/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,24 @@ export const qDotOrBraceExpr = (
matcher: q.QueryBuilder<Ctx, parser.Node>,
): q.QueryBuilder<Ctx, parser.Node> =>
q.sym<Ctx>(symValue).alt(
q.alt<Ctx>(
q.op<Ctx>('.').join(matcher),
q.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '{',
endsWith: '}',
search: matcher,
}),
),
q.op<Ctx>('.').join(matcher),
q.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '{',
endsWith: '}',
search: matcher,
}),
);

export const qGroupId = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'groupId'),
);

export const qArtifactId = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'artifactId'),
);

export const qVersion = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'version'),
);
15 changes: 3 additions & 12 deletions lib/modules/manager/gradle/parser/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import type { Ctx } from '../types';
import {
GRADLE_PLUGINS,
cleanupTempVars,
qArtifactId,
qDotOrBraceExpr,
qGroupId,
qTemplateString,
qValueMatcher,
qVersion,
storeInTokenMap,
storeVarToken,
} from './common';
Expand All @@ -17,18 +20,6 @@ import {
handleLongFormDep,
} from './handlers';

const qGroupId = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'groupId'),
);

const qArtifactId = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'artifactId'),
);

const qVersion = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'version'),
);

// "foo:bar:1.2.3"
// "foo:bar:$baz"
// "foo" + "${bar}" + baz
Expand Down
6 changes: 1 addition & 5 deletions lib/modules/manager/gradle/parser/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import type { Ctx } from '../types';
import {
cleanupTempVars,
qStringValue,
qValueMatcher,
qVersion,
storeInTokenMap,
storeVarToken,
} from './common';
import { handlePlugin } from './handlers';

const qVersion = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'version'),
);

export const qPlugins = q
.sym(regEx(/^(?:id|kotlin)$/), storeVarToken)
.handler((ctx) => storeInTokenMap(ctx, 'methodName'))
Expand Down
45 changes: 24 additions & 21 deletions lib/modules/manager/gradle/parser/registry-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const qUri = q
// mavenCentral { ... }
const qPredefinedRegistries = q
.sym(regEx(`^(?:${Object.keys(REGISTRY_URLS).join('|')})$`), storeVarToken)
.handler((ctx) => storeInTokenMap(ctx, 'registryUrl'))
.alt(
q.tree({
type: 'wrapped-tree',
Expand All @@ -45,10 +46,31 @@ const qPredefinedRegistries = q
endsWith: '}',
}),
)
.handler((ctx) => storeInTokenMap(ctx, 'registryUrl'))
.handler(handlePredefinedRegistryUrl)
.handler(cleanupTempVars);

// { url = "https://some.repo" }
const qMavenArtifactRegistry = q.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '{',
endsWith: '}',
search: q.alt(
q
.sym<Ctx>('name')
.opt(q.op('='))
.join(qValueMatcher)
.handler((ctx) => storeInTokenMap(ctx, 'name')),
q.sym<Ctx>('url').opt(q.op('=')).join(qUri),
q.sym<Ctx>('setUrl').tree({
maxDepth: 1,
startsWith: '(',
endsWith: ')',
search: q.begin<Ctx>().join(qUri).end(),
}),
),
});

// maven(url = uri("https://foo.bar/baz"))
// maven { name = some; url = "https://foo.bar/${name}" }
const qCustomRegistryUrl = q
Expand All @@ -61,26 +83,7 @@ const qCustomRegistryUrl = q
endsWith: ')',
search: q.begin<Ctx>().opt(q.sym<Ctx>('url').op('=')).join(qUri).end(),
}),
q.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '{',
endsWith: '}',
search: q.alt(
q
.sym<Ctx>('name')
.opt(q.op('='))
.join(qValueMatcher)
.handler((ctx) => storeInTokenMap(ctx, 'name')),
q.sym<Ctx>('url').opt(q.op('=')).join(qUri),
q.sym<Ctx>('setUrl').tree({
maxDepth: 1,
startsWith: '(',
endsWith: ')',
search: q.begin<Ctx>().join(qUri).end(),
}),
),
}),
qMavenArtifactRegistry,
)
.handler(handleCustomRegistryUrl)
.handler(cleanupTempVars);
Expand Down
10 changes: 2 additions & 8 deletions lib/modules/manager/gradle/parser/version-catalogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { query as q } from 'good-enough-parser';
import type { Ctx } from '../types';
import {
cleanupTempVars,
qArtifactId,
qGroupId,
qStringValue,
qStringValueAsSymbol,
qValueMatcher,
Expand All @@ -10,14 +12,6 @@ import {
} from './common';
import { handleLibraryDep, handlePlugin } from './handlers';

const qGroupId = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'groupId'),
);

const qArtifactId = qValueMatcher.handler((ctx) =>
storeInTokenMap(ctx, 'artifactId'),
);

const qVersionCatalogVersion = q
.op<Ctx>('.')
.alt(
Expand Down
Loading