From b6e6bfceb248f183f5ce112fb8473e004a1527b9 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Tue, 17 Dec 2024 10:19:16 -0800 Subject: [PATCH] Update grinder script. (#1627) Update grinder script. The workflow for shipping dart_style used to be that we published a new version on pub and then rolled it into the Dart SDK. That meant that the dart_style version number in the SDK accurately described what was in dart_style. A few years ago, we changed to rolling into the SDK first. That lets us validate dart_style inside google3 before irrevocably publishing, which is a good thing. But it has an unfortunate side effect. If you run `dart format --version`, it shows the *previous* version number of dart_style because the version number only gets bumped when we publish. This is really confusing if you're trying to debug the formatter in the SDK and it appears to be an older version than it actually is. To fix that, I'm tweaking the workflow slightly. When we begin working on a new version of the formatter, we bump the version number in the pubspec to, say, 3.4.5-wip. At that point, we immediately update the version number constant shown by `--version` to "3.4.5" (the version it *will* be). That way, when it gets rolled into the SDK, it prints the upcoming version number. When a version of dart_style is about to be published, we remove `-wip` from the pubspec but the constant printed by `--version` is already done. To make this workflow a little less error-prone, I updated the grinder script to add tasks for starting a new patch, minor, and major version. I also updated the docs for the old task now that we use the workflow automation for publishing. Co-authored-by: Kevin Moore --- lib/src/cli/formatter_options.dart | 2 +- tool/grind.dart | 103 +++++++++++++++++------------ 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/lib/src/cli/formatter_options.dart b/lib/src/cli/formatter_options.dart index c6403156..2c71f02e 100644 --- a/lib/src/cli/formatter_options.dart +++ b/lib/src/cli/formatter_options.dart @@ -12,7 +12,7 @@ import 'show.dart'; import 'summary.dart'; // Note: The following line of code is modified by tool/grind.dart. -const dartStyleVersion = '3.0.0'; +const dartStyleVersion = '3.0.1-wip'; /// Global options that affect how the formatter produces and uses its outputs. final class FormatterOptions { diff --git a/tool/grind.dart b/tool/grind.dart index e4da5b8d..db4a3563 100644 --- a/tool/grind.dart +++ b/tool/grind.dart @@ -26,48 +26,57 @@ Future validate() async { Dart.run('bin/format.dart', arguments: ['.']); } +/// Increments the patch version of the current version. +@Task() +Future patch() async { + var version = _readVersion(); + _updateVersion( + Version(version.major, version.minor, version.patch + 1, pre: 'wip')); +} + +/// Increments the minor version of the current version. +@Task() +Future minor() async { + var version = _readVersion(); + _updateVersion(Version(version.major, version.minor + 1, 0, pre: 'wip')); +} + +/// Increments the major version of the current version. +@Task() +Future major() async { + var version = _readVersion(); + _updateVersion(Version(version.major + 1, 0, 0, pre: 'wip')); +} + /// Gets ready to publish a new version of the package. /// /// To publish a version, you need to: /// -/// 1. Make sure the version in the pubspec is a "-dev" number. This should -/// already be the case since you've already landed patches that change -/// the formatter and bumped to that as a consequence. -/// -/// 2. Run this task: -/// -/// pub run grinder bump -/// -/// 3. Commit the change to a branch. -/// -/// 4. Send it out for review: -/// -/// git cl upload +/// 1. Make sure the version in the pubspec is a "-wip" number. This should +/// already be the case since you've already landed patches that change +/// the formatter and bumped to that as a consequence. /// -/// 5. After the review is complete, land it: +/// 2. Run this task: /// -/// git cl land +/// ``` +/// dart run grinder bump +/// ``` /// -/// 6. Tag the commit: +/// 3. Commit the change to a branch, push it to GitHub, and review and merge +/// it there. /// -/// git tag -a "" -m "" -/// git push origin +/// 4. After the PR is merged using the publishing automation to publish the +/// new version: /// -/// 7. Publish the package: -/// -/// pub lish +/// https://github.com/dart-lang/ecosystem/wiki/Publishing-automation @Task() @Depends(validate) -Future bump() async { - // Read the version from the pubspec. - var pubspecFile = getFile('pubspec.yaml'); - var pubspec = pubspecFile.readAsStringSync(); - var version = - Version.parse((yaml.loadYaml(pubspec) as Map)['version'] as String); +Future ship() async { + var version = _readVersion(); - // Require a "-dev" version since we don't otherwise know what to bump it to. + // Require a "-wip" version since we don't otherwise know what to bump it to. if (!version.isPreRelease) { - throw StateError('Cannot publish non-dev version $version.'); + throw StateError('Cannot publish non-wip version $version.'); } // Don't allow versions like "1.2.3-dev+4" because it's not clear if the @@ -76,25 +85,37 @@ Future bump() async { throw StateError('Cannot publish build version $version.'); } - var bumped = Version(version.major, version.minor, version.patch); + // Remove the pre-release suffix. + _updateVersion(Version(version.major, version.minor, version.patch)); +} + +/// Reads the current package version from the pubspec. +Version _readVersion() { + var pubspecFile = getFile('pubspec.yaml'); + var pubspec = pubspecFile.readAsStringSync(); + return Version.parse((yaml.loadYaml(pubspec) as Map)['version'] as String); +} + +/// Sets version numbers in the dart_style repository with [version]. +void _updateVersion(Version version) { + // Read the version from the pubspec. + var pubspecFile = getFile('pubspec.yaml'); + var pubspec = pubspecFile.readAsStringSync(); // Update the version in the pubspec. - pubspec = pubspec.replaceAll(_versionPattern, 'version: $bumped'); + pubspec = pubspec.replaceAll(_versionPattern, 'version: $version'); pubspecFile.writeAsStringSync(pubspec); - // Update the version constant in formatter_options.dart. + // Update the version constant in formatter_options.dart (minus any + // pre-release prefix). We do this eagerly so that the version number shown + // by `dart format --version` is the version that *will* be published, even + // though we roll into the Dart SDK before publishing the final version. + var withoutPrerelease = Version(version.major, version.minor, version.patch); var versionFile = getFile('lib/src/cli/formatter_options.dart'); var versionSource = versionFile.readAsStringSync().replaceAll( RegExp(r"const dartStyleVersion = '[^']+';"), - "const dartStyleVersion = '$bumped';"); + "const dartStyleVersion = '$withoutPrerelease';"); versionFile.writeAsStringSync(versionSource); - // Update the version in the CHANGELOG. - var changelogFile = getFile('CHANGELOG.md'); - var changelog = changelogFile - .readAsStringSync() - .replaceAll(version.toString(), bumped.toString()); - changelogFile.writeAsStringSync(changelog); - - log("Updated version to '$bumped'."); + log("Updated version to '$version'."); }