Skip to content

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

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions lib/src/command/bump.dart
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.');
Copy link
Member

@jonasfj jonasfj Sep 4, 2024

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:

Would update version from $currentVersion to $newVersion.
- version: $currentVersion
+ version: $newVersion

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Display a message that says: "Do update CHANGELOG.md before publishing!"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
}
2 changes: 2 additions & 0 deletions lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:path/path.dart' as p;

import 'command.dart' show PubTopLevel, lineLength;
import 'command/add.dart';
import 'command/bump.dart';
import 'command/cache.dart';
import 'command/deps.dart';
import 'command/downgrade.dart';
Expand Down Expand Up @@ -139,6 +140,7 @@ class PubCommandRunner extends CommandRunner<int> implements PubTopLevel {
// When adding new commands be sure to also add them to
// `pub_embeddable_command.dart`.
addCommand(AddCommand());
addCommand(BumpCommand());
addCommand(CacheCommand());
addCommand(DepsCommand());
addCommand(DowngradeCommand());
Expand Down
2 changes: 2 additions & 0 deletions lib/src/pub_embeddable_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'command.dart' show PubCommand, PubTopLevel;
import 'command.dart';
import 'command/add.dart';
import 'command/bump.dart';
import 'command/cache.dart';
import 'command/deps.dart';
import 'command/downgrade.dart';
Expand Down Expand Up @@ -69,6 +70,7 @@ class PubEmbeddableCommand extends PubCommand implements PubTopLevel {
//
// New commands should (most likely) be included in both lists.
addSubcommand(AddCommand());
addSubcommand(BumpCommand());
addSubcommand(CacheCommand());
addSubcommand(DepsCommand());
addSubcommand(DowngradeCommand());
Expand Down
53 changes: 53 additions & 0 deletions test/bump_test.dart
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();
});
}
1 change: 1 addition & 0 deletions test/testdata/goldens/embedding/embedding_test/--help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Usage: pub_command_runner pub [arguments...]

Available subcommands:
add Add dependencies to `pubspec.yaml`.
bump Increases the version number of the current package.
cache Work with the system cache.
deps Print package dependencies.
downgrade Downgrade the current package's dependencies to oldest versions.
Expand Down
17 changes: 17 additions & 0 deletions test/testdata/goldens/help_test/pub bump --help.txt
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)
-n, --dry-run Report what would change but don't change anything.

Run "pub help" to see global options.