diff --git a/bin/pubver.dart b/bin/pubver.dart index ade1111..7daf311 100644 --- a/bin/pubver.dart +++ b/bin/pubver.dart @@ -3,4 +3,4 @@ import 'dart:io'; import 'package:pubspec_version/pubspec_version.dart'; void main(List args) async => - exit(await App(Console.stdio()).run(args)); + exit(await Application(Console.stdio()).run(args)); diff --git a/lib/pubspec_version.dart b/lib/pubspec_version.dart index b340246..0b385ed 100644 --- a/lib/pubspec_version.dart +++ b/lib/pubspec_version.dart @@ -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 run(List 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 extends Command { - final name = 'bump'; - final description = 'Bumps the package version.'; - - BumpVersion(List> 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'; diff --git a/lib/src/application.dart b/lib/src/application.dart new file mode 100644 index 0000000..bcd4153 --- /dev/null +++ b/lib/src/application.dart @@ -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 run(List 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; + } + } +} diff --git a/lib/src/bump_version.dart b/lib/src/bump_version.dart new file mode 100644 index 0000000..35f1bd3 --- /dev/null +++ b/lib/src/bump_version.dart @@ -0,0 +1,10 @@ +import 'package:args/command_runner.dart'; + +class BumpVersion extends Command { + final name = 'bump'; + final description = 'Bumps the package version.'; + + BumpVersion(List> subcommands) { + subcommands.forEach(addSubcommand); + } +} diff --git a/lib/src/bumper.dart b/lib/src/bumper.dart new file mode 100644 index 0000000..db75cd7 --- /dev/null +++ b/lib/src/bumper.dart @@ -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](); +} diff --git a/lib/src/console.dart b/lib/src/console.dart new file mode 100644 index 0000000..32e4524 --- /dev/null +++ b/lib/src/console.dart @@ -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); +} diff --git a/lib/src/get_version.dart b/lib/src/get_version.dart new file mode 100644 index 0000000..3000b6f --- /dev/null +++ b/lib/src/get_version.dart @@ -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()); + } +} diff --git a/lib/src/set_version.dart b/lib/src/set_version.dart new file mode 100644 index 0000000..021d1ea --- /dev/null +++ b/lib/src/set_version.dart @@ -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]); + } +} diff --git a/lib/src/update_version.dart b/lib/src/update_version.dart new file mode 100644 index 0000000..7de64a7 --- /dev/null +++ b/lib/src/update_version.dart @@ -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); +} diff --git a/pubspec.yaml b/pubspec.yaml index 49e9c75..c4cea00 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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" diff --git a/test/functional_test.dart b/test/functional_test.dart index 6f2fdd7..04ea557 100644 --- a/test/functional_test.dart +++ b/test/functional_test.dart @@ -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); @@ -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 { diff --git a/test/pubspec_sample.yaml b/test/pubspec_sample.yaml index 80dd4a0..8442c06 100644 --- a/test/pubspec_sample.yaml +++ b/test/pubspec_sample.yaml @@ -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"