Commit 61bfe5ce authored by Ian Fischer's avatar Ian Fischer

Unify process_wrapper and process to have the same api and logging styles.

Also add unchecked runSync wrapper and use it in places where command failures don’t matter.
parent 8cac55a4
......@@ -12,7 +12,7 @@ import 'package:logging/logging.dart';
import 'package:path/path.dart' as path;
import 'application_package.dart';
import 'process_wrapper.dart';
import 'process.dart';
final Logger _logging = new Logger('sky_tools.device');
......@@ -299,24 +299,20 @@ class AndroidDevice extends _Device {
bool stop(AndroidApk apk) {
// Turn off reverse port forwarding
try {
runCheckedSync([adbPath, 'reverse', '--remove', 'tcp:$_serverPort']);
} catch (e) {}
runSync([adbPath, 'reverse', '--remove', 'tcp:$_serverPort']);
// Stop the app
runCheckedSync([adbPath, 'shell', 'am', 'force-stop', apk.appPackageID]);
runSync([adbPath, 'shell', 'am', 'force-stop', apk.appPackageID]);
// Kill the server
try {
if (Platform.isMacOS) {
String pid = runCheckedSync(['lsof', '-i', ':$_serverPort', '-t']);
// Killing a pid with a shell command from within dart is hard,
// so use a library command, but it's still nice to give the
// equivalent command when doing verbose logging.
_logging.info('kill $pid');
Process.killPid(int.parse(pid));
} else {
runCheckedSync(['fuser', '-k', '$_serverPort/tcp']);
}
} catch (e) {}
if (Platform.isMacOS) {
String pid = runSync(['lsof', '-i', ':$_serverPort', '-t']);
// Killing a pid with a shell command from within dart is hard,
// so use a library command, but it's still nice to give the
// equivalent command when doing verbose logging.
_logging.info('kill $pid');
Process.killPid(int.parse(pid));
} else {
runSync(['fuser', '-k', '$_serverPort/tcp']);
}
return true;
}
......
......@@ -12,10 +12,12 @@ import 'package:logging/logging.dart';
final Logger _logging = new Logger('sky_tools.process');
// This runs the command and streams stdout/stderr from the child process to this process' stdout/stderr.
Future<int> runCommandAndStreamOutput(String command, List<String> args) async {
_logging.fine("Starting ${command} with args: ${args}");
Process proc = await Process.start(command, args);
/// This runs the command and streams stdout/stderr from the child process to
/// this process' stdout/stderr.
Future<int> runCommandAndStreamOutput(List<String> cmd) async {
_logging.info(cmd.join(' '));
Process proc =
await Process.start(cmd[0], cmd.getRange(1, cmd.length).toList());
proc.stdout.transform(UTF8.decoder).listen((data) {
stdout.write(data);
});
......@@ -24,3 +26,31 @@ Future<int> runCommandAndStreamOutput(String command, List<String> args) async {
});
return proc.exitCode;
}
/// Run cmd and return stdout.
/// Throws an error if cmd exits with a non-zero value.
String runCheckedSync(List<String> cmd) =>
_runWithLoggingSync(cmd, checked: true);
/// Run cmd and return stdout.
String runSync(List<String> cmd) => _runWithLoggingSync(cmd);
String _runWithLoggingSync(List<String> cmd, {bool checked: false}) {
_logging.info(cmd.join(' '));
ProcessResult results =
Process.runSync(cmd[0], cmd.getRange(1, cmd.length).toList());
if (results.exitCode != 0) {
String errorDescription = 'Error code ${results.exitCode} '
'returned when attempting to run command: ${cmd.join(' ')}';
_logging.fine(errorDescription);
if (results.stderr.length > 0) {
_logging.info('Errors logged: ${results.stderr.trim()}');
}
if (checked) {
throw errorDescription;
}
}
_logging.fine(results.stdout.trim());
return results.stdout;
}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library sky_tools.process_wrapper;
import 'dart:io';
import 'package:logging/logging.dart';
final Logger _logging = new Logger('sky_tools.process_wrapper');
String runCheckedSync(List<String> cmd) {
_logging.info(cmd.join(' '));
ProcessResult results =
Process.runSync(cmd[0], cmd.getRange(1, cmd.length).toList());
if (results.exitCode != 0) {
if (results.stderr.length > 0) {
_logging.info('Errors logged: ' + results.stderr);
}
throw 'Error code ' +
results.exitCode.toString() +
' returned when attempting to run command: ' +
cmd.join(' ');
}
_logging.fine(results.stdout.trim());
return results.stdout;
}
......@@ -45,7 +45,8 @@ class RunMojoCommand extends Command {
String appName = path.basename(appPath);
String appDir = path.dirname(appPath);
String buildFlag = mojoConfig == _MojoConfig.Debug ? '--debug' : '--release';
List<String> args = [
List<String> cmd = [
command,
'--android',
buildFlag,
'http://app/$appName',
......@@ -54,25 +55,26 @@ class RunMojoCommand extends Command {
'--url-mappings=mojo:sky_viewer=http://sky_viewer/sky_viewer.mojo',
];
if (_logging.level <= Level.INFO) {
args.add('--verbose');
cmd.add('--verbose');
if (_logging.level <= Level.FINE) {
args.add('--verbose');
cmd.add('--verbose');
}
}
args.addAll(additionalArgs);
return runCommandAndStreamOutput(command, args);
cmd.addAll(additionalArgs);
return runCommandAndStreamOutput(cmd);
}
Future<int> _runLinux(String mojoPath, _MojoConfig mojoConfig, String appPath, List<String> additionalArgs) async {
String viewerPath = await _makePathAbsolute(await ArtifactStore.getPath(Artifact.SkyViewerMojo));
String mojoBuildType = mojoConfig == _MojoConfig.Debug ? 'Debug' : 'Release';
String mojoShellPath = await _makePathAbsolute(path.join(mojoPath, 'out', mojoBuildType, 'mojo_shell'));
List<String> args = [
List<String> cmd = [
mojoShellPath,
'file://${appPath}',
'--url-mappings=mojo:sky_viewer=file://${viewerPath}'
];
args.addAll(additionalArgs);
return runCommandAndStreamOutput(mojoShellPath, args);
cmd.addAll(additionalArgs);
return runCommandAndStreamOutput(cmd);
}
@override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment