Skip to content

Commit

Permalink
fix(gradle): Support gradle versions as templates (#13576)
Browse files Browse the repository at this point in the history
* fix(gradle): Support gradle versions as templates

* More test
  • Loading branch information
zharinov committed Jan 16, 2022
1 parent 0fdede3 commit ee55afb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/manager/gradle/shallow/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ describe('manager/gradle/shallow/parser', () => {
${'baz = "1.2.3"'} | ${'id "foo.bar" version baz'} | ${{ depName: 'foo.bar', lookupName: 'foo.bar:foo.bar.gradle.plugin', currentValue: '1.2.3' }}
${'baz = "1.2.3"'} | ${'id("foo.bar") version baz'} | ${{ depName: 'foo.bar', lookupName: 'foo.bar:foo.bar.gradle.plugin', currentValue: '1.2.3' }}
${'baz = "1.3.71"'} | ${'kotlin("jvm") version baz'} | ${{ depName: 'org.jetbrains.kotlin.jvm', lookupName: 'org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin', currentValue: '1.3.71' }}
${'z = "1.2.3"'} | ${'id "x.y" version "$z"'} | ${{ depName: 'x.y', lookupName: 'x.y:x.y.gradle.plugin', currentValue: '1.2.3' }}
${''} | ${'id "x.y" version "$z"'} | ${{ depName: 'x.y', skipReason: SkipReason.UnknownVersion, currentValue: 'z' }}
${''} | ${'id "x.y" version "x${y}z"'} | ${{ depName: 'x.y', skipReason: SkipReason.UnknownVersion }}
${'z = "1.2.3"'} | ${'id("x.y") version "$z"'} | ${{ depName: 'x.y', lookupName: 'x.y:x.y.gradle.plugin', currentValue: '1.2.3' }}
${''} | ${'id("x.y") version "$z"'} | ${{ depName: 'x.y', skipReason: SkipReason.UnknownVersion, currentValue: 'z' }}
${''} | ${'id("x.y") version "x${y}z"'} | ${{ depName: 'x.y', skipReason: SkipReason.UnknownVersion }}
`('$input', ({ def, input, output }) => {
const { deps } = parseGradle([def, input].join('\n'));
expect(deps).toMatchObject([output].filter(Boolean));
Expand Down
39 changes: 37 additions & 2 deletions lib/manager/gradle/shallow/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,29 @@ function processPlugin({
dep.managerData = { fileReplacePosition, packageFile };
dep.skipReason = SkipReason.UnknownVersion;
}
} else if (pluginVersion.type === TokenType.StringInterpolation) {
const versionTpl = pluginVersion as StringInterpolation;
const children = versionTpl.children;
const [child] = children;
if (child?.type === TokenType.Variable && children.length === 1) {
const varData = variables[child.value];
if (varData) {
const currentValue = varData.value;
const fileReplacePosition = varData.fileReplacePosition;
dep.currentValue = currentValue;
dep.managerData = { fileReplacePosition, packageFile };
} else {
const currentValue = child.value;
const fileReplacePosition = child.offset;
dep.currentValue = currentValue;
dep.managerData = { fileReplacePosition, packageFile };
dep.skipReason = SkipReason.UnknownVersion;
}
} else {
const fileReplacePosition = versionTpl.offset;
dep.managerData = { fileReplacePosition, packageFile };
dep.skipReason = SkipReason.UnknownVersion;
}
} else {
const currentValue = pluginVersion.value;
const fileReplacePosition = pluginVersion.offset;
Expand Down Expand Up @@ -372,6 +395,8 @@ const matcherConfigs: SyntaxMatchConfig[] = [
},
{
// id 'foo.bar' version '1.2.3'
// id 'foo.bar' version fooBarVersion
// id 'foo.bar' version "$fooBarVersion"
matchers: [
{
matchType: TokenType.Word,
Expand All @@ -381,7 +406,11 @@ const matcherConfigs: SyntaxMatchConfig[] = [
{ matchType: TokenType.String, tokenMapKey: 'pluginName' },
{ matchType: TokenType.Word, matchValue: 'version' },
{
matchType: [TokenType.String, TokenType.Word],
matchType: [
TokenType.String,
TokenType.Word,
TokenType.StringInterpolation,
],
tokenMapKey: 'pluginVersion',
},
endOfInstruction,
Expand All @@ -390,6 +419,8 @@ const matcherConfigs: SyntaxMatchConfig[] = [
},
{
// id('foo.bar') version '1.2.3'
// id('foo.bar') version fooBarVersion
// id('foo.bar') version "$fooBarVersion"
matchers: [
{
matchType: TokenType.Word,
Expand All @@ -401,7 +432,11 @@ const matcherConfigs: SyntaxMatchConfig[] = [
{ matchType: TokenType.RightParen },
{ matchType: TokenType.Word, matchValue: 'version' },
{
matchType: [TokenType.String, TokenType.Word],
matchType: [
TokenType.String,
TokenType.Word,
TokenType.StringInterpolation,
],
tokenMapKey: 'pluginVersion',
},
endOfInstruction,
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/gradle/shallow/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const lexer = moo.states({
[TokenType.Comma]: ',',
[TokenType.Operator]: /(?:==|\+=?|-=?|\/=?|\*\*?|\.+|:)/, // TODO #12870
[TokenType.Assignment]: '=',
[TokenType.Word]: { match: /[a-zA-Z$_][a-zA-Z0-9$_]+/ }, // TODO #12870
[TokenType.Word]: { match: /[a-zA-Z$_][a-zA-Z0-9$_]*/ }, // TODO #12870
[TokenType.LeftParen]: { match: '(' },
[TokenType.RightParen]: { match: ')' },
[TokenType.LeftBracket]: { match: '[' },
Expand Down

0 comments on commit ee55afb

Please sign in to comment.