Skip to content

Commit cd08eff

Browse files
committed
Support updating versions in set forms in gradle
1 parent 749f2a8 commit cd08eff

3 files changed

Lines changed: 66 additions & 12 deletions

File tree

‎.github/actions/update-project-versions/__tests__/index.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,28 @@ describe('updateBuildGradleContent', () => {
297297
expect(updated).toContain(`description = 'My project'`);
298298
});
299299

300+
it('updates springCloudVersion declared with set() in an ext block', () => {
301+
const content = `ext {\n\tset('springCloudVersion', "2024.0.1-SNAPSHOT")\n}\n`;
302+
const { updated, updatedProperties } = updateBuildGradleContent(
303+
content, '4.2.9', { 'spring-cloud': '2024.0.9' });
304+
expect(updated).toBe(`ext {\n\tset('springCloudVersion', "2024.0.9")\n}\n`);
305+
expect(updatedProperties).toEqual(['springCloudVersion: 2024.0.9']);
306+
});
307+
308+
it('handles set() with either quote style and spacing', () => {
309+
const content = `ext {\n set( "springCloudVersion" , 'x' )\n}\n`;
310+
const { updated } = updateBuildGradleContent(content, '4.2.9', { 'spring-cloud': '2024.0.9' });
311+
expect(updated).toBe(`ext {\n set( "springCloudVersion" , '2024.0.9' )\n}\n`);
312+
});
313+
314+
it('leaves set() of a key that is not a project version alone', () => {
315+
const content = `ext {\n\tset('javaVersion', "17")\n\tset('springCloudVersion', "2024.0.9")\n}\n`;
316+
const { updated, updatedProperties } = updateBuildGradleContent(
317+
content, '4.2.9', { 'spring-cloud': '2024.0.9' });
318+
expect(updated).toBe(content);
319+
expect(updatedProperties).toEqual([]);
320+
});
321+
300322
// spring-cloud-function's Gradle samples declare these in an ext block rather than in
301323
// gradle.properties, so a release used to leave them at whatever they had been pinned at
302324
// - Boot 2.1.0.BUILD-SNAPSHOT in a train releasing against Boot 4.

‎.github/actions/update-project-versions/dist/index.js‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28857,20 +28857,36 @@ function updateBuildGradleContent(content, projectVersion, versions = {}) {
2885728857
//
2885828858
// A key that resolves to no project is left alone, which is what keeps `javaVersion` and
2885928859
// similar build settings out of it.
28860+
//
28861+
// Two spellings of the same extra property are recognised:
28862+
// springCloudVersion = "2024.0.1-SNAPSHOT"
28863+
// set('springCloudVersion', "2024.0.1-SNAPSHOT") <- what start.spring.io generates
2886028864
const updatedProperties = [];
28865+
const ASSIGNMENT = /^(\s*)([a-zA-Z][a-zA-Z0-9]*Version)(\s*=\s*)(['"])([^'"]+)(['"])(.*)$/;
28866+
const SET_CALL = /^(\s*set\(\s*)(['"])([a-zA-Z][a-zA-Z0-9]*Version)(['"])(\s*,\s*)(['"])([^'"]+)(['"])(.*)$/;
2886128867
const updated = withProjectVersion.split('\n').map((line) => {
28862-
const match = line.match(
28863-
/^(\s*)([a-zA-Z][a-zA-Z0-9]*Version)(\s*=\s*)(['"])([^'"]+)(['"])(.*)$/
28864-
);
28865-
if (!match) return line;
28868+
let key, currentValue, rewrite;
28869+
let match = line.match(ASSIGNMENT);
28870+
if (match) {
28871+
const [, indent, k, separator, openQuote, value, closeQuote, trailing] = match;
28872+
key = k;
28873+
currentValue = value;
28874+
rewrite = v => `${indent}${k}${separator}${openQuote}${v}${closeQuote}${trailing}`;
28875+
} else if ((match = line.match(SET_CALL))) {
28876+
const [, head, keyOpen, k, keyClose, comma, openQuote, value, closeQuote, trailing] = match;
28877+
key = k;
28878+
currentValue = value;
28879+
rewrite = v => `${head}${keyOpen}${k}${keyClose}${comma}${openQuote}${v}${closeQuote}${trailing}`;
28880+
} else {
28881+
return line;
28882+
}
2886628883

28867-
const [, indent, key, separator, openQuote, currentValue, closeQuote, trailing] = match;
2886828884
const projectName = camelToKebab(key.slice(0, -'Version'.length));
2886928885
const targetVersion = versions[projectName];
2887028886
if (!targetVersion || currentValue === targetVersion) return line;
2887128887

2887228888
updatedProperties.push(`${key}: ${targetVersion}`);
28873-
return `${indent}${key}${separator}${openQuote}${targetVersion}${closeQuote}${trailing}`;
28889+
return rewrite(targetVersion);
2887428890
}).join('\n');
2887528891

2887628892
// Inline dependency coordinates - "group:artifact:version" - for artifacts this train

‎.github/actions/update-project-versions/src/index.js‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,20 +632,36 @@ function updateBuildGradleContent(content, projectVersion, versions = {}) {
632632
//
633633
// A key that resolves to no project is left alone, which is what keeps `javaVersion` and
634634
// similar build settings out of it.
635+
//
636+
// Two spellings of the same extra property are recognised:
637+
// springCloudVersion = "2024.0.1-SNAPSHOT"
638+
// set('springCloudVersion', "2024.0.1-SNAPSHOT") <- what start.spring.io generates
635639
const updatedProperties = [];
640+
const ASSIGNMENT = /^(\s*)([a-zA-Z][a-zA-Z0-9]*Version)(\s*=\s*)(['"])([^'"]+)(['"])(.*)$/;
641+
const SET_CALL = /^(\s*set\(\s*)(['"])([a-zA-Z][a-zA-Z0-9]*Version)(['"])(\s*,\s*)(['"])([^'"]+)(['"])(.*)$/;
636642
const updated = withProjectVersion.split('\n').map((line) => {
637-
const match = line.match(
638-
/^(\s*)([a-zA-Z][a-zA-Z0-9]*Version)(\s*=\s*)(['"])([^'"]+)(['"])(.*)$/
639-
);
640-
if (!match) return line;
643+
let key, currentValue, rewrite;
644+
let match = line.match(ASSIGNMENT);
645+
if (match) {
646+
const [, indent, k, separator, openQuote, value, closeQuote, trailing] = match;
647+
key = k;
648+
currentValue = value;
649+
rewrite = v => `${indent}${k}${separator}${openQuote}${v}${closeQuote}${trailing}`;
650+
} else if ((match = line.match(SET_CALL))) {
651+
const [, head, keyOpen, k, keyClose, comma, openQuote, value, closeQuote, trailing] = match;
652+
key = k;
653+
currentValue = value;
654+
rewrite = v => `${head}${keyOpen}${k}${keyClose}${comma}${openQuote}${v}${closeQuote}${trailing}`;
655+
} else {
656+
return line;
657+
}
641658

642-
const [, indent, key, separator, openQuote, currentValue, closeQuote, trailing] = match;
643659
const projectName = camelToKebab(key.slice(0, -'Version'.length));
644660
const targetVersion = versions[projectName];
645661
if (!targetVersion || currentValue === targetVersion) return line;
646662

647663
updatedProperties.push(`${key}: ${targetVersion}`);
648-
return `${indent}${key}${separator}${openQuote}${targetVersion}${closeQuote}${trailing}`;
664+
return rewrite(targetVersion);
649665
}).join('\n');
650666

651667
// Inline dependency coordinates - "group:artifact:version" - for artifacts this train

0 commit comments

Comments
 (0)