-
Notifications
You must be signed in to change notification settings - Fork 232
pub bump
command
#4361
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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:pub_semver/pub_semver.dart'; | ||
import 'package:yaml_edit/yaml_edit.dart'; | ||
|
||
import '../command.dart'; | ||
import '../io.dart'; | ||
import '../log.dart' as log; | ||
|
||
class BumpCommand extends PubCommand { | ||
@override | ||
String get name => 'bump'; | ||
@override | ||
String get description => ''' | ||
Increases the version number of the current package. | ||
'''; | ||
|
||
BumpCommand() { | ||
argParser.addFlag( | ||
'major', | ||
negatable: false, | ||
help: 'Increment the major version number (eg. 3.1.2 -> 4.0.0)', | ||
); | ||
argParser.addFlag( | ||
'minor', | ||
negatable: false, | ||
help: 'Increment the minor version number (eg. 3.1.2 -> 3.2.0)', | ||
); | ||
argParser.addFlag( | ||
'patch', | ||
negatable: false, | ||
help: 'Increment the patch version number (eg. 3.1.2 -> 3.1.3)', | ||
); | ||
argParser.addFlag( | ||
'breaking', | ||
negatable: false, | ||
help: 'Increment to the next breaking version (eg. 0.1.2 -> 0.2.0)', | ||
); | ||
argParser.addFlag( | ||
'dry-run', | ||
abbr: 'n', | ||
negatable: false, | ||
help: "Report what would change but don't change anything.", | ||
); | ||
} | ||
|
||
@override | ||
Future<void> runProtected() async { | ||
final currentVersion = entrypoint.workPackage.pubspec.version; | ||
final Version newVersion; | ||
|
||
final breaking = argResults.flag('breaking'); | ||
final major = argResults.flag('major'); | ||
final minor = argResults.flag('minor'); | ||
final patch = argResults.flag('patch'); | ||
final optionCount = | ||
[breaking, major, minor, patch].fold(0, (p, v) => p + (v ? 1 : 0)); | ||
if (optionCount != 1) { | ||
usageException('Provide exactly one of the options ' | ||
'`--breaking`, `--major`, `--minor` or `--patch`.'); | ||
} | ||
|
||
if (breaking) { | ||
newVersion = currentVersion.nextBreaking; | ||
} else if (major) { | ||
newVersion = currentVersion.nextMajor; | ||
} else if (minor) { | ||
newVersion = currentVersion.nextMinor; | ||
} else if (patch) { | ||
newVersion = currentVersion.nextPatch; | ||
} else { | ||
throw StateError('Should not be possible'); | ||
} | ||
|
||
if (argResults.flag('dry-run')) { | ||
log.message('Would update version from $currentVersion to $newVersion.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} else { | ||
log.message('Updating version from $currentVersion to $newVersion.'); | ||
final yamlEditor = | ||
YamlEditor(readTextFile(entrypoint.workPackage.pubspecPath)); | ||
|
||
yamlEditor.update(['version'], newVersion.toString()); | ||
writeTextFile(entrypoint.workPackage.pubspecPath, yamlEditor.toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Display a message that says: "Do update CHANGELOG.md before publishing!" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 appDir(pubspec: {'version': from}).create(); | ||
await runPub( | ||
args: ['bump', part, '--dry-run'], | ||
output: contains('Would update version from $from to $to.'), | ||
); | ||
await runPub( | ||
args: ['bump', part], | ||
output: contains('Updating version from $from to $to.'), | ||
); | ||
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 | ||
'''), | ||
]).create(); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# GENERATED BY: test/help_test.dart | ||
|
||
## Section 0 | ||
$ pub bump --help | ||
Increases the version number of the current package. | ||
|
||
|
||
Usage: pub bump <subcommand> [arguments...] | ||
-h, --help Print this usage information. | ||
--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) | ||
--breaking Increment to the next breaking version (eg. 0.1.2 -> 0.2.0) | ||
jonasfj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-n, --dry-run Report what would change but don't change anything. | ||
|
||
Run "pub help" to see global options. | ||
|
Uh oh!
There was an error while loading. Please reload this page.