Skip to content

Commit fd7b73b

Browse files
authored
fix(dart): add multiline flag to replace regex (#1524)
The dart `pubspec.yaml` is updated here with regex, and the file contains newlines. To properly match the replacement, the `/m` flag is needed for multiline match and replace. Prior to this fix, `pubspec.yaml` was never updated with the dart strategy. Fixes #1523
1 parent ac9318b commit fd7b73b

4 files changed

Lines changed: 50 additions & 11 deletions

File tree

__snapshots__/pubspec-yaml.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
exports['PubspecYaml updateContent leaves malformatted build numbers alone in pubspec.yaml file 1'] = `
2+
name: hello_world
3+
description: Hello World
4+
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
5+
6+
version: 0.6.0+abc
7+
8+
environment:
9+
sdk: '>=2.12.0 <3.0.0'
10+
11+
`
12+
113
exports['PubspecYaml updateContent updates version in pubspec.yaml file 1'] = `
214
name: hello_world
315
description: Hello World
416
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
517
6-
version: 0.5.0
18+
version: 0.6.0
719
820
environment:
921
sdk: '>=2.12.0 <3.0.0'
@@ -15,7 +27,7 @@ name: hello_world
1527
description: Hello World
1628
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
1729
18-
version: 0.5.0+12
30+
version: 0.6.0+13
1931
2032
environment:
2133
sdk: '>=2.12.0 <3.0.0'

src/updaters/dart/pubspec-yaml.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,30 @@ export class PubspecYaml extends DefaultUpdater {
2424
* @param {string} content The initial content
2525
* @returns {string} The updated content
2626
*/
27+
2728
updateContent(content: string): string {
28-
const oldVersion = content.match(/version: ([0-9.]+)\+?([0-9]*$)/);
29+
const oldVersion = content.match(/^version: ([0-9.]+)\+?(.*$)/m);
2930
let buildNumber = '';
3031

3132
if (oldVersion) {
32-
buildNumber = `${oldVersion[2]}`;
33-
if (buildNumber.length > 0) {
33+
buildNumber = oldVersion[2];
34+
const parsedBuild = parseInt(buildNumber);
35+
if (!isNaN(parsedBuild)) {
36+
buildNumber = `+${parsedBuild + 1}`;
37+
logger.info(
38+
`updating from ${oldVersion[1]}+${oldVersion[2]} to ${this.version}${buildNumber}`
39+
);
40+
} else if (buildNumber.length > 0) {
3441
buildNumber = `+${buildNumber}`;
3542
logger.info(
36-
`updating from ${oldVersion[1]}${buildNumber} to ${this.version}${buildNumber}`
43+
`updating from ${oldVersion[1]}+${oldVersion[2]} to ${this.version}${buildNumber}`
3744
);
3845
} else {
3946
logger.info(`updating from ${oldVersion[1]} to ${this.version}`);
4047
}
4148
}
4249
return content.replace(
43-
/version: ([0-9.]+)\+?([0-9]*$)/,
50+
/^version: .*$/m,
4451
`version: ${this.version}${buildNumber}`
4552
);
4653
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: hello_world
2+
description: Hello World
3+
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
4+
5+
version: 0.5.0+abc
6+
7+
environment:
8+
sdk: '>=2.12.0 <3.0.0'

test/updaters/pubspec-yaml.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,36 @@ describe('PubspecYaml', () => {
2727
const oldContent = readFileSync(
2828
resolve(fixturesPath, './pubspec.yaml'),
2929
'utf8'
30-
).replace(/\r\n/g, '\n');
30+
).replace(/\r\n/g, '\n'); // required for windows
3131
const version = new PubspecYaml({
3232
version: Version.parse('0.6.0'),
3333
});
3434
const newContent = version.updateContent(oldContent);
35-
snapshot(newContent.replace(/\r\n/g, '\n'));
35+
snapshot(newContent);
3636
});
3737

3838
it('updates version with build number in pubspec.yaml file', async () => {
3939
const oldContent = readFileSync(
4040
resolve(fixturesPath, './pubspec_with_build_no.yaml'),
4141
'utf8'
42-
).replace(/\r\n/g, '\n');
42+
).replace(/\r\n/g, '\n'); // required for windows
4343
const version = new PubspecYaml({
4444
version: Version.parse('0.6.0'),
4545
});
4646
const newContent = version.updateContent(oldContent);
47-
snapshot(newContent.replace(/\r\n/g, '\n'));
47+
snapshot(newContent);
48+
});
49+
50+
it('leaves malformatted build numbers alone in pubspec.yaml file', async () => {
51+
const oldContent = readFileSync(
52+
resolve(fixturesPath, './pubspec_with_build_no_bad.yaml'),
53+
'utf8'
54+
).replace(/\r\n/g, '\n'); // required for windows
55+
const version = new PubspecYaml({
56+
version: Version.parse('0.6.0'),
57+
});
58+
const newContent = version.updateContent(oldContent);
59+
snapshot(newContent);
4860
});
4961
});
5062
});

0 commit comments

Comments
 (0)