Skip to content

Commit 20397ef

Browse files
authored
Add grinder tasks to bump package versions (#2405)
1 parent c907bcb commit 20397ef

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

tool/grind.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:grinder/grinder.dart';
1111
import 'package:path/path.dart' as p;
1212
import 'package:source_span/source_span.dart';
1313

14+
import 'grind/bump_version.dart';
1415
import 'grind/generate_deprecations.dart';
1516
import 'grind/synchronize.dart';
1617
import 'grind/utils.dart';
@@ -125,6 +126,8 @@ void main(List<String> args) {
125126

126127
pkg.addAllTasks();
127128

129+
addBumpVersionTasks();
130+
128131
afterTask("pkg-npm-dev", _addDefaultExport);
129132
afterTask("pkg-npm-release", _addDefaultExport);
130133

tool/grind/bump_version.dart

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2024 Google Inc. Use of this source code is governed by an
2+
// MIT-style license that can be found in the LICENSE file or at
3+
// https://opensource.org/licenses/MIT.
4+
5+
import 'dart:io';
6+
import 'dart:convert';
7+
8+
import 'package:grinder/grinder.dart';
9+
import 'package:path/path.dart' as p;
10+
import 'package:pub_semver/pub_semver.dart';
11+
import 'package:source_span/source_span.dart';
12+
import 'package:yaml/yaml.dart';
13+
14+
/// A regular expression that matches a version in a pubspec.
15+
final _pubspecVersionRegExp = RegExp(r'^version: (.*)$', multiLine: true);
16+
17+
/// Adds grinder tasks for bumping package versions.
18+
void addBumpVersionTasks() {
19+
for (var patch in [false, true]) {
20+
for (var dev in [true, false]) {
21+
addTask(GrinderTask(
22+
'bump-version-${patch ? 'patch' : 'minor'}' + (dev ? '-dev' : ''),
23+
taskFunction: () => _bumpVersion(patch, dev),
24+
description: 'Bump the version of all packages to the next '
25+
'${patch ? 'patch' : 'minor'}${dev ? ' dev' : ''} version'));
26+
}
27+
}
28+
}
29+
30+
/// Bumps the current package versions to the next [patch] version, with `-dev`
31+
/// if [dev] is true.
32+
void _bumpVersion(bool patch, bool dev) {
33+
// Returns the version to which to bump [version].
34+
Version chooseNextVersion(Version version, SourceSpan span) {
35+
if (dev) {
36+
if (patch
37+
? version.preRelease.isNotEmpty
38+
: version.patch == 0 ||
39+
version.preRelease.length != 1 ||
40+
version.preRelease.first != "dev") {
41+
fail(span.message("Version is already pre-release", color: true));
42+
}
43+
} else if (version.preRelease.length == 1 &&
44+
version.preRelease.first == "dev" &&
45+
(patch || version.patch == 0)) {
46+
// If it's already a dev version, just mark it stable instead of
47+
// increasing it.
48+
return Version(version.major, version.minor, version.patch);
49+
}
50+
51+
var nextVersion =
52+
patch || version.major == 0 ? version.nextPatch : version.nextMinor;
53+
return Version(nextVersion.major, nextVersion.minor, nextVersion.patch,
54+
pre: dev ? "dev" : null);
55+
}
56+
57+
/// Adds a "No user-visible changes" entry for [version] to the changelog in
58+
/// [dir].
59+
void addChangelogEntry(String dir, Version version) {
60+
var path = p.join(dir, "CHANGELOG.md");
61+
var text = File(path).readAsStringSync();
62+
if (!dev && text.startsWith("## ${version}-dev\n")) {
63+
File(path).writeAsStringSync(
64+
text.replaceFirst("## ${version}-dev\n", "## ${version}\n"));
65+
} else if (text.startsWith("## ${version}\n")) {
66+
return;
67+
} else {
68+
File(path).writeAsStringSync(
69+
"## ${version}\n\n* No user-visible changes.\n\n$text");
70+
}
71+
}
72+
73+
// Bumps the current version of [pubspec] to the next [patch] version, with
74+
// `-dev` if [dev] is true.
75+
void bumpDartVersion(String path) {
76+
var text = File(path).readAsStringSync();
77+
var pubspec = loadYaml(text, sourceUrl: p.toUri(path)) as YamlMap;
78+
var version = chooseNextVersion(Version.parse(pubspec["version"] as String),
79+
pubspec.nodes["version"]!.span);
80+
File(path).writeAsStringSync(
81+
text.replaceFirst(_pubspecVersionRegExp, 'version: $version'));
82+
addChangelogEntry(p.dirname(path), version);
83+
}
84+
85+
bumpDartVersion('pubspec.yaml');
86+
bumpDartVersion('pkg/sass_api/pubspec.yaml');
87+
88+
var packageJsonPath = 'pkg/sass-parser/package.json';
89+
var packageJsonText = File(packageJsonPath).readAsStringSync();
90+
var packageJson =
91+
loadYaml(packageJsonText, sourceUrl: p.toUri(packageJsonPath)) as YamlMap;
92+
var version = chooseNextVersion(
93+
Version.parse(packageJson["version"] as String),
94+
packageJson.nodes["version"]!.span);
95+
File(packageJsonPath).writeAsStringSync(JsonEncoder.withIndent(" ")
96+
.convert({...packageJson, "version": version.toString()}) +
97+
"\n");
98+
addChangelogEntry("pkg/sass-parser", version);
99+
}

0 commit comments

Comments
 (0)