-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split the project into smaller files (#16)
- Loading branch information
Showing
12 changed files
with
160 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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](); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |