-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathfunction_tool.dart
More file actions
50 lines (43 loc) · 1.67 KB
/
Copy pathfunction_tool.dart
File metadata and controls
50 lines (43 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import 'dart:async';
import 'package:args/args.dart';
import 'package:dart_dev/src/utils/assert_no_args_after_separator.dart';
import 'package:io/io.dart';
import 'package:logging/logging.dart';
import '../dart_dev_tool.dart';
import '../utils/assert_no_positional_args_nor_args_after_separator.dart';
/// A utility class designed to make it simple to create a [DevTool] from just a
/// Dart function.
///
/// Use [DevTool.fromFunction] to create [FunctionTool] instances.
class FunctionTool extends DevTool {
FunctionTool(FutureOr<int> Function(DevToolExecutionContext context) function,
{ArgParser argParser})
: _argParser = argParser,
_function = function;
final FutureOr<int> Function(DevToolExecutionContext context) _function;
// ---------------------------------------------------------------------------
// DevTool Overrides
// ---------------------------------------------------------------------------
@override
ArgParser get argParser => _argParser;
final ArgParser _argParser;
@override
FutureOr<int> run([DevToolExecutionContext context]) async {
context ??= DevToolExecutionContext();
if (context.argResults != null) {
if (argParser == null) {
assertNoPositionalArgsNorArgsAfterSeparator(
context.argResults, context.usageException,
commandName: context.commandName);
}
}
final exitCode = await _function(context);
if (exitCode != null) {
return exitCode;
}
Logger('DartFunctionTool').warning(
'${context.commandName != null ? 'The ${context.commandName}' : 'This'}'
' command did not return an exit code.');
return ExitCode.software.code;
}
}