Skip to content

Commit 0976e90

Browse files
committed
moved show executables flag to deps subcommand
* run --list flag is removed * buffer is used to output executables * tests are adapted for --dev/--no-dev flag
1 parent 6cfb71e commit 0976e90

6 files changed

+347
-238
lines changed

lib/src/command/deps.dart

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,37 @@
44

55
import 'dart:collection';
66

7+
import 'package:analyzer/analyzer.dart' as analyzer;
8+
import 'package:path/path.dart' as p;
9+
710
import '../ascii_tree.dart' as tree;
811
import '../command.dart';
12+
import '../dart.dart';
913
import '../log.dart' as log;
1014
import '../package.dart';
15+
import '../package_name.dart';
1116
import '../utils.dart';
1217

18+
/// Returns `true` if [path] looks like a Dart entrypoint.
19+
bool _isDartExecutable(String path) {
20+
if (p.extension(path) == '.dart') {
21+
try {
22+
var unit = analyzer.parseDartFile(path, parseFunctionBodies: false);
23+
return isEntrypoint(unit);
24+
} on analyzer.AnalyzerErrorGroup {
25+
return false;
26+
}
27+
}
28+
29+
return false;
30+
}
31+
32+
/// The immediate non-dev dependencies this package specifies in its pubspec.
33+
List<PackageRange> _nonDevDependencies(Package package) {
34+
var toSkip = package.devDependencies.map((d) => d.name);
35+
return package.immediateDependencies.where((r) => !toSkip.contains(r.name));
36+
}
37+
1338
/// Handles the `deps` pub command.
1439
class DepsCommand extends PubCommand {
1540
String get name => "deps";
@@ -36,6 +61,9 @@ class DepsCommand extends PubCommand {
3661
negatable: true,
3762
help: "Whether to include dev dependencies.",
3863
defaultsTo: true);
64+
65+
argParser.addFlag("executables",
66+
negatable: false, help: "List all available executables.");
3967
}
4068

4169
void run() {
@@ -44,18 +72,22 @@ class DepsCommand extends PubCommand {
4472

4573
_buffer = new StringBuffer();
4674

47-
_buffer.writeln(_labelPackage(entrypoint.root));
48-
49-
switch (argResults["style"]) {
50-
case "compact":
51-
_outputCompact();
52-
break;
53-
case "list":
54-
_outputList();
55-
break;
56-
case "tree":
57-
_outputTree();
58-
break;
75+
if (argResults['executables']) {
76+
_outputExecutables();
77+
} else {
78+
_buffer.writeln(_labelPackage(entrypoint.root));
79+
80+
switch (argResults["style"]) {
81+
case "compact":
82+
_outputCompact();
83+
break;
84+
case "list":
85+
_outputList();
86+
break;
87+
case "tree":
88+
_outputTree();
89+
break;
90+
}
5991
}
6092

6193
log.message(_buffer);
@@ -231,4 +263,61 @@ class DepsCommand extends PubCommand {
231263
'was generated, please run "pub get" again.');
232264
return null;
233265
}
266+
267+
/// Outputs all executables reachable from [entrypoint].
268+
void _outputExecutables() {
269+
var packages = []
270+
..add(entrypoint.root)
271+
..addAll((_includeDev
272+
? entrypoint.root.immediateDependencies
273+
: _nonDevDependencies(entrypoint.root))
274+
.map((dep) => entrypoint.packageGraph.packages[dep.name]));
275+
276+
for (var package in packages) {
277+
var executables = _getExecutablesFor(package);
278+
if (executables.isNotEmpty) {
279+
_buffer.writeln(_formatExecutables(package.name, executables.toList()));
280+
}
281+
}
282+
}
283+
284+
/// Lists all Dart files in the `bin` directory of the [package].
285+
///
286+
/// Returns file names without extensions.
287+
List<String> _getExecutablesFor(Package package) => package
288+
.listFiles(beneath: 'bin', recursive: false)
289+
.where(_isDartExecutable)
290+
.map(p.basenameWithoutExtension);
291+
292+
/// Returns formatted string that lists [executables] for the [packageName].
293+
/// Examples:
294+
///
295+
/// _formatExecutables('foo', ['foo']) // -> 'foo'
296+
/// _formatExecutables('foo', ['bar']) // -> 'foo:bar'
297+
/// _formatExecutables('foo', ['bar', 'foo']) // -> 'foo: foo, bar'
298+
///
299+
/// Note the leading space before first executable and sorting order in the
300+
/// last example.
301+
String _formatExecutables(String packageName, List<String> executables) {
302+
if (executables.length == 1) {
303+
// If executable matches the package name omit the name of executable in
304+
// the output.
305+
return executables.first != packageName
306+
? '${packageName}:${log.bold(executables.first)}'
307+
: log.bold(executables.first);
308+
}
309+
310+
// Sort executables to make executable that matches the package name to be
311+
// the first in the list.
312+
executables.sort((e1, e2) {
313+
if (e1 == packageName)
314+
return -1;
315+
else if (e2 == packageName)
316+
return 1;
317+
else
318+
return e1.compareTo(e2);
319+
});
320+
321+
return '${packageName}: ${executables.map(log.bold).join(', ')}';
322+
}
234323
}

lib/src/command/run.dart

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,14 @@
44

55
import 'dart:async';
66

7-
import 'package:analyzer/analyzer.dart' as analyzer;
87
import 'package:barback/barback.dart';
98
import 'package:path/path.dart' as p;
109

1110
import '../command.dart';
12-
import '../dart.dart';
1311
import '../executable.dart';
1412
import '../io.dart';
15-
import '../log.dart' as log;
16-
import '../package.dart';
1713
import '../utils.dart';
1814

19-
/// Returns `true` if [path] looks like a Dart entrypoint.
20-
bool _isDartExecutable(String path) {
21-
if (p.extension(path) == '.dart') {
22-
try {
23-
var unit = analyzer.parseDartFile(path, parseFunctionBodies: false);
24-
return isEntrypoint(unit);
25-
} on analyzer.AnalyzerErrorGroup {
26-
return false;
27-
}
28-
}
29-
30-
return false;
31-
}
32-
3315
/// Handles the `run` pub command.
3416
class RunCommand extends PubCommand {
3517
String get name => "run";
@@ -40,20 +22,14 @@ class RunCommand extends PubCommand {
4022
RunCommand() {
4123
argParser.addFlag("checked",
4224
abbr: "c", help: "Enable runtime type checks and assertions.");
43-
argParser.addFlag('list',
44-
negatable: false, help: 'List all available executables.');
25+
4526
argParser.addOption("mode",
4627
help: 'Mode to run transformers in.\n'
4728
'(defaults to "release" for dependencies, "debug" for '
4829
'entrypoint)');
4930
}
5031

5132
Future run() async {
52-
if (argResults['list']) {
53-
_listExecutables();
54-
return;
55-
}
56-
5733
if (argResults.rest.isEmpty) {
5834
usageException("Must specify an executable to run.");
5935
}
@@ -94,61 +70,4 @@ class RunCommand extends PubCommand {
9470
checked: argResults['checked'], mode: mode);
9571
await flushThenExit(exitCode);
9672
}
97-
98-
/// Lists all executables reachable from [entrypoint].
99-
void _listExecutables() {
100-
var packages = []
101-
..add(entrypoint.root)
102-
..addAll(entrypoint.root.immediateDependencies
103-
.map((dep) => entrypoint.packageGraph.packages[dep.name]));
104-
105-
for (var package in packages) {
106-
var executables = _listExecutablesFor(package);
107-
if (executables.isNotEmpty) {
108-
log.message(_formatExecutables(package.name, executables.toList()));
109-
}
110-
}
111-
}
112-
113-
/// Lists all Dart files in the `bin` directory of the [package].
114-
///
115-
/// Returns file names without extensions.
116-
List<String> _listExecutablesFor(Package package) {
117-
return package
118-
.listFiles(beneath: 'bin', recursive: false)
119-
.where(_isDartExecutable)
120-
.map(p.basenameWithoutExtension);
121-
}
122-
123-
/// Returns formatted string that lists [executables] for the [packageName].
124-
/// Examples:
125-
///
126-
/// _formatExecutables('foo', ['foo']) // -> 'foo'
127-
/// _formatExecutables('foo', ['bar']) // -> 'foo:bar'
128-
/// _formatExecutables('foo', ['bar', 'foo']) // -> 'foo: foo, bar'
129-
///
130-
/// Note the leading space before first executable and sorting order in the
131-
/// last example.
132-
String _formatExecutables(String packageName, List<String> executables) {
133-
if (executables.length == 1) {
134-
// If executable matches the package name omit the name of executable in
135-
// the output.
136-
return executables.first != packageName
137-
? '${packageName}:${log.bold(executables.first)}'
138-
: log.bold(executables.first);
139-
} else {
140-
// Sort executables to make executable that matches the package name to be
141-
// the first in the list.
142-
executables.sort((e1, e2) {
143-
if (e1 == packageName)
144-
return -1;
145-
else if (e2 == packageName)
146-
return 1;
147-
else
148-
return e1.compareTo(e2);
149-
});
150-
151-
return '${packageName}: ${executables.map(log.bold).join(', ')}';
152-
}
153-
}
15473
}

0 commit comments

Comments
 (0)