Skip to content

Commit

Permalink
feat(gradle): add interpolation for local name variable in registry U…
Browse files Browse the repository at this point in the history
…RL (#16136)
  • Loading branch information
Churro committed Jun 22, 2022
1 parent dca5f9d commit b9a400a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
9 changes: 7 additions & 2 deletions lib/modules/manager/gradle/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ describe('modules/manager/gradle/extract', () => {
maven {
url = "\${repositoryBaseURL}/repository-build"
}
maven {
name = "baz"
url = "\${repositoryBaseURL}/\${name}"
}
}
dependencies {
Expand Down Expand Up @@ -230,13 +234,14 @@ describe('modules/manager/gradle/extract', () => {
depName: 'com.google.protobuf:protobuf-java',
currentValue: '2.17.0',
managerData: {
fileReplacePosition: 227,
fileReplacePosition: 335,
packageFile: 'build.gradle',
},
fileReplacePosition: 227,
fileReplacePosition: 335,
registryUrls: [
'https://repo.maven.apache.org/maven2',
'https://dummy.org/whatever/repository-build',
'https://dummy.org/whatever/baz',
],
},
],
Expand Down
41 changes: 21 additions & 20 deletions lib/modules/manager/gradle/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,27 @@ describe('modules/manager/gradle/parser', () => {

describe('registryUrls', () => {
test.each`
def | input | url
${''} | ${'url ""'} | ${null}
${''} | ${'url "#!@"'} | ${null}
${''} | ${'url "https://example.com"'} | ${'https://example.com'}
${'base="https://foo.bar"'} | ${'url "${base}/baz"'} | ${'https://foo.bar/baz'}
${''} | ${'url("https://example.com")'} | ${'https://example.com'}
${'base="https://foo.bar"'} | ${'url("${base}/baz")'} | ${'https://foo.bar/baz'}
${''} | ${'mavenCentral()'} | ${MAVEN_REPO}
${''} | ${'jcenter()'} | ${JCENTER_REPO}
${''} | ${'google()'} | ${GOOGLE_REPO}
${''} | ${'google { content { includeGroup "foo" } }'} | ${GOOGLE_REPO}
${''} | ${'gradlePluginPortal()'} | ${GRADLE_PLUGIN_PORTAL_REPO}
${''} | ${'maven("https://foo.bar/baz/qux")'} | ${'https://foo.bar/baz/qux'}
${'base="https://foo.bar"'} | ${'maven("${base}/baz/qux")'} | ${'https://foo.bar/baz/qux'}
${''} | ${'maven { url = uri("https://foo.bar/baz")'} | ${'https://foo.bar/baz'}
${'base="https://foo.bar"'} | ${'maven { url = uri("${base}/baz")'} | ${'https://foo.bar/baz'}
${''} | ${"maven { url 'https://foo.bar/baz'"} | ${'https://foo.bar/baz'}
${'base="https://foo.bar"'} | ${'maven { url "${base}/baz"'} | ${'https://foo.bar/baz'}
${''} | ${"maven { url = 'https://foo.bar/baz'"} | ${'https://foo.bar/baz'}
${'base="https://foo.bar"'} | ${'maven { url = "${base}/baz"'} | ${'https://foo.bar/baz'}
def | input | url
${''} | ${'url ""'} | ${null}
${''} | ${'url "#!@"'} | ${null}
${''} | ${'url "https://example.com"'} | ${'https://example.com'}
${'base="https://foo.bar"'} | ${'url "${base}/baz"'} | ${'https://foo.bar/baz'}
${''} | ${'url("https://example.com")'} | ${'https://example.com'}
${'base="https://foo.bar"'} | ${'url("${base}/baz")'} | ${'https://foo.bar/baz'}
${''} | ${'mavenCentral()'} | ${MAVEN_REPO}
${''} | ${'jcenter()'} | ${JCENTER_REPO}
${''} | ${'google()'} | ${GOOGLE_REPO}
${''} | ${'google { content { includeGroup "foo" } }'} | ${GOOGLE_REPO}
${''} | ${'gradlePluginPortal()'} | ${GRADLE_PLUGIN_PORTAL_REPO}
${''} | ${'maven("https://foo.bar/baz/qux")'} | ${'https://foo.bar/baz/qux'}
${'base="https://foo.bar"'} | ${'maven("${base}/baz/qux")'} | ${'https://foo.bar/baz/qux'}
${''} | ${'maven { url = uri("https://foo.bar/baz")'} | ${'https://foo.bar/baz'}
${'base="https://foo.bar"'} | ${'maven { url = uri("${base}/baz")'} | ${'https://foo.bar/baz'}
${''} | ${"maven { url 'https://foo.bar/baz'"} | ${'https://foo.bar/baz'}
${'base="https://foo.bar"'} | ${'maven { url "${base}/baz"'} | ${'https://foo.bar/baz'}
${''} | ${"maven { url = 'https://foo.bar/baz'"} | ${'https://foo.bar/baz'}
${'base="https://foo.bar"'} | ${'maven { url = "${base}/baz"'} | ${'https://foo.bar/baz'}
${'base="https://foo.bar"'} | ${'maven { name = "baz"\nurl = "${base}/${name}"'} | ${'https://foo.bar/baz'}
`('$def | $input', ({ def, input, url }) => {
const expected = [url].filter(Boolean);
const { urls } = parseGradle([def, input].join('\n'));
Expand Down
44 changes: 43 additions & 1 deletion lib/modules/manager/gradle/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,21 @@ function processCustomRegistryUrl({
tokenMap,
variables,
}: SyntaxHandlerInput): SyntaxHandlerOutput {
let localVariables = variables;
if (tokenMap.keyToken?.value === 'name') {
localVariables = {
...variables,
name: {
key: 'name',
value: tokenMap.valToken.value,
},
};
}

let registryUrl: string | null = tokenMap.registryUrl?.value;
if (tokenMap.registryUrl?.type === TokenType.StringInterpolation) {
const token = tokenMap.registryUrl as StringInterpolation;
registryUrl = interpolateString(token.children, variables);
registryUrl = interpolateString(token.children, localVariables);
}

try {
Expand Down Expand Up @@ -556,6 +567,37 @@ const matcherConfigs: SyntaxMatchConfig[] = [
],
handler: processCustomRegistryUrl,
},
{
// maven { name = "baz"; url = "https://maven.springframework.org/${name}" }
matchers: [
{
matchType: TokenType.Word,
matchValue: 'maven',
},
{ matchType: TokenType.LeftBrace },
{
matchType: TokenType.Word,
matchValue: 'name',
tokenMapKey: 'keyToken',
},
{ matchType: TokenType.Assignment },
{
matchType: [TokenType.String, TokenType.StringInterpolation],
tokenMapKey: 'valToken',
},
{
matchType: TokenType.Word,
matchValue: 'url',
},
{ matchType: TokenType.Assignment },
{
matchType: [TokenType.String, TokenType.StringInterpolation],
tokenMapKey: 'registryUrl',
},
endOfInstruction,
],
handler: processCustomRegistryUrl,
},
{
// maven { url = "https://maven.springframework.org/release"
matchers: [
Expand Down

0 comments on commit b9a400a

Please sign in to comment.