Skip to content

Commit

Permalink
Add --retain-build flag (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Sep 2, 2019
1 parent 8470405 commit b7c6155
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 144 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.6.0] - 2019-09-02
### Added
- the `--retain-build` (`-b`) flag to retain the build number when bumping versions

## [0.5.0] - 2019-08-18
### Changed
- the `bump build` command sets the subsequent numeric build parts to zeroes
Expand Down Expand Up @@ -41,7 +45,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.5.0...HEAD
[Unreleased]: https://github.com/f3ath/pubspec-version/compare/0.6.0...HEAD
[0.6.0]: https://github.com/f3ath/pubspec-version/compare/0.5.0...0.6.0
[0.5.0]: https://github.com/f3ath/pubspec-version/compare/0.4.0...0.5.0
[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
Expand Down
2 changes: 1 addition & 1 deletion bin/pubver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import 'dart:io';
import 'package:pubspec_version/pubspec_version.dart';

void main(List<String> args) async =>
exit(await Application(Console.stdio()).run(args));
exit((await buildApp(Console.stdio()).run(args)) ?? 0);
2 changes: 1 addition & 1 deletion lib/pubspec_version.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export 'package:pubspec_version/src/application.dart';
export 'package:pubspec_version/src/cli/build_app.dart';
export 'package:pubspec_version/src/console.dart';
44 changes: 0 additions & 44 deletions lib/src/application.dart

This file was deleted.

10 changes: 0 additions & 10 deletions lib/src/bump_version.dart

This file was deleted.

16 changes: 0 additions & 16 deletions lib/src/bumper.dart

This file was deleted.

88 changes: 88 additions & 0 deletions lib/src/cli/build_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'package:args/command_runner.dart';
import 'package:pubspec_version/src/cli/command_container.dart';
import 'package:pubspec_version/src/cli/pub_spec_command.dart';
import 'package:pubspec_version/src/console.dart';

CommandRunner<int> buildApp(Console console) =>
CommandRunner<int>('pubver', 'Package version manager.')
..addCommand(CommandContainer(
'bump',
'Bumps the package version.',
[
PubSpecCommand(
'breaking',
'Bumps the version to the next breaking.',
(_) async {
await _.bumpBreaking();
console.log(await _.readVersion());
return 0;
},
),
PubSpecCommand(
'major',
'Bumps the major version.',
(_) async {
await _.bumpMajor();
console.log(await _.readVersion());
return 0;
},
),
PubSpecCommand(
'minor',
'Bumps the minor version.',
(_) async {
await _.bumpMinor();
console.log(await _.readVersion());
return 0;
},
),
PubSpecCommand(
'patch',
'Bumps the patch version.',
(_) async {
await _.bumpPatch();
console.log(await _.readVersion());
return 0;
},
),
PubSpecCommand(
'build',
'Bumps the first numeric part of the build version.',
(_) async {
await _.bumpBuild();
console.log(await _.readVersion());
return 0;
},
),
],
))
..addCommand(PubSpecCommand(
'set',
'Sets the package version.',
(_) async {
if (_.arguments.length < 2) {
console.error(
'Please provide the version' + '\n' * 2 + 'Example: set 3.2.1');
return 64;
}
await _.writeVersion(_.getArgument(1));
console.log(await _.readVersion());
return 0;
},
))
..addCommand(PubSpecCommand(
'get',
'Gets the current package version.',
(_) async {
console.log(await _.readVersion());
return 0;
},
))
..argParser.addOption('pubspec-dir',
abbr: 'd',
help: 'Directory containing pubspec.yaml.',
defaultsTo: '.')
..argParser.addFlag('retain-build',
abbr: 'b',
help: 'Retain build when bumping major, minor, or patch.',
defaultsTo: false);
10 changes: 10 additions & 0 deletions lib/src/cli/command_container.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:args/command_runner.dart';

class CommandContainer<T> extends Command<T> {
final String name;
final String description;

CommandContainer(this.name, this.description, List<Command<T>> subcommands) {
subcommands.forEach(addSubcommand);
}
}
36 changes: 36 additions & 0 deletions lib/src/cli/pub_spec_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:args/command_runner.dart';
import 'package:pubspec_version/src/pubspec_version_app.dart';

class PubSpecCommand extends Command<int> {
final String description;
final String name;
final _Payload _payload;

PubSpecCommand(this.name, this.description, this._payload);

bool get retainBuild => globalResults['retain-build'];

Future<String> readVersion() async => (await _app.readVersion()).toString();

Future<void> writeVersion(String version) => _app.writeVersionString(version);

Future<void> bumpBreaking() => _app.bumpBreaking(retainBuild: retainBuild);

Future<void> bumpMajor() => _app.bumpMajor(retainBuild: retainBuild);

Future<void> bumpMinor() => _app.bumpMinor(retainBuild: retainBuild);

Future<void> bumpPatch() => _app.bumpPatch(retainBuild: retainBuild);

Future<void> bumpBuild() => _app.bumpBuild();

String getArgument(int index) => globalResults.arguments[index];

List<String> get arguments => globalResults.arguments;

Future<int> run() => _payload(this);

PubSpecVersionApp get _app => PubSpecVersionApp(globalResults['pubspec-dir']);
}

typedef Future<int> _Payload(PubSpecCommand command);
4 changes: 2 additions & 2 deletions lib/src/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Console {
Console.stdio() : this(stdout, stderr);

/// Writes the [message] to the error sink
void error(Object message) => _error.writeln(message);
void error(Object message) => _error.writeln(message.toString());

/// Writes the [message] the to the normal output sink
void log(Object message) => _output.writeln(message);
void log(Object message) => _output.writeln(message.toString());
}
21 changes: 0 additions & 21 deletions lib/src/get_version_command.dart

This file was deleted.

48 changes: 48 additions & 0 deletions lib/src/pubspec_version_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:io';

import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec/pubspec.dart' as ps;
import 'package:pubspec_version/src/version_transform.dart';

class PubSpecVersionApp {
final Directory _dir;

PubSpecVersionApp(String path) : _dir = Directory(path);

Future<VersionTransform> readVersion() async {
final pubSpec = await _load();
return VersionTransform(pubSpec.version);
}

Future<void> writeVersion(VersionTransform version) async {
final pubSpec = await _load();
await pubSpec.copy(version: version.version).save(_dir);
}

Future<void> writeVersionString(String version) =>
writeVersion(VersionTransform(Version.parse(version)));

Future<void> bumpBreaking({bool retainBuild = false}) =>
_update((_) => _.bumpBreaking(), retainBuild: retainBuild);

Future<void> bumpMajor({bool retainBuild = false}) =>
_update((_) => _.bumpMajor(), retainBuild: retainBuild);

Future<void> bumpMinor({bool retainBuild = false}) =>
_update((_) => _.bumpMinor(), retainBuild: retainBuild);

Future<void> bumpPatch({bool retainBuild = false}) =>
_update((_) => _.bumpPatch(), retainBuild: retainBuild);

Future<void> bumpBuild() => _update((_) => _.bumpBuild());

Future<void> _update(VersionTransform mutate(VersionTransform v),
{bool retainBuild = false}) async {
final version = await readVersion();
var newVersion =
retainBuild ? mutate(version).withBuildFrom(version) : mutate(version);
await writeVersion(newVersion);
}

Future<ps.PubSpec> _load() => ps.PubSpec.load(_dir);
}
18 changes: 0 additions & 18 deletions lib/src/set_version_command.dart

This file was deleted.

23 changes: 0 additions & 23 deletions lib/src/version_command.dart

This file was deleted.

Loading

0 comments on commit b7c6155

Please sign in to comment.