@@ -6,20 +6,31 @@ import 'dart:convert';
66import 'dart:io' ;
77
88import 'package:path/path.dart' as path;
9+ import 'package:pub_semver/pub_semver.dart' ;
910
1011import '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
2536void 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
100108class ChangelogUpdaterCommand extends ReportCommand {
0 commit comments