-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pub bump
command
#4361
Merged
Merged
pub bump
command
#4361
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:pub_semver/pub_semver.dart'; | ||
import 'package:yaml/yaml.dart'; | ||
import 'package:yaml_edit/yaml_edit.dart'; | ||
|
||
import '../command.dart'; | ||
import '../io.dart'; | ||
import '../log.dart' as log; | ||
|
||
class BumpSubcommand extends PubCommand { | ||
@override | ||
final String name; | ||
@override | ||
final String description; | ||
|
||
final Version Function(Version) updateVersion; | ||
BumpSubcommand(this.name, this.description, this.updateVersion) { | ||
argParser.addFlag( | ||
'dry-run', | ||
abbr: 'n', | ||
negatable: false, | ||
help: "Report what would change, but don't change anything.", | ||
); | ||
} | ||
|
||
String? _versionLines(YamlMap map, String text, String prefix) { | ||
final entry = map.nodes.entries | ||
.firstWhereOrNull((e) => (e.key as YamlNode).value == 'version'); | ||
if (entry == null) return null; | ||
|
||
final firstLine = (entry.key as YamlNode).span.start.line; | ||
final lastLine = entry.value.span.end.line; | ||
final lines = text.split('\n'); | ||
return lines | ||
.sublist(firstLine, lastLine + 1) | ||
.map((x) => '$prefix$x') | ||
.join('\n'); | ||
} | ||
|
||
@override | ||
Future<void> runProtected() async { | ||
final pubspec = entrypoint.workPackage.pubspec; | ||
final currentVersion = pubspec.version; | ||
|
||
final newVersion = updateVersion(currentVersion); | ||
|
||
final originalPubspecText = | ||
readTextFile(entrypoint.workPackage.pubspecPath); | ||
final yamlEditor = YamlEditor(originalPubspecText); | ||
yamlEditor.update(['version'], newVersion.toString()); | ||
final updatedPubspecText = yamlEditor.toString(); | ||
final beforeText = _versionLines(pubspec.fields, originalPubspecText, '- '); | ||
final afterText = _versionLines( | ||
yamlEditor.parseAt([]) as YamlMap, | ||
updatedPubspecText, | ||
'+ ', | ||
); | ||
if (argResults.flag('dry-run')) { | ||
log.message('Would update version from $currentVersion to $newVersion.'); | ||
log.message('Diff:'); | ||
if (beforeText != null) { | ||
log.message(beforeText); | ||
} | ||
if (afterText != null) { | ||
log.message(afterText); | ||
} | ||
} else { | ||
log.message('Updating version from $currentVersion to $newVersion.'); | ||
log.message('Diff:'); | ||
|
||
if (beforeText != null) { | ||
log.message(beforeText); | ||
} | ||
if (afterText != null) { | ||
log.message(afterText); | ||
log.message('\nRemember to update `CHANGELOG.md` before publishing.'); | ||
} | ||
writeTextFile( | ||
entrypoint.workPackage.pubspecPath, | ||
yamlEditor.toString(), | ||
); | ||
} | ||
} | ||
} | ||
|
||
class BumpCommand extends PubCommand { | ||
@override | ||
String get name => 'bump'; | ||
@override | ||
String get description => ''' | ||
Increases the version number of the current package. | ||
'''; | ||
|
||
BumpCommand() { | ||
addSubcommand( | ||
BumpSubcommand( | ||
'major', | ||
'Increment the major version number (eg. 3.1.2 -> 4.0.0)', | ||
(v) => v.nextMajor, | ||
), | ||
); | ||
addSubcommand( | ||
BumpSubcommand( | ||
'minor', | ||
'Increment the minor version number (eg. 3.1.2 -> 3.2.0)', | ||
(v) => v.nextMinor, | ||
), | ||
); | ||
addSubcommand( | ||
BumpSubcommand( | ||
'patch', | ||
'Increment the patch version number (eg. 3.1.2 -> 3.1.3)', | ||
(v) => v.nextPatch, | ||
), | ||
); | ||
|
||
addSubcommand( | ||
BumpSubcommand( | ||
'breaking', | ||
'Increment to the next breaking version (eg. 0.1.2 -> 0.2.0)', | ||
(v) => v.nextBreaking, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test/test.dart'; | ||
|
||
import 'descriptor.dart'; | ||
import 'test_pub.dart'; | ||
|
||
void main() { | ||
void testBump(String part, String from, String to) { | ||
test('Bumps the $part version from $from to $to', () async { | ||
await dir(appPath, [ | ||
file( | ||
'pubspec.yaml', | ||
''' | ||
name: myapp | ||
version: $from # comment | ||
environment: | ||
sdk: $defaultSdkConstraint | ||
''', | ||
), | ||
]).create(); | ||
await runPub( | ||
args: ['bump', part, '--dry-run'], | ||
output: ''' | ||
Would update version from $from to $to. | ||
Diff: | ||
- version: $from # comment | ||
+ version: $to # comment | ||
''', | ||
); | ||
await runPub( | ||
args: ['bump', part], | ||
output: ''' | ||
Updating version from $from to $to. | ||
Diff: | ||
- version: $from # comment | ||
+ version: $to # comment | ||
|
||
Remember to update `CHANGELOG.md` before publishing. | ||
''', | ||
); | ||
await appDir(pubspec: {'version': to}).validate(); | ||
}); | ||
} | ||
|
||
testBump('major', '0.0.0', '1.0.0'); | ||
testBump('major', '1.2.3', '2.0.0'); | ||
testBump('minor', '0.1.1-dev+2', '0.2.0'); | ||
testBump('minor', '1.2.3', '1.3.0'); | ||
testBump('patch', '0.1.1-dev+2', '0.1.1'); | ||
testBump('patch', '0.1.1+2', '0.1.2'); | ||
testBump('patch', '1.2.3', '1.2.4'); | ||
testBump('breaking', '0.2.0', '0.3.0'); | ||
testBump('breaking', '1.2.3', '2.0.0'); | ||
|
||
test('Creates top-level version field if missing', () async { | ||
await dir(appPath, [ | ||
file('pubspec.yaml', ''' | ||
name: my_app | ||
'''), | ||
]).create(); | ||
await runPub( | ||
args: ['bump', 'breaking'], | ||
output: contains('Updating version from 0.0.0 to 0.1.0'), | ||
); | ||
await dir(appPath, [ | ||
file('pubspec.yaml', ''' | ||
name: my_app | ||
version: 0.1.0 | ||
'''), | ||
]).validate(); | ||
}); | ||
|
||
test('Writes all lines of diff', () async { | ||
await dir(appPath, [ | ||
file('pubspec.yaml', ''' | ||
name: my_app | ||
version: >- | ||
1.0.0 | ||
'''), | ||
]).create(); | ||
await runPub( | ||
args: ['bump', 'minor'], | ||
output: allOf( | ||
contains('Updating version from 1.0.0 to 1.1.0'), | ||
contains(''' | ||
Diff: | ||
- version: >- | ||
- 1.0.0 | ||
+ version: 1.1.0 | ||
'''), | ||
), | ||
); | ||
await dir(appPath, [ | ||
file('pubspec.yaml', ''' | ||
name: my_app | ||
version: 1.1.0 | ||
'''), | ||
]).validate(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# GENERATED BY: test/help_test.dart | ||
|
||
## Section 0 | ||
$ pub bump --help | ||
Increases the version number of the current package. | ||
|
||
|
||
Usage: pub bump [arguments...] | ||
-h, --help Print this usage information. | ||
|
||
Available subcommands: | ||
breaking Increment to the next breaking version (eg. 0.1.2 -> 0.2.0) | ||
major Increment the major version number (eg. 3.1.2 -> 4.0.0) | ||
minor Increment the minor version number (eg. 3.1.2 -> 3.2.0) | ||
patch Increment the patch version number (eg. 3.1.2 -> 3.1.3) | ||
|
||
Run "pub help" to see global options. | ||
|
12 changes: 12 additions & 0 deletions
12
test/testdata/goldens/help_test/pub bump breaking --help.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# GENERATED BY: test/help_test.dart | ||
|
||
## Section 0 | ||
$ pub bump breaking --help | ||
Increment to the next breaking version (eg. 0.1.2 -> 0.2.0) | ||
|
||
Usage: pub bump breaking <subcommand> [arguments...] | ||
-h, --help Print this usage information. | ||
-n, --dry-run Report what would change, but don't change anything. | ||
|
||
Run "pub help" to see global options. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# GENERATED BY: test/help_test.dart | ||
|
||
## Section 0 | ||
$ pub bump major --help | ||
Increment the major version number (eg. 3.1.2 -> 4.0.0) | ||
|
||
Usage: pub bump major <subcommand> [arguments...] | ||
-h, --help Print this usage information. | ||
-n, --dry-run Report what would change, but don't change anything. | ||
|
||
Run "pub help" to see global options. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# GENERATED BY: test/help_test.dart | ||
|
||
## Section 0 | ||
$ pub bump minor --help | ||
Increment the minor version number (eg. 3.1.2 -> 3.2.0) | ||
|
||
Usage: pub bump minor <subcommand> [arguments...] | ||
-h, --help Print this usage information. | ||
-n, --dry-run Report what would change, but don't change anything. | ||
|
||
Run "pub help" to see global options. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# GENERATED BY: test/help_test.dart | ||
|
||
## Section 0 | ||
$ pub bump patch --help | ||
Increment the patch version number (eg. 3.1.2 -> 3.1.3) | ||
|
||
Usage: pub bump patch <subcommand> [arguments...] | ||
-h, --help Print this usage information. | ||
-n, --dry-run Report what would change, but don't change anything. | ||
|
||
Run "pub help" to see global options. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Show me the diff! :D
Can we do something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done