Skip to content

Commit 1519e2f

Browse files
[tool] Support pre-1.0 versions in batch releases (flutter#12257)
Adds support for Dart's semver convention of shifting the conceptual meaning of versions by one place for pre-1.0 package when determining batch release versions. For example, a batch release with a "major" changelog entry would go from 0.x.y to 0.x+1.0.
1 parent 29736f0 commit 1519e2f

3 files changed

Lines changed: 117 additions & 55 deletions

File tree

‎script/tool/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
* Adds support for batch release of pre-1.0 packages.
4+
15
## 0.14.4
26

37
* Adds `--run-on-staged-packages` argument to support running commands only

‎script/tool/lib/src/branches_for_batch_release_command.dart‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ class BranchesForBatchReleaseCommand extends PackageCommand {
7878
}
7979

8080
final pubspec = Pubspec.parse(package.pubspecFile.readAsStringSync());
81-
if (pubspec.version == null || pubspec.version!.major < 1) {
82-
printError(
83-
'This script only supports packages with version >= 1.0.0. Current version: ${pubspec.version}.',
84-
);
81+
if (pubspec.version == null) {
82+
printError('The package has no version specified.');
8583
throw ToolExit(_kExitPackageMalformed);
8684
}
8785
final _ReleaseInfo releaseInfo = _getReleaseInfo(pendingChangelogs, pubspec.version!);
@@ -131,15 +129,34 @@ class BranchesForBatchReleaseCommand extends PackageCommand {
131129
}
132130
final VersionChange effectiveVersionChange = VersionChange.values[versionIndex];
133131

134-
final Version? newVersion = switch (effectiveVersionChange) {
135-
VersionChange.skip => null,
136-
VersionChange.major => Version(oldVersion.major + 1, 0, 0),
137-
VersionChange.minor => Version(oldVersion.major, oldVersion.minor + 1, 0),
138-
VersionChange.patch => Version(oldVersion.major, oldVersion.minor, oldVersion.patch + 1),
139-
};
132+
final Version? newVersion = _newVersionFollowingDartSemVer(oldVersion, effectiveVersionChange);
140133
return _ReleaseInfo(newVersion, changelogs);
141134
}
142135

136+
Version? _newVersionFollowingDartSemVer(Version oldVersion, VersionChange change) {
137+
if (oldVersion.major == 0) {
138+
final oldBuildNumber = oldVersion.build.isEmpty ? 0 : oldVersion.build.first as int;
139+
return switch (change) {
140+
VersionChange.skip => null,
141+
VersionChange.major => Version(0, oldVersion.minor + 1, 0),
142+
VersionChange.minor => Version(0, oldVersion.minor, oldVersion.patch + 1),
143+
VersionChange.patch => Version(
144+
0,
145+
oldVersion.minor,
146+
oldVersion.patch,
147+
build: '${oldBuildNumber + 1}',
148+
),
149+
};
150+
} else {
151+
return switch (change) {
152+
VersionChange.skip => null,
153+
VersionChange.major => Version(oldVersion.major + 1, 0, 0),
154+
VersionChange.minor => Version(oldVersion.major, oldVersion.minor + 1, 0),
155+
VersionChange.patch => Version(oldVersion.major, oldVersion.minor, oldVersion.patch + 1),
156+
};
157+
}
158+
}
159+
143160
/// Creates a branch with a commit contains the changes for the release.
144161
///
145162
/// This method will create a new branch, update the pubspec.yaml, update the changelog,

‎script/tool/test/branches_for_batch_release_command_test.dart‎

Lines changed: 86 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ void main() {
3535
'--remote=origin',
3636
], errorHandler: errorHandler);
3737

38-
RepositoryPackage createTestPackage() {
39-
final RepositoryPackage package = createFakePackage('a_package', packagesDir);
38+
RepositoryPackage createTestPackage({String version = '1.0.0'}) {
39+
final RepositoryPackage package = createFakePackage('a_package', packagesDir, version: version);
4040

4141
package.changelogFile.writeAsStringSync('''
42-
## 1.0.0
42+
## $version
4343
4444
- Old changes
45-
''');
46-
package.pubspecFile.writeAsStringSync('''
47-
name: a_package
48-
version: 1.0.0
4945
''');
5046
package.directory.childDirectory('pending_changelogs').createSync();
5147
return package;
@@ -184,6 +180,89 @@ version: patch
184180
expect(changelogContent, contains('A new feature'));
185181
expect(gitProcessRunner.recordedCalls, orderedEquals(getExpectedGitCalls('1.0.1')));
186182
});
183+
test('bumps "patch" version for minor pre-1.0', () async {
184+
final RepositoryPackage package = createTestPackage(version: '0.1.0');
185+
addTearDown(() {
186+
package.directory.deleteSync(recursive: true);
187+
});
188+
createPendingChangelogFile(package, 'a.yaml', '''
189+
changelog: A new feature
190+
version: minor
191+
''');
192+
193+
final List<String> output = await runBatchCommand();
194+
195+
expect(
196+
output,
197+
containsAllInOrder(<String>[
198+
'Parsing package "a_package"...',
199+
' Creating new branch "release-branch"...',
200+
' Pushing branch release-branch to remote origin...',
201+
]),
202+
);
203+
204+
expect(package.pubspecFile.readAsStringSync(), contains('version: 0.1.1'));
205+
final String changelogContent = package.changelogFile.readAsStringSync();
206+
expect(changelogContent, startsWith('## 0.1.1'));
207+
expect(changelogContent, contains('A new feature'));
208+
expect(gitProcessRunner.recordedCalls, orderedEquals(getExpectedGitCalls('0.1.1')));
209+
});
210+
211+
test('bumps "minor" version for major in pre-1.0', () async {
212+
final RepositoryPackage package = createTestPackage(version: '0.1.0');
213+
addTearDown(() {
214+
package.directory.deleteSync(recursive: true);
215+
});
216+
createPendingChangelogFile(package, 'a.yaml', '''
217+
changelog: A new feature
218+
version: major
219+
''');
220+
221+
final List<String> output = await runBatchCommand();
222+
223+
expect(
224+
output,
225+
containsAllInOrder(<String>[
226+
'Parsing package "a_package"...',
227+
' Creating new branch "release-branch"...',
228+
' Pushing branch release-branch to remote origin...',
229+
]),
230+
);
231+
232+
expect(package.pubspecFile.readAsStringSync(), contains('version: 0.2.0'));
233+
final String changelogContent = package.changelogFile.readAsStringSync();
234+
expect(changelogContent, startsWith('## 0.2.0'));
235+
expect(changelogContent, contains('A new feature'));
236+
expect(gitProcessRunner.recordedCalls, orderedEquals(getExpectedGitCalls('0.2.0')));
237+
});
238+
239+
test('bumps build number for patch in pre-1.0', () async {
240+
final RepositoryPackage package = createTestPackage(version: '0.1.0');
241+
addTearDown(() {
242+
package.directory.deleteSync(recursive: true);
243+
});
244+
createPendingChangelogFile(package, 'a.yaml', '''
245+
changelog: A new feature
246+
version: patch
247+
''');
248+
249+
final List<String> output = await runBatchCommand();
250+
251+
expect(
252+
output,
253+
containsAllInOrder(<String>[
254+
'Parsing package "a_package"...',
255+
' Creating new branch "release-branch"...',
256+
' Pushing branch release-branch to remote origin...',
257+
]),
258+
);
259+
260+
expect(package.pubspecFile.readAsStringSync(), contains('version: 0.1.0+1'));
261+
final String changelogContent = package.changelogFile.readAsStringSync();
262+
expect(changelogContent, startsWith('## 0.1.0+1'));
263+
expect(changelogContent, contains('A new feature'));
264+
expect(gitProcessRunner.recordedCalls, orderedEquals(getExpectedGitCalls('0.1.0+1')));
265+
});
187266

188267
test('merges multiple changelogs, minor and major', () async {
189268
final RepositoryPackage package = createTestPackage();
@@ -599,43 +678,5 @@ version: major
599678

600679
expect(output.last, contains('Failed to push to release-a_package-2.0.0: error'));
601680
});
602-
603-
test('throws for pre-1.0.0 packages', () async {
604-
final RepositoryPackage package = createFakePackage('a_package', packagesDir);
605-
606-
addTearDown(() {
607-
package.directory.deleteSync(recursive: true);
608-
});
609-
610-
// Set a pre-1.0.0 version.
611-
package.changelogFile.writeAsStringSync('''
612-
## 0.5.0
613-
614-
- Old changes
615-
''');
616-
package.pubspecFile.writeAsStringSync('''
617-
name: a_package
618-
version: 0.5.0
619-
''');
620-
package.directory.childDirectory('pending_changelogs').createSync();
621-
createPendingChangelogFile(package, 'a.yaml', '''
622-
changelog: A new feature
623-
version: minor
624-
''');
625-
626-
final List<String> output = await runBatchCommand(
627-
errorHandler: (Error e) {
628-
expect(e, isA<ToolExit>());
629-
expect((e as ToolExit).exitCode, 3);
630-
},
631-
);
632-
633-
expect(
634-
output.last,
635-
contains(
636-
'This script only supports packages with version >= 1.0.0. Current version: 0.5.0.',
637-
),
638-
);
639-
});
640681
});
641682
}

0 commit comments

Comments
 (0)