Skip to content

Commit

Permalink
Adds "bump build" command to address #13 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Aug 18, 2019
1 parent 6144f24 commit ff71f1f
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 43 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.4.0] - 2019-08-18
### Added
- the `bump build` command

## [0.3.0] - 2019-05-29
### Added
- the `get` command ([PR](https://github.com/f3ath/pubspec-version/pull/14))
Expand Down Expand Up @@ -33,7 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial version

[Unreleased]: https://github.com/f3ath/pubspec-version/compare/0.3.0...HEAD
[Unreleased]: https://github.com/f3ath/pubspec-version/compare/0.4.0...HEAD
[0.4.0]: https://github.com/f3ath/pubspec-version/compare/0.3.0...0.4.0
[0.3.0]: https://github.com/f3ath/pubspec-version/compare/0.2.2...0.3.0
[0.2.2]: https://github.com/f3ath/pubspec-version/compare/0.2.1...0.2.2
[0.2.1]: https://github.com/f3ath/pubspec-version/compare/0.2.0...0.2.1
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# pubver
CLI tool to set/bump the `version` key in pubspec.yaml. Semver-compliant\*.
CLI tool to set/bump the `version` key in pubspec.yaml. Semver-compliant (Almost.
It uses [pub_semver](https://pub.dartlang.org/packages/pub_semver) which is a bit different.)

## Installing
You can install the package from the command line:
Install the package from the command line:
```
pub global activate pubspec_version
```
Expand All @@ -13,7 +14,7 @@ This will add the **pubver** binary to your `~/.pub-cache/bin`.
```
pubver bump <part>
```
where `<part>` can be either **breaking**, **major**, **minor** or **patch**.
where `<part>` can be either **breaking**, **major**, **minor**, **patch** or **build**.

#### Examples
Before | Command | After
Expand All @@ -23,6 +24,14 @@ Before | Command | After
0.2.1 | `pubver bump major` | 1.0.0
0.2.1 | `pubver bump minor` | 0.3.0
0.2.1 | `pubver bump patch` | 0.2.2
0.2.1 | `pubver bump build` | 0.2.1+1
0.2.1+42 | `pubver bump build` | 0.2.1+43
0.2.1+foo | `pubver bump build` | 0.2.1+foo.1
0.2.1+42.foo | `pubver bump build` | 0.2.1+43.foo
0.2.1+foo.bar.1.2 | `pubver bump build` | 0.2.1+foo.bar.2.2

The `bump build` command is a bit tricky. It either increments the first numeric part of the build (if there is a
numeric part) or appends `.1` to the build (otherwise).

### Setting the version
```
Expand All @@ -40,9 +49,3 @@ The tool prints the new version to stdout. This allows post processing, e.g. mak
```bash
git ci . -m "Release $(pubver bump breaking)"
```
```
pubver set "$(pubver get)+1"
```

___
\*almost. It uses [pub_semver](https://pub.dartlang.org/packages/pub_semver) which is a bit different.
22 changes: 13 additions & 9 deletions lib/src/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import 'package:args/command_runner.dart';
import 'package:pubspec_version/src/bump_version.dart';
import 'package:pubspec_version/src/bumper.dart';
import 'package:pubspec_version/src/console.dart';
import 'package:pubspec_version/src/get_version.dart';
import 'package:pubspec_version/src/set_version.dart';
import 'package:pubspec_version/src/get_version_command.dart';
import 'package:pubspec_version/src/next_build.dart';
import 'package:pubspec_version/src/set_version_command.dart';

class Application {
final Console console;
Expand All @@ -14,16 +15,19 @@ class Application {

Future<int> run(List<String> args) async {
final bumpers = [
Bumper('breaking', 'Bumps the version to the next breaking.', console),
Bumper('major', 'Bumps the major version.', console),
Bumper('minor', 'Bumps the minor version.', console),
Bumper('patch', 'Bumps the patch version.', console),
Bumper('breaking', 'Bumps the version to the next breaking.', console,
(_) => _.nextBreaking),
Bumper('major', 'Bumps the major version.', console, (_) => _.nextMajor),
Bumper('minor', 'Bumps the minor version.', console, (_) => _.nextMinor),
Bumper('patch', 'Bumps the patch version.', console, (_) => _.nextPatch),
Bumper('build', 'Bumps the first numeric part of the build version.',
console, nextBuild),
];
final commandRunner =
await CommandRunner('pubver', 'Package version manager.')
..addCommand(BumpVersion(bumpers))
..addCommand(SetVersion(console))
..addCommand(GetVersion(console))
..addCommand(BumpVersionCommand(bumpers))
..addCommand(SetVersionCommand(console))
..addCommand(GetVersionCommand(console))
..argParser.addOption('pubspec-dir',
abbr: 'd',
help: 'Directory containing pubspec.yaml.',
Expand Down
4 changes: 2 additions & 2 deletions lib/src/bump_version.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:args/command_runner.dart';

class BumpVersion<T> extends Command<T> {
class BumpVersionCommand<T> extends Command<T> {
final name = 'bump';
final description = 'Bumps the package version.';

BumpVersion(List<Command<T>> subcommands) {
BumpVersionCommand(List<Command<T>> subcommands) {
subcommands.forEach(addSubcommand);
}
}
16 changes: 7 additions & 9 deletions lib/src/bumper.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_version/src/update_version.dart';
import 'package:pubspec_version/src/version_command.dart';

import 'console.dart';

class Bumper extends UpdateVersion {
class Bumper extends VersionCommand {
final String name;
final String description;
final _Mutator mutator;

Bumper(this.name, this.description, Console c) : super(c);
Bumper(this.name, this.description, Console c, this.mutator) : super(c);

Version nextVersion(Version v) => {
'breaking': () => v.nextBreaking,
'major': () => v.nextMajor,
'minor': () => v.nextMinor,
'patch': () => v.nextPatch,
}[name]();
Version nextVersion(Version v) => mutator(v);
}

typedef Version _Mutator(Version v);
4 changes: 2 additions & 2 deletions lib/src/get_version.dart → lib/src/get_version_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import 'package:args/command_runner.dart';
import 'package:pubspec/pubspec.dart';
import 'package:pubspec_version/src/console.dart';

class GetVersion extends Command {
class GetVersionCommand extends Command {
final Console console;
final name = 'get';
final description = 'Gets the current package version.';

GetVersion(this.console);
GetVersionCommand(this.console);

@override
Future run() async {
Expand Down
12 changes: 12 additions & 0 deletions lib/src/next_build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:pub_semver/pub_semver.dart';

Version nextBuild(Version v) {
final build = List.from(v.build);
final firstIntegerIndex = build.indexWhere((_) => _ is int);
if (firstIntegerIndex == -1) return _withBuild(v, (build + [1]).join('.'));
build[firstIntegerIndex] += 1;
return _withBuild(v, (build).join('.'));
}

Version _withBuild(Version v, String build) =>
Version(v.major, v.minor, v.patch, build: build);
6 changes: 3 additions & 3 deletions lib/src/set_version.dart → lib/src/set_version_command.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:args/command_runner.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_version/src/console.dart';
import 'package:pubspec_version/src/update_version.dart';
import 'package:pubspec_version/src/version_command.dart';

class SetVersion extends UpdateVersion {
class SetVersionCommand extends VersionCommand {
final name = 'set';
final description = 'Sets the package version.';

SetVersion(Console c) : super(c);
SetVersionCommand(Console c) : super(c);

Version nextVersion(Version v) {
if (globalResults.arguments.length < 2) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/update_version.dart → lib/src/version_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec/pubspec.dart';
import 'package:pubspec_version/src/console.dart';

abstract class UpdateVersion extends Command {
abstract class VersionCommand extends Command {
final Console console;

UpdateVersion(this.console);
VersionCommand(this.console);

Future run() async {
final dir = Directory(globalResults['pubspec-dir']);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
author: "Alexey Karapetov <karapetov@gmail.com>"
description: "A CLI tool to set/bump the package version in pubspec.yaml. Think of \"npm version\" for Dart."
description: "A CLI tool to set/bump the package version in pubspec.yaml. The \"npm version\" for Dart."
homepage: "https://github.com/f3ath/pubspec-version"
name: "pubspec_version"
version: "0.3.0"
version: "0.4.0"
dependencies:
args: "^1.5.0"
pub_semver: "^1.4.2"
Expand Down
12 changes: 9 additions & 3 deletions test/functional_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ void main() {
await expectVersion('0.3.3');
});

test('bump build', () async {
final code = await app.run(['bump', 'build', '-d', temp.path]);
expect(code, 0);
await expectVersion('0.3.2+43');
});

test('set', () async {
final code = await app.run(['set', '5.4.321', '-d', temp.path]);
final code = await app.run(['set', '5.4.321+12', '-d', temp.path]);
expect(code, 0);
await expectVersion('5.4.321');
await expectVersion('5.4.321+12');
});

test('set called without version', () async {
Expand All @@ -68,6 +74,6 @@ void main() {
test('get', () async {
final code = await app.run(['get', '-d', temp.path]);
expect(code, 0);
await expectVersion('0.3.2');
await expectVersion('0.3.2+42');
});
}
2 changes: 1 addition & 1 deletion test/pubspec_sample.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
description: "A CLI tool to set/bump the `version` key in pubspec.yaml."
name: "pubspec_version"
version: "0.3.2"
version: "0.3.2+42"
dependencies:
args: "^1.5.0"
pub_semver: "^1.4.2"
Expand Down
20 changes: 20 additions & 0 deletions test/unit/next_build_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_version/src/next_build.dart';
import 'package:test/test.dart';

void main() {
test('bumping empty build sets it to 1', () async {
final v = Version.parse('1.2.3');
expect(nextBuild(v).toString(), '1.2.3+1');
});

test('non-numeric build gets ".1" appended to it', () async {
final v = Version.parse('1.2.3+foo42');
expect(nextBuild(v).toString(), '1.2.3+foo42.1');
});

test('in a complex build the first numeric part gets incremented', () async {
final v = Version.parse('1.2.3+foo.1.2.3.bar');
expect(nextBuild(v).toString(), '1.2.3+foo.2.2.3.bar');
});
}

0 comments on commit ff71f1f

Please sign in to comment.