Skip to content

Commit

Permalink
Merge pull request #13 from Workiva/improvements
Browse files Browse the repository at this point in the history
CP-822 CLI improvements & cleanup.
  • Loading branch information
jayudey-wf committed Aug 13, 2015
2 parents 8299ab2 + 6780413 commit dc6b56f
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 34 deletions.
2 changes: 1 addition & 1 deletion bin/dart_dev.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library dart_dev.bin.dart_dev;
import 'dart:io';

import 'package:dart_dev/dart_dev.dart' show dev;
import 'package:dart_dev/process.dart' show TaskProcess;
import 'package:dart_dev/util.dart' show TaskProcess;

main(List<String> args) async {
File devFile = new File('./tool/dev.dart');
Expand Down
4 changes: 0 additions & 4 deletions lib/io.dart

This file was deleted.

3 changes: 0 additions & 3 deletions lib/process.dart

This file was deleted.

20 changes: 16 additions & 4 deletions lib/src/dart_dev_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import 'dart:io';

import 'package:args/args.dart';

import 'package:dart_dev/io.dart'
show parseArgsFromCommand, parseExecutableFromCommand, reporter;
import 'package:dart_dev/process.dart';
import 'package:dart_dev/util.dart'
show
TaskProcess,
parseArgsFromCommand,
parseExecutableFromCommand,
reporter;

import 'package:dart_dev/src/tasks/cli.dart';
import 'package:dart_dev/src/tasks/config.dart';
Expand Down Expand Up @@ -75,7 +78,16 @@ Future _run(List<String> args) async {
_parser.addCommand(command, cli.argParser);
});

ArgResults env = _parser.parse(args);
ArgResults env;
try {
env = _parser.parse(args);
} on FormatException catch (e) {
reporter.error('${e.message}\n', shout: true);
reporter.log(_generateUsage(), shout: true);
exitCode = 1;
return;
}

String task;
if (env.command != null) {
task = env.command.name;
Expand Down
12 changes: 1 addition & 11 deletions lib/src/io.dart → lib/src/reporter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library dart_dev.src.io;
library dart_dev.src.reporter;

import 'dart:async';
import 'dart:io';
Expand All @@ -12,16 +12,6 @@ final AnsiPen _green = new AnsiPen()..green();
final AnsiPen _red = new AnsiPen()..red();
final AnsiPen _yellow = new AnsiPen()..yellow();

String parseExecutableFromCommand(String command) {
return command.split(' ').first;
}

List<String> parseArgsFromCommand(String command) {
var parts = command.split(' ');
if (parts.length <= 1) return [];
return parts.getRange(1, parts.length).toList();
}

class Reporter {
bool color = true;
bool quiet = false;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tasks/analyze/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library dart_dev.src.tasks.analyze.api;
import 'dart:async';
import 'dart:io';

import 'package:dart_dev/process.dart';
import 'package:dart_dev/util.dart' show TaskProcess;

import 'package:dart_dev/src/tasks/analyze/config.dart';
import 'package:dart_dev/src/tasks/task.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tasks/analyze/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:async';

import 'package:args/args.dart';

import 'package:dart_dev/io.dart' show reporter;
import 'package:dart_dev/util.dart' show reporter;

import 'package:dart_dev/src/tasks/analyze/api.dart';
import 'package:dart_dev/src/tasks/analyze/config.dart';
Expand Down
18 changes: 16 additions & 2 deletions lib/src/tasks/examples/api.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
library dart_dev.src.tasks.examples.api;

import 'dart:async';
import 'dart:io';

import 'package:dart_dev/process.dart';
import 'package:dart_dev/util.dart' show TaskProcess;

import 'package:dart_dev/src/tasks/examples/config.dart';
import 'package:dart_dev/src/tasks/task.dart';

bool hasExamples() {
Directory exampleDir = new Directory('example');
return exampleDir.existsSync();
}

ExamplesTask serveExamples(
{String hostname: defaultHostname, int port: defaultPort}) {
if (!hasExamples()) throw new Exception(
'This project does not have any examples.');

var dartiumExecutable = 'dartium';
var dartiumArgs = ['http://$hostname:$port'];

Expand Down Expand Up @@ -50,7 +59,12 @@ class ExamplesTask extends Task {
StreamController<String> _pubServeOutput = new StreamController();

ExamplesTask(String this.dartiumCommand, String this.pubServeCommand,
Future this.done);
Future this.done) {
done.then((_) {
_dartiumOutput.close();
_pubServeOutput.close();
});
}

Stream<String> get dartiumOutput => _dartiumOutput.stream;
Stream<String> get pubServeOutput => _pubServeOutput.stream;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/tasks/examples/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:async';

import 'package:args/args.dart';

import 'package:dart_dev/io.dart' show reporter;
import 'package:dart_dev/util.dart' show reporter;

import 'package:dart_dev/src/tasks/examples/api.dart';
import 'package:dart_dev/src/tasks/examples/config.dart';
Expand All @@ -29,6 +29,9 @@ class ExamplesCli extends TaskCli {
port = int.parse(port);
}

if (!hasExamples()) return new CliResult.fail(
'This project does not have any examples.');

ExamplesTask task = serveExamples(hostname: hostname, port: port);
reporter.logGroup(task.pubServeCommand, outputStream: task.pubServeOutput);
await task.done;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tasks/format/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ library dart_dev.src.tasks.format.api;

import 'dart:async';

import 'package:dart_dev/process.dart';
import 'package:dart_dev/util.dart' show TaskProcess;

import 'package:dart_dev/src/tasks/format/config.dart';
import 'package:dart_dev/src/tasks/task.dart';
Expand Down
13 changes: 12 additions & 1 deletion lib/src/tasks/format/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:async';

import 'package:args/args.dart';

import 'package:dart_dev/io.dart' show reporter;
import 'package:dart_dev/util.dart' show hasImmediateDependency, reporter;

import 'package:dart_dev/src/tasks/format/api.dart';
import 'package:dart_dev/src/tasks/format/config.dart';
Expand All @@ -22,6 +22,17 @@ class FormatCli extends TaskCli {
final String command = 'format';

Future<CliResult> run(ArgResults parsedArgs) async {
try {
if (!hasImmediateDependency('dart_style')) return new CliResult.fail(
'Package "dart_style" must be an immediate dependency in order to run its executables.');
} catch (e) {
// It's possible that this check may throw if the pubspec.yaml
// could not be found or if the yaml could not be parsed.
// We silence this error and let the task continue in case it
// was a false negative. If it was accurate, then the task will
// fail anyway and the error will be available in the output.
}

bool check = TaskCli.valueOf('check', parsedArgs, config.format.check);
List<String> directories = config.format.directories;

Expand Down
2 changes: 2 additions & 0 deletions lib/src/tasks/init/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const String _initialConfig = '''library tool.dev;
import 'package:dart_dev/dart_dev.dart' show dev, config;
main(List<String> args) async {
// https://github.com/Workiva/dart_dev
// Perform task configuration here as necessary.
// Available task configurations:
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tasks/test/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ library dart_dev.src.tasks.test.api;

import 'dart:async';

import 'package:dart_dev/process.dart';
import 'package:dart_dev/util.dart' show TaskProcess;

import 'package:dart_dev/src/tasks/task.dart';

Expand Down
5 changes: 4 additions & 1 deletion lib/src/tasks/test/cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:async';

import 'package:args/args.dart';

import 'package:dart_dev/io.dart';
import 'package:dart_dev/util.dart' show hasImmediateDependency, reporter;

import 'package:dart_dev/src/tasks/cli.dart';
import 'package:dart_dev/src/tasks/config.dart';
Expand All @@ -27,6 +27,9 @@ class TestCli extends TaskCli {
final String command = 'test';

Future<CliResult> run(ArgResults parsedArgs) async {
if (!hasImmediateDependency('test')) return new CliResult.fail(
'Package "test" must be an immediate dependency in order to run its executables.');

bool unit = parsedArgs['unit'];
bool integration = parsedArgs['integration'];
List<String> platforms =
Expand Down
28 changes: 28 additions & 0 deletions lib/src/util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
library dart_dev.src.util;

import 'dart:io';

import 'package:yaml/yaml.dart';

bool hasImmediateDependency(String packageName) {
File pubspec = new File('pubspec.yaml');
Map pubspecYaml = loadYaml(pubspec.readAsStringSync());
List deps = [];
if (pubspecYaml.containsKey('dependencies')) {
deps.addAll((pubspecYaml['dependencies'] as Map).keys);
}
if (pubspecYaml.containsKey('dev_dependencies')) {
deps.addAll((pubspecYaml['dev_dependencies'] as Map).keys);
}
return deps.contains(packageName);
}

String parseExecutableFromCommand(String command) {
return command.split(' ').first;
}

List<String> parseArgsFromCommand(String command) {
var parts = command.split(' ');
if (parts.length <= 1) return [];
return parts.getRange(1, parts.length).toList();
}
9 changes: 9 additions & 0 deletions lib/util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library dart_dev.util;

export 'package:dart_dev/src/reporter.dart' show Reporter, reporter;
export 'package:dart_dev/src/task_process.dart' show TaskProcess;
export 'package:dart_dev/src/util.dart'
show
hasImmediateDependency,
parseArgsFromCommand,
parseExecutableFromCommand;
4 changes: 2 additions & 2 deletions tool/dev.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ library dart_dev.dev;
import 'package:dart_dev/dart_dev.dart';

main(args) async {
config.analyze.entryPoints = ['lib/', 'test/', 'tool/'];
config.format.directories = ['lib/', 'test/', 'tool/'];
config.analyze.entryPoints = ['bin/', 'lib/', 'tool/'];
config.format.directories = ['bin/', 'lib/', 'tool/'];

await dev(args);
}

0 comments on commit dc6b56f

Please sign in to comment.