Skip to content

Commit

Permalink
feat(bazel): Support for maven.artifact positional args (#20471)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Feb 17, 2023
1 parent 1dad564 commit e87af92
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
14 changes: 13 additions & 1 deletion lib/modules/manager/bazel/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ describe('modules/manager/bazel/parser', () => {
group = "com.example2",
artifact = "bar",
version = "2.2.2",
),
maven.artifact(
"com.example3",
"baz",
"3.3.3",
neverlink = True
)
],
repositories = [
Expand All @@ -246,10 +252,16 @@ describe('modules/manager/bazel/parser', () => {
'com.example1:foo:1.1.1',
{
_function: 'maven.artifact',
artifact: 'bar',
group: 'com.example2',
artifact: 'bar',
version: '2.2.2',
},
{
_function: 'maven.artifact',
'0': 'com.example3',
'1': 'baz',
'2': '3.3.3',
},
],
repositories: [
'https://example1.com/maven2',
Expand Down
21 changes: 15 additions & 6 deletions lib/modules/manager/bazel/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Ctx {
stack: NestedFragment[];
recordKey?: string;
subRecordKey?: string;
argIndex?: number;
}

function emptyCtx(source: string): Ctx {
Expand Down Expand Up @@ -140,13 +141,18 @@ const kwParams = q
startsWith: '(',
endsWith: ')',
search: q
.sym<Ctx>((ctx, { value: subRecordKey }) => ({
...ctx,
subRecordKey,
}))
.op('=')
.opt(
q
.sym<Ctx>((ctx, { value: subRecordKey }) => ({
...ctx,
subRecordKey,
}))
.op('=')
)
.str((ctx, { value: subRecordValue, offset }) => {
const subRecordKey = ctx.subRecordKey!;
const argIndex = ctx.argIndex ?? 0;

const subRecordKey = ctx.subRecordKey! ?? argIndex.toString();
const ruleFragment = currentFragment(ctx);
if (ruleFragment.type === 'record') {
ruleFragment.children[subRecordKey] = {
Expand All @@ -156,9 +162,12 @@ const kwParams = q
};
}
delete ctx.subRecordKey;
ctx.argIndex = argIndex + 1;
return ctx;
}),
postHandler: (ctx, tree) => {
delete ctx.argIndex;

const callFrag = currentFragment(ctx);
ctx.stack.pop();
if (callFrag.type === 'record' && tree.type === 'wrapped-tree') {
Expand Down
20 changes: 18 additions & 2 deletions lib/modules/manager/bazel/rules/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,17 @@ describe('modules/manager/bazel/rules/index', () => {
artifacts: [
'com.example1:foo:1.1.1',
{
artifact: 'bar',
function: 'maven.artifact',
_function: 'maven.artifact',
group: 'com.example2',
artifact: 'bar',
version: '2.2.2',
},
{
_function: 'maven.artifact',
'0': 'com.example3',
'1': 'baz',
'2': '3.3.3',
},
],
repositories: [
'https://example1.com/maven2',
Expand Down Expand Up @@ -389,6 +395,16 @@ describe('modules/manager/bazel/rules/index', () => {
'https://example2.com/maven2',
],
},
{
currentValue: '3.3.3',
datasource: 'maven',
depType: 'maven_install',
depName: 'com.example3:baz',
registryUrls: [
'https://example1.com/maven2',
'https://example2.com/maven2',
],
},
]);
});
});
Expand Down
19 changes: 14 additions & 5 deletions lib/modules/manager/bazel/rules/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import type { PackageDependency } from '../../types';

export const mavenRules = ['maven_install'] as const;

const ArtifactSpec = z.object({
group: z.string(),
artifact: z.string(),
version: z.string(),
});
const ArtifactSpec = z.union([
z.object({
group: z.string(),
artifact: z.string(),
version: z.string(),
}),
z
.object({
'0': z.string(),
'1': z.string(),
'2': z.string(),
})
.transform((x) => ({ group: x[0], artifact: x[1], version: x[2] })),
]);
type ArtifactSpec = z.infer<typeof ArtifactSpec>;

export const MavenTarget = z
Expand Down

0 comments on commit e87af92

Please sign in to comment.