Skip to content

Commit

Permalink
add!: proper sub commands for bump and log commands
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-kolja committed Jan 29, 2024
1 parent ac25b19 commit 86e6594
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 30 deletions.
65 changes: 40 additions & 25 deletions lib/src/cli/command/bump_command.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
import 'package:args/command_runner.dart';
import 'package:cider/src/project.dart';
import 'package:cider/src/cli/command/bump_sub_command.dart';
import 'package:cider/src/cli/command/cider_command.dart';
import 'package:version_manipulation/mutations.dart';

enum BumpType {
breaking,
major,
minor,
patch,
build,
pre,
release,
}

class BumpCommand extends CiderCommand {
BumpCommand(super.printer) {
mutations.keys.forEach(argParser.addCommand);
argParser
..addFlag('keep-build', help: 'Keep the existing build')
..addFlag('bump-build', help: 'Also bump the build')
..addOption('build',
help: 'Sets the build to the given value', defaultsTo: '')
..addOption('pre',
help: 'Sets the pre-release to the given value', defaultsTo: '');
for (final type in BumpType.values) {
addSubcommand(BumpSubCommand(
type.name,
description: _subcommandDescriptions[type]!,
mutation: _mutations[type]!,
printer: printer,
));
}
}

static const mutations = <String, VersionMutation>{
'breaking': BumpBreaking(),
'build': BumpBuild(),
'major': BumpMajor(),
'minor': BumpMinor(),
'patch': BumpPatch(),
'pre': BumpPreRelease(),
'release': Release(),
static const _mutations = <BumpType, VersionMutation>{
BumpType.breaking: BumpBreaking(),
BumpType.major: BumpMajor(),
BumpType.minor: BumpMinor(),
BumpType.patch: BumpPatch(),
BumpType.build: BumpBuild(),
BumpType.pre: BumpPreRelease(),
BumpType.release: Release(),
};

static const _subcommandDescriptions = <BumpType, String>{
BumpType.breaking: 'Bump the breaking version',
BumpType.major: 'Bump the major version',
BumpType.minor: 'Bump the minor version',
BumpType.patch: 'Bump the patch version',
BumpType.build: 'Bump the build version',
BumpType.pre: 'Bump the pre-release version',
BumpType.release: 'Bump the release version',
};

@override
Expand All @@ -31,14 +53,7 @@ class BumpCommand extends CiderCommand {

@override
Future<int> exec(Project project) async {
final part = argResults!.command?.name ??
(throw ArgumentError('Version part must be specified'));
final result = await project.bumpVersion(mutations[part]!,
keepBuild: argResults!['keep-build'],
bumpBuild: argResults!['bump-build'],
build: argResults!['build'],
pre: argResults!['pre']);
printer.out.writeln(result);
return 0;
throw UsageException(
'Bump command can only be used with subcommands', usage);
}
}
41 changes: 41 additions & 0 deletions lib/src/cli/command/bump_sub_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:cider/src/cli/console.dart';
import 'package:cider/src/project.dart';
import 'package:cider/src/cli/command/cider_command.dart';
import 'package:version_manipulation/mutations.dart';

class BumpSubCommand extends CiderCommand {
BumpSubCommand(
this.name, {
required this.description,
required this.mutation,
required Console printer,
}) : super(printer) {
argParser
..addFlag('keep-build', help: 'Keep the existing build')
..addFlag('bump-build', help: 'Also bump the build')
..addOption('build',
help: 'Sets the build to the given value', defaultsTo: '')
..addOption('pre',
help: 'Sets the pre-release to the given value', defaultsTo: '');
}

@override
final String name;

@override
final String description;

final VersionMutation mutation;

@override
Future<int> exec(Project project) async {
final result = await project.bumpVersion(mutation,
keepBuild: argResults!['keep-build'],
bumpBuild: argResults!['bump-build'],
build: argResults!['build'],
pre: argResults!['pre']);
printer.out.writeln(result);

return 0;
}
}
35 changes: 32 additions & 3 deletions lib/src/cli/command/log_command.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import 'package:args/command_runner.dart';
import 'package:cider/src/cli/command/cider_command.dart';
import 'package:cider/src/cli/command/log_sub_command.dart';
import 'package:cider/src/project.dart';

enum LogType {
add,
fix,
change,
deprecate,
remove,
security,
}

class LogCommand extends CiderCommand {
LogCommand(super.printer);
LogCommand(super.printer) {
for (final type in LogType.values) {
addSubcommand(LogSubCommand(
type.name,
type: type,
description: _subcommandDescriptions[type]!,
printer: printer,
));
}
}

static const _subcommandDescriptions = <LogType, String>{
LogType.add: 'Add a new feature to the changelog',
LogType.fix: 'Add a new bug fix to the changelog',
LogType.change: 'Add a new change to the changelog',
LogType.deprecate: 'Add a new deprecation to the changelog',
LogType.remove: 'Add a new removal to the changelog',
LogType.security: 'Add a new security fix to the changelog',
};

@override
final name = 'log';
Expand All @@ -12,7 +41,7 @@ class LogCommand extends CiderCommand {

@override
Future<int> exec(Project project) async {
await project.addUnreleased(argResults!.rest.first, argResults!.rest[1]);
return 0;
throw UsageException(
'Log command can only be used with subcommands', usage);
}
}
28 changes: 28 additions & 0 deletions lib/src/cli/command/log_sub_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:cider/src/cli/command/log_command.dart';
import 'package:cider/src/cli/console.dart';
import 'package:cider/src/project.dart';
import 'package:cider/src/cli/command/cider_command.dart';

class LogSubCommand extends CiderCommand {
LogSubCommand(
this.name, {
required this.description,
required this.type,
required Console printer,
}) : super(printer);

@override
final String name;

@override
final String description;

final LogType type;

@override
Future<int> exec(Project project) async {
await project.addUnreleased(type.name, argResults!.rest[0]);

return 0;
}
}
5 changes: 3 additions & 2 deletions test/functional_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,9 @@ I love my dog.
});
test('version part must be specified', () async {
final code = await run(['bump']);
expect(code, 65);
expect(err.buffer.toString().trim(), 'Version part must be specified');
expect(code, 64);
expect(err.buffer.toString().trim().split('\n')[0],
'Usage: cider bump <subcommand> [arguments]');
});
});
});
Expand Down

0 comments on commit 86e6594

Please sign in to comment.