Skip to content

Commit 78abf72

Browse files
authored
fix(repo_manage): use pub_semver for changelog -wip transitions (#475)
1 parent 0bfa654 commit 78abf72

4 files changed

Lines changed: 105 additions & 25 deletions

File tree

‎pkgs/repo_manage/lib/changelog_updater.dart‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,31 @@ import 'dart:convert';
66
import 'dart:io';
77

88
import 'package:path/path.dart' as path;
9+
import 'package:pub_semver/pub_semver.dart';
910

1011
import 'src/common.dart';
1112

12-
String _incrementPatch(String version) {
13-
final parts = version.split('.');
14-
if (parts.length >= 3) {
15-
final patchStr = parts[2].split('-').first.split('+').first;
16-
if (int.tryParse(patchStr) case final patchNum?) {
17-
final newPatchStr = (patchNum + 1).toString();
18-
parts[2] = parts[2].replaceFirst(patchStr, newPatchStr);
19-
return parts.join('.');
20-
}
13+
extension on Version {
14+
bool get isWip =>
15+
isPreRelease &&
16+
switch (preRelease.last) {
17+
final String s => s == 'wip' || s.endsWith('-wip'),
18+
_ => false,
19+
};
20+
}
21+
22+
Version _nextWipVersion(Version? current) {
23+
if (current == null) return Version(0, 0, 1, pre: 'wip');
24+
if (current.isPreRelease) {
25+
return Version(
26+
current.major,
27+
current.minor,
28+
current.patch,
29+
pre: '${current.preRelease.join('.')}-wip',
30+
);
2131
}
22-
return version;
32+
final next = current.nextPatch;
33+
return Version(next.major, next.minor, next.patch, pre: 'wip');
2334
}
2435

2536
void updatePubspecVersion(File pubspecFile, String newVersion) {
@@ -45,26 +56,25 @@ void updateChangelog({
4556
}) {
4657
final changelogContent = changelogFile.readAsStringSync();
4758
final lines = LineSplitter.split(changelogContent).toList();
48-
var currentVersion = '0.0.1';
59+
Version? currentVersion;
4960
var currentVersionLine = 0;
5061

5162
for (var i = 0; i < lines.length; i++) {
5263
final line = lines[i].trim();
5364
if (line.startsWith('## ')) {
54-
final content = line.substring(3).trim();
55-
final version = content.split(' ').first;
56-
if (version.startsWith(RegExp(r'\d'))) {
57-
currentVersion = version;
65+
final token = line.substring(3).trim().split(' ').first;
66+
try {
67+
currentVersion = Version.parse(token);
5868
currentVersionLine = i;
5969
break;
70+
} on FormatException {
71+
// Ignore non-SemVer level-2 headings.
6072
}
6173
}
6274
}
6375

64-
final isWip = currentVersion.endsWith('-wip');
65-
6676
final output = <String>[];
67-
if (isWip) {
77+
if (currentVersion != null && currentVersion.isWip) {
6878
var sectionEnd = lines.indexWhere(
6979
(line) => line.startsWith('## '), currentVersionLine + 1) -
7080
1;
@@ -76,25 +86,23 @@ void updateChangelog({
7686
output.add('- $message');
7787
output.addAll(lines.skip(sectionEnd));
7888
} else {
89+
final newVersion = _nextWipVersion(currentVersion).toString();
7990
output.addAll([
80-
'## ${_incrementPatch(currentVersion)}-wip',
91+
'## $newVersion',
8192
'',
8293
'- $message',
8394
'',
8495
...lines,
8596
]);
86-
}
87-
88-
changelogFile.writeAsStringSync('${output.join('\n')}\n');
8997

90-
if (!isWip) {
91-
final newVersion = '${_incrementPatch(currentVersion)}-wip';
9298
final changelogDir = path.dirname(changelogFile.path);
9399
final pubspecFile = File(path.join(changelogDir, 'pubspec.yaml'));
94100
if (pubspecFile.existsSync()) {
95101
updatePubspecVersion(pubspecFile, newVersion);
96102
}
97103
}
104+
105+
changelogFile.writeAsStringSync('${output.join('\n')}\n');
98106
}
99107

100108
class ChangelogUpdaterCommand extends ReportCommand {

‎pkgs/repo_manage/pubspec.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies:
1616
github: ^9.11.0
1717
graphql: ^5.0.0
1818
path: ^1.8.0
19+
pub_semver: ^2.1.0
1920

2021
dev_dependencies:
2122
dart_flutter_team_lints: ^3.0.0

‎pkgs/repo_manage/test/changelog_updater_test.dart‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,77 @@ version: 1.0.1-wip
149149
expect(pubspecFile.readAsStringSync(), '''
150150
name: my_package
151151
version: 1.0.1-wip
152+
''');
153+
});
154+
155+
test('strips build metadata when incrementing patch to WIP', () {
156+
final tempDir = Directory.systemTemp.createTempSync();
157+
addTearDown(() => tempDir.deleteSync(recursive: true));
158+
final changelogFile = File('${tempDir.path}/CHANGELOG.md');
159+
final pubspecFile = File('${tempDir.path}/pubspec.yaml');
160+
161+
changelogFile.writeAsStringSync('''
162+
## 1.2.3+build.4
163+
164+
- Released version with build metadata
165+
''');
166+
pubspecFile.writeAsStringSync('''
167+
name: my_package
168+
version: 1.2.3+build.4
169+
''');
170+
171+
updateChangelog(changelogFile: changelogFile, message: 'Next change');
172+
173+
expect(changelogFile.readAsStringSync(), '''
174+
## 1.2.4-wip
175+
176+
- Next change
177+
178+
## 1.2.3+build.4
179+
180+
- Released version with build metadata
181+
''');
182+
183+
expect(pubspecFile.readAsStringSync(), '''
184+
name: my_package
185+
version: 1.2.4-wip
186+
''');
187+
});
188+
189+
test('appends -wip to a published pre-release and coalesces follow-ups',
190+
() {
191+
final tempDir = Directory.systemTemp.createTempSync();
192+
addTearDown(() => tempDir.deleteSync(recursive: true));
193+
final changelogFile = File('${tempDir.path}/CHANGELOG.md');
194+
final pubspecFile = File('${tempDir.path}/pubspec.yaml');
195+
196+
changelogFile.writeAsStringSync('''
197+
## 1.2.3-alpha.3
198+
199+
- Published alpha
200+
''');
201+
pubspecFile.writeAsStringSync('''
202+
name: my_package
203+
version: 1.2.3-alpha.3
204+
''');
205+
206+
updateChangelog(changelogFile: changelogFile, message: 'First fix');
207+
updateChangelog(changelogFile: changelogFile, message: 'Second fix');
208+
209+
expect(changelogFile.readAsStringSync(), '''
210+
## 1.2.3-alpha.3-wip
211+
212+
- First fix
213+
- Second fix
214+
215+
## 1.2.3-alpha.3
216+
217+
- Published alpha
218+
''');
219+
220+
expect(pubspecFile.readAsStringSync(), '''
221+
name: my_package
222+
version: 1.2.3-alpha.3-wip
152223
''');
153224
});
154225
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 0.0.2-wip
1+
## 0.0.1-wip
22

33
- Initial entry
44

0 commit comments

Comments
 (0)