runDpk function
Runs dpk with arguments and returns the exit code.
dpkCommand is the command line that starts this dpk. When given, dpk
in scripts runs that same dpk. Tests pass a buffered console, a fake
processRunner, and a workingDirectory to run dpk in-process.
Implementation
Future<int> runDpk(
List<String> arguments, {
Console? console,
ProcessRunner? processRunner,
String? workingDirectory,
Map<String, String>? environment,
List<String>? dpkCommand,
}) async {
final Invocation invocation;
try {
invocation = Invocation.parse(arguments);
} on UsageException catch (e) {
(console ?? Console.stdio()).err.writeln(e);
return 64;
}
final output = console ?? Console.stdio(color: invocation.color);
output
..verbose = invocation.verbose
..quiet = invocation.quiet;
if (invocation.version && invocation.command == null) {
output.info(renderVersionOutput());
return 0;
}
final cwd = workingDirectory ?? Directory.current.path;
try {
final start = invocation.directory == null
? cwd
: _resolve(cwd, invocation.directory!);
final project = invocation.needsProject
? Project.load(start, cacheDirectoryOverride: invocation.cacheDirectory)
: _tryLoad(start, invocation);
for (final warning in project?.warnings ?? const []) {
output.warning(warning.toString());
}
final context = DpkContext(
invocation: invocation,
console: output,
processRunner: processRunner ?? SystemProcessRunner(output),
workingDirectory: cwd,
environment: environment ?? Platform.environment,
project: project,
dpkCommand: dpkCommand,
);
return await DpkCommandRunner(context).run(invocation.runnerArguments) ?? 0;
} on UsageException catch (e) {
output.err.writeln(e);
return 64;
} on DpkException catch (e) {
output.error(e.message);
return e.exitCode;
} on ProjectException catch (e) {
output.error(e.message);
return 1;
} on ConfigException catch (e) {
output.error(e.toString());
return 1;
} on RunPlanException catch (e) {
output.error(e.message);
return 1;
}
}