@@ -32,12 +32,14 @@ class ReleaseCommand extends Command {
3232 _runTests ();
3333 _checkSemver ();
3434 if (! isPrerelease) {
35- _removePrereleaseFlagFromPubspec ();
35+ final oldVersion = _removePrereleaseFlagFromPubspec ();
36+ _updateChangelog (oldVersion);
3637 _commit ('Version ${_getCurrentVersion ()}' );
3738 }
3839 await _publishDryRunAsync ();
3940 _createTag ('v${_getCurrentVersion ()}' );
4041 _setNextPrereleaseVersion ();
42+ _addNextVersionToChangelog ();
4143 _commit ('Version ${_getCurrentVersion ()}' );
4244 }
4345
@@ -182,19 +184,19 @@ class ReleaseCommand extends Command {
182184 }
183185 }
184186
185- void _removePrereleaseFlagFromPubspec () {
187+ String _removePrereleaseFlagFromPubspec () {
186188 final String pubspecPath = path.join (_getApiToolRootPath (), 'pubspec.yaml' );
187189 final pubspecContent = File (pubspecPath).readAsStringSync ();
188190
189191 final pubspec = Pubspec .parse (pubspecContent);
190192 final currentVersion = pubspec.version! ;
191193
194+ final currentVersionString = currentVersion.canonicalizedVersion;
195+
192196 if (currentVersion.preRelease.isEmpty) {
193- return ;
197+ return currentVersionString ;
194198 }
195199
196- final currentVersionString = currentVersion.canonicalizedVersion;
197-
198200 final newVersion = Version (
199201 currentVersion.major, currentVersion.minor, currentVersion.patch);
200202 final newVersionString = newVersion.canonicalizedVersion;
@@ -205,6 +207,7 @@ class ReleaseCommand extends Command {
205207 );
206208
207209 File (pubspecPath).writeAsStringSync (newPubspecContent);
210+ return currentVersionString;
208211 }
209212
210213 void _commit (String message) {
@@ -280,4 +283,31 @@ class ReleaseCommand extends Command {
280283 );
281284 File (pubspecPath).writeAsStringSync (newPubspecContent);
282285 }
286+
287+ void _addNextVersionToChangelog () {
288+ print ('adding next version to changelog' );
289+ final changelogPath = path.join (_getApiToolRootPath (), 'CHANGELOG.md' );
290+ final changelogContent = File (changelogPath).readAsStringSync ();
291+ final newVersion = _getCurrentVersion ();
292+ final newChangelogContent = changelogContent.replaceFirst (
293+ '# Changelog\n ' ,
294+ '# Changelog\n\n ## Version $newVersion \n ' ,
295+ );
296+ File (changelogPath).writeAsStringSync (newChangelogContent);
297+ }
298+
299+ void _updateChangelog (String oldVersion) {
300+ print ('updating changelog' );
301+ final changelogPath = path.join (_getApiToolRootPath (), 'CHANGELOG.md' );
302+ final changelogContent = File (changelogPath).readAsStringSync ();
303+ final newVersion = _getCurrentVersion ();
304+ final newChangelogContent = changelogContent.replaceFirst (
305+ '## Version $oldVersion ' ,
306+ '## Version $newVersion ' ,
307+ );
308+ if (newChangelogContent == changelogContent) {
309+ throw Exception ('Could not find version $oldVersion in CHANGELOG.md' );
310+ }
311+ File (changelogPath).writeAsStringSync (newChangelogContent);
312+ }
283313}
0 commit comments