Skip to content

Commit

Permalink
Update grinder script. (#1627)
Browse files Browse the repository at this point in the history
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 <kevmoo@users.noreply.github.com>
  • Loading branch information
munificent and kevmoo authored Dec 17, 2024
1 parent 67ba681 commit b6e6bfc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/src/cli/formatter_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
103 changes: 62 additions & 41 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,57 @@ Future<void> validate() async {
Dart.run('bin/format.dart', arguments: ['.']);
}

/// Increments the patch version of the current version.
@Task()
Future<void> 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<void> 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<void> 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 "<version>" -m "<version>"
/// git push origin <version>
/// 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<void> 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<void> 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
Expand All @@ -76,25 +85,37 @@ Future<void> 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'.");
}

0 comments on commit b6e6bfc

Please sign in to comment.