Skip to content

Commit

Permalink
refactor(gradle): extract redundant functions and remove snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Churro committed Jan 6, 2025
1 parent 8641996 commit a69d7f9
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 150 deletions.
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 @@ -791,7 +791,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
12 changes: 12 additions & 0 deletions lib/modules/manager/gradle/parser/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,15 @@ export const qDotOrBraceExpr = (
}),
),
);

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,8 +4,11 @@ import type { Ctx } from '../types';
import {
GRADLE_PLUGINS,
cleanupTempVars,
qArtifactId,
qGroupId,
qTemplateString,
qValueMatcher,
qVersion,
storeInTokenMap,
storeVarToken,
} from './common';
Expand All @@ -16,18 +19,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

0 comments on commit a69d7f9

Please sign in to comment.