Skip to content

Commit

Permalink
Split the project into smaller files (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored May 30, 2019
1 parent 7846ee5 commit 6144f24
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 127 deletions.
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 App(Console.stdio()).run(args));
exit(await Application(Console.stdio()).run(args));
120 changes: 2 additions & 118 deletions lib/pubspec_version.dart
Original file line number Diff line number Diff line change
@@ -1,118 +1,2 @@
import 'dart:async';
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec/pubspec.dart';

class App {
final Console console;

App(this.console);

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),
];
final commandRunner =
await CommandRunner('pubver', 'Package version manager.')
..addCommand(BumpVersion(bumpers))
..addCommand(SetVersion(console))
..addCommand(GetVersion(console))
..argParser.addOption('pubspec-dir',
abbr: 'd',
help: 'Directory containing pubspec.yaml.',
defaultsTo: '.');

try {
await commandRunner.run(args);
return 0;
} on UsageException catch (e) {
console.error(e.toString());
return 64;
}
}
}

class Bumper extends UpdateVersion {
final String name;
final String description;

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

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

class Console {
final Stdout _output;
final Stdout _error;

Console(this._output, this._error);

Console.stdio() : this(stdout, stderr);

void error(Object e) => _error.writeln(e);

void log(Object message) => _output.writeln(message);
}

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

BumpVersion(List<Command<T>> subcommands) {
subcommands.forEach(addSubcommand);
}
}

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

SetVersion(Console c) : super(c);

Version nextVersion(Version v) {
if (globalResults.arguments.length < 2)
throw UsageException('Please provide the version', 'Example: set 3.2.1');
return Version.parse(globalResults.arguments[1]);
}
}

abstract class UpdateVersion extends Command {
final Console console;

UpdateVersion(this.console);

Future run() async {
final dir = Directory(globalResults['pubspec-dir']);
final pubSpec = await PubSpec.load(dir);
final version = nextVersion(pubSpec.version);
await pubSpec.copy(version: version).save(dir);
console.log(version.toString());
}

Version nextVersion(Version v);
}

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

GetVersion(this.console);

@override
Future run() async {
final dir = Directory(globalResults['pubspec-dir']);
final pubSpec = await PubSpec.load(dir);
console.log(pubSpec.version.toString());
}
}
export 'package:pubspec_version/src/application.dart';
export 'package:pubspec_version/src/console.dart';
40 changes: 40 additions & 0 deletions lib/src/application.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:async';

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';

class Application {
final Console console;

Application(this.console);

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),
];
final commandRunner =
await CommandRunner('pubver', 'Package version manager.')
..addCommand(BumpVersion(bumpers))
..addCommand(SetVersion(console))
..addCommand(GetVersion(console))
..argParser.addOption('pubspec-dir',
abbr: 'd',
help: 'Directory containing pubspec.yaml.',
defaultsTo: '.');

try {
await commandRunner.run(args);
return 0;
} on UsageException catch (e) {
console.error(e.toString());
return 64;
}
}
}
10 changes: 10 additions & 0 deletions lib/src/bump_version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:args/command_runner.dart';

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

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

import 'console.dart';

class Bumper extends UpdateVersion {
final String name;
final String description;

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

Version nextVersion(Version v) => {
'breaking': () => v.nextBreaking,
'major': () => v.nextMajor,
'minor': () => v.nextMinor,
'patch': () => v.nextPatch,
}[name]();
}
19 changes: 19 additions & 0 deletions lib/src/console.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'dart:io';

/// A very simple Console output abstraction.
/// Allows to print log messages and error messages.
class Console {
final Stdout _output;
final Stdout _error;

/// Creates an instance with
const Console(this._output, this._error);

Console.stdio() : this(stdout, stderr);

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

/// Writes the [message] the to the normal output sink
void log(Object message) => _output.writeln(message);
}
21 changes: 21 additions & 0 deletions lib/src/get_version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:async';
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:pubspec/pubspec.dart';
import 'package:pubspec_version/src/console.dart';

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

GetVersion(this.console);

@override
Future run() async {
final dir = Directory(globalResults['pubspec-dir']);
final pubSpec = await PubSpec.load(dir);
console.log(pubSpec.version.toString());
}
}
18 changes: 18 additions & 0 deletions lib/src/set_version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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';

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

SetVersion(Console c) : super(c);

Version nextVersion(Version v) {
if (globalResults.arguments.length < 2) {
throw UsageException('Please provide the version', 'Example: set 3.2.1');
}
return Version.parse(globalResults.arguments[1]);
}
}
23 changes: 23 additions & 0 deletions lib/src/update_version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:async';
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec/pubspec.dart';
import 'package:pubspec_version/src/console.dart';

abstract class UpdateVersion extends Command {
final Console console;

UpdateVersion(this.console);

Future run() async {
final dir = Directory(globalResults['pubspec-dir']);
final pubSpec = await PubSpec.load(dir);
final version = nextVersion(pubSpec.version);
await pubSpec.copy(version: version).save(dir);
console.log(version.toString());
}

Version nextVersion(Version v);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ dev_dependencies:
mockito: "^3.0.0"
test: "^1.0.0"
environment:
sdk: ">=2.0.0 <3.0.0"
sdk: ">=2.3.0 <3.0.0"
executables:
pubver: "pubver"
4 changes: 2 additions & 2 deletions test/functional_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MockConsole extends Mock implements Console {}
void main() {
Directory temp;
MockConsole mockConsole;
App app;
Application app;

expectVersion(String v) async {
expect((await PubSpec.load(Directory(temp.path))).version.toString(), v);
Expand All @@ -21,7 +21,7 @@ void main() {
temp = await Directory.systemTemp.createTemp();
File('test/pubspec_sample.yaml').copy('${temp.path}/pubspec.yaml');
mockConsole = MockConsole();
app = App(mockConsole);
app = Application(mockConsole);
});

tearDown(() async {
Expand Down
10 changes: 5 additions & 5 deletions test/pubspec_sample.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
description: "A CLI tool to set/bump the `version` key in pubspec.yaml."
name: "pubspec_version"
version: "0.3.2"
dependencies:
dependencies:
args: "^1.5.0"
pub_semver: "^1.4.2"
pubspec: "^0.1.0"
dev_dependencies:
dev_dependencies:
test: "^1.0.0"
environment:
environment:
sdk: ">=2.0.0 <3.0.0"
executables:
pubspec-version: "pubspec-version"
executables:
pubver: "pubver"

0 comments on commit 6144f24

Please sign in to comment.