diff --git a/lib/modules/manager/gradle/parser.spec.ts b/lib/modules/manager/gradle/parser.spec.ts index d4903f5a0feb887baf28da3e511732e4085ea845..d33ed1ee31d924dae8a3a8f7b98e7b21d46edcf9 100644 --- a/lib/modules/manager/gradle/parser.spec.ts +++ b/lib/modules/manager/gradle/parser.spec.ts @@ -63,14 +63,16 @@ describe('modules/manager/gradle/parser', () => { describe('variable substitutions', () => { test.each` - def | str | output - ${'foo = "1.2.3"'} | ${'"foo:bar:$foo@@@"'} | ${null} - ${'baz = "1.2.3"'} | ${'"foo:bar:$baz"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'baz' }} - ${'foo.bar = "1.2.3"'} | ${'"foo:bar:$foo.bar"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'foo.bar' }} - ${'foo = "1.2.3"'} | ${'"foo:bar_$foo:4.5.6"'} | ${{ depName: 'foo:bar_1.2.3', managerData: { fileReplacePosition: 28 } }} - ${''} | ${'foo.bar = "foo:bar:1.2.3"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3' }} - ${'baz = "1.2.3"'} | ${'foobar = "foo:bar:$baz"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'baz' }} - ${'baz = "1.2.3"'} | ${'group: "foo", name: "bar", version: baz'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'baz' }} + def | str | output + ${'foo = "1.2.3"'} | ${'"foo:bar:$foo@@@"'} | ${null} + ${'baz = "1.2.3"'} | ${'"foo:bar:$baz"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'baz' }} + ${'foo.bar = "1.2.3"'} | ${'"foo:bar:$foo.bar"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'foo.bar' }} + ${'foo = "1.2.3"'} | ${'"foo:bar_$foo:4.5.6"'} | ${{ depName: 'foo:bar_1.2.3', managerData: { fileReplacePosition: 28 } }} + ${''} | ${'foo.bar = "foo:bar:1.2.3"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3' }} + ${'baz = "1.2.3"'} | ${'foobar = "foo:bar:$baz"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'baz' }} + ${'baz = "1.2.3"'} | ${'group: "foo", name: "bar", version: baz'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'baz' }} + ${'baz = "1.2.3"'} | ${'library("foo.bar", "foo", "bar").versionRef("baz")'} | ${{ depName: 'foo:bar', currentValue: '1.2.3', groupName: 'baz' }} + ${'library("foo.bar", "foo", "bar")'} | ${'"${foo.bar}:1.2.3"'} | ${{ depName: 'foo:bar', currentValue: '1.2.3' }} `('$def | $str', ({ def, str, output }) => { const input = [def, str].join('\n'); const { deps } = parseGradle(input); diff --git a/lib/modules/manager/gradle/parser.ts b/lib/modules/manager/gradle/parser.ts index 44a967ed3f8d43260bc40bb39c0125bd9529b834..58f1041afb2e915e493c8069afac532c8871bf3d 100644 --- a/lib/modules/manager/gradle/parser.ts +++ b/lib/modules/manager/gradle/parser.ts @@ -349,6 +349,34 @@ function processLongFormDep({ return null; } +function processLibraryDep(input: SyntaxHandlerInput): SyntaxHandlerOutput { + const { tokenMap } = input; + + const varNameToken = tokenMap.varName; + const key = varNameToken.value; + const fileReplacePosition = varNameToken.offset; + const packageFile = input.packageFile; + + const groupId = tokenMap.groupId?.value; + const artifactId = tokenMap.artifactId?.value; + const value = `${groupId}:${artifactId}`; + const res: SyntaxHandlerOutput = {}; + + if (groupId && artifactId) { + res.vars = { [key]: { key, value, fileReplacePosition, packageFile } }; + const versionRefToken = tokenMap.version; + if (versionRefToken) { + const version: Token = { ...versionRefToken, type: TokenType.Word }; + const depRes = processLongFormDep({ + ...input, + tokenMap: { ...input.tokenMap, version }, + }); + return { ...depRes, ...res }; + } + } + return res; +} + const matcherConfigs: SyntaxMatchConfig[] = [ { // foo.bar = 'baz' @@ -375,7 +403,7 @@ const matcherConfigs: SyntaxMatchConfig[] = [ { // set('foo', 'bar') matchers: [ - { matchType: TokenType.Word, matchValue: 'set' }, + { matchType: TokenType.Word, matchValue: ['set', 'version'] }, { matchType: TokenType.LeftParen }, { matchType: TokenType.String, tokenMapKey: 'keyToken' }, { matchType: TokenType.Comma }, @@ -584,6 +612,39 @@ const matcherConfigs: SyntaxMatchConfig[] = [ ], handler: processCustomRegistryUrl, }, + { + // library("foobar", "foo", "bar").versionRef("foo.bar") + matchers: [ + { matchType: TokenType.Word, matchValue: 'library' }, + { matchType: TokenType.LeftParen }, + { matchType: TokenType.String, tokenMapKey: 'varName' }, + { matchType: TokenType.Comma }, + { matchType: potentialStringTypes, tokenMapKey: 'groupId' }, + { matchType: TokenType.Comma }, + { matchType: potentialStringTypes, tokenMapKey: 'artifactId' }, + { matchType: TokenType.RightParen }, + { matchType: TokenType.Dot }, + { matchType: TokenType.Word, matchValue: 'versionRef' }, + { matchType: TokenType.LeftParen }, + { matchType: TokenType.String, tokenMapKey: 'version' }, + { matchType: TokenType.RightParen }, + ], + handler: processLibraryDep, + }, + { + // library("foobar", "foo", "bar") + matchers: [ + { matchType: TokenType.Word, matchValue: 'library' }, + { matchType: TokenType.LeftParen }, + { matchType: TokenType.String, tokenMapKey: 'varName' }, + { matchType: TokenType.Comma }, + { matchType: potentialStringTypes, tokenMapKey: 'groupId' }, + { matchType: TokenType.Comma }, + { matchType: potentialStringTypes, tokenMapKey: 'artifactId' }, + { matchType: TokenType.RightParen }, + ], + handler: processLibraryDep, + }, { // group: "com.example", name: "my.dependency", version: "1.2.3" matchers: [