move CI tests into the main repo (#5758)

This commit is contained in:
Yegor
2016-09-14 13:22:53 -07:00
committed by GitHub
parent 4ec5144427
commit 1ba1562293
42 changed files with 2624 additions and 3 deletions

View File

@@ -0,0 +1,202 @@
// Copyright 2016 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.
import 'dart:async';
import 'dart:io';
import 'dart:math' as math;
import 'package:path/path.dart' as path;
import 'utils.dart';
typedef Future<Adb> AdbGetter();
/// Get an instance of [Adb].
///
/// See [realAdbGetter] for signature. This can be overwritten for testing.
AdbGetter adb = realAdbGetter;
Adb _currentDevice;
/// Picks a random Android device out of connected devices and sets it as
/// [_currentDevice].
Future<Null> pickNextDevice() async {
List<Adb> allDevices =
(await Adb.deviceIds).map((String id) => new Adb(deviceId: id)).toList();
if (allDevices.length == 0) throw 'No Android devices detected';
// TODO(yjbanov): filter out and warn about those with low battery level
_currentDevice = allDevices[new math.Random().nextInt(allDevices.length)];
}
Future<Adb> realAdbGetter() async {
if (_currentDevice == null) await pickNextDevice();
return _currentDevice;
}
/// Gets the ID of an unlocked device, unlocking it if necessary.
// TODO(yjbanov): abstract away iOS from Android.
Future<String> getUnlockedDeviceId({ bool ios: false }) async {
if (ios) {
// We currently do not have a way to lock/unlock iOS devices, or even to
// pick one out of many. So we pick the first random iPhone and assume it's
// already unlocked. For now we'll just keep them at minimum screen
// brightness so they don't drain battery too fast.
List<String> iosDeviceIds =
grep('UniqueDeviceID', from: await eval('ideviceinfo', <String>[]))
.map((String line) => line.split(' ').last)
.toList();
if (iosDeviceIds.isEmpty) throw 'No connected iOS devices found.';
return iosDeviceIds.first;
}
Adb device = await adb();
await device.unlock();
return device.deviceId;
}
/// Android Debug Bridge (`adb`) client that exposes a subset of functions
/// relevant to on-device testing.
class Adb {
Adb({ this.deviceId });
final String deviceId;
// Parses information about a device. Example:
//
// 015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper
static final RegExp _kDeviceRegex = new RegExp(r'^(\S+)\s+(\S+)(.*)');
/// Reports connection health for every device.
static Future<Map<String, HealthCheckResult>> checkDevices() async {
Map<String, HealthCheckResult> results = <String, HealthCheckResult>{};
for (String deviceId in await deviceIds) {
try {
Adb device = new Adb(deviceId: deviceId);
// Just a smoke test that we can read wakefulness state
// TODO(yjbanov): also check battery level
await device._getWakefulness();
results['android-device-$deviceId'] = new HealthCheckResult.success();
} catch (e, s) {
results['android-device-$deviceId'] = new HealthCheckResult.error(e, s);
}
}
return results;
}
/// Kills the `adb` server causing it to start a new instance upon next
/// command.
///
/// Restarting `adb` helps with keeping device connections alive. When `adb`
/// runs non-stop for too long it loses connections to devices.
static Future<Null> restart() async {
await exec(adbPath, <String>['kill-server'], canFail: false);
}
/// List of device IDs visible to `adb`.
static Future<List<String>> get deviceIds async {
List<String> output =
(await eval(adbPath, <String>['devices', '-l'], canFail: false))
.trim()
.split('\n');
List<String> results = <String>[];
for (String line in output) {
// Skip lines like: * daemon started successfully *
if (line.startsWith('* daemon ')) continue;
if (line.startsWith('List of devices')) continue;
if (_kDeviceRegex.hasMatch(line)) {
Match match = _kDeviceRegex.firstMatch(line);
String deviceID = match[1];
String deviceState = match[2];
if (!const <String>['unauthorized', 'offline'].contains(deviceState)) {
results.add(deviceID);
}
} else {
throw 'Failed to parse device from adb output: $line';
}
}
return results;
}
/// Whether the device is awake.
Future<bool> isAwake() async {
return await _getWakefulness() == 'Awake';
}
/// Whether the device is asleep.
Future<bool> isAsleep() async {
return await _getWakefulness() == 'Asleep';
}
/// Wake up the device if it is not awake using [togglePower].
Future<Null> wakeUp() async {
if (!(await isAwake())) await togglePower();
}
/// Send the device to sleep mode if it is not asleep using [togglePower].
Future<Null> sendToSleep() async {
if (!(await isAsleep())) await togglePower();
}
/// Sends `KEYCODE_POWER` (26), which causes the device to toggle its mode
/// between awake and asleep.
Future<Null> togglePower() async {
await shellExec('input', const <String>['keyevent', '26']);
}
/// Unlocks the device by sending `KEYCODE_MENU` (82).
///
/// This only works when the device doesn't have a secure unlock pattern.
Future<Null> unlock() async {
await wakeUp();
await shellExec('input', const <String>['keyevent', '82']);
}
/// Retrieves device's wakefulness state.
///
/// See: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/PowerManagerInternal.java
Future<String> _getWakefulness() async {
String powerInfo = await shellEval('dumpsys', <String>['power']);
String wakefulness =
grep('mWakefulness=', from: powerInfo).single.split('=')[1].trim();
return wakefulness;
}
/// Executes [command] on `adb shell` and returns its exit code.
Future<Null> shellExec(String command, List<String> arguments,
{ Map<String, String> env }) async {
await exec(adbPath, <String>['shell', command]..addAll(arguments),
env: env, canFail: false);
}
/// Executes [command] on `adb shell` and returns its standard output as a [String].
Future<String> shellEval(String command, List<String> arguments,
{ Map<String, String> env }) {
return eval(adbPath, <String>['shell', command]..addAll(arguments),
env: env, canFail: false);
}
}
/// Path to the `adb` executable.
String get adbPath {
String androidHome = Platform.environment['ANDROID_HOME'];
if (androidHome == null)
throw 'ANDROID_HOME environment variable missing. This variable must '
'point to the Android SDK directory containing platform-tools.';
File adbPath = file(path.join(androidHome, 'platform-tools/adb'));
if (!adbPath.existsSync()) throw 'adb not found at: $adbPath';
return adbPath.absolute.path;
}

View File

@@ -0,0 +1,62 @@
// Copyright (c) 2016 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.
import 'dart:async';
import 'framework.dart';
/// A benchmark harness used to run a benchmark multiple times and report the
/// best result.
abstract class Benchmark {
Benchmark(this.name);
final String name;
TaskResult bestResult;
Future<Null> init() => new Future<Null>.value();
Future<num> run();
TaskResult get lastResult;
@override
String toString() => name;
}
/// Runs a [benchmark] [iterations] times and reports the best result.
///
/// Use [warmUpBenchmark] to discard cold performance results.
Future<num> runBenchmark(Benchmark benchmark, {
int iterations: 1,
bool warmUpBenchmark: false
}) async {
await benchmark.init();
List<num> allRuns = <num>[];
num minValue;
if (warmUpBenchmark)
await benchmark.run();
while (iterations > 0) {
iterations--;
print('');
try {
num result = await benchmark.run();
allRuns.add(result);
if (minValue == null || result < minValue) {
benchmark.bestResult = benchmark.lastResult;
minValue = result;
}
} catch (error) {
print('benchmark failed with error: $error');
}
}
return minValue;
}

View File

@@ -0,0 +1,217 @@
// Copyright 2016 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.
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'dart:isolate';
import 'package:logging/logging.dart';
import 'utils.dart';
/// Maximum amount of time a single task is allowed to take to run.
///
/// If exceeded the task is considered to have failed.
const Duration taskTimeout = const Duration(minutes: 10);
/// Represents a unit of work performed in the CI environment that can
/// succeed, fail and be retried independently of others.
typedef Future<TaskResult> TaskFunction();
bool _isTaskRegistered = false;
/// Registers a [task] to run, returns the result when it is complete.
///
/// Note, the task does not run immediately but waits for the request via the
/// VM service protocol to run it.
///
/// It is ok for a [task] to perform many things. However, only one task can be
/// registered per Dart VM.
Future<TaskResult> task(TaskFunction task) {
if (_isTaskRegistered)
throw new StateError('A task is already registered');
_isTaskRegistered = true;
// TODO: allow overriding logging.
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
_TaskRunner runner = new _TaskRunner(task);
runner.keepVmAliveUntilTaskRunRequested();
return runner.whenDone;
}
class _TaskRunner {
static final Logger logger = new Logger('TaskRunner');
final TaskFunction task;
// TODO: workaround for https://github.com/dart-lang/sdk/issues/23797
RawReceivePort _keepAlivePort;
Timer _startTaskTimeout;
bool _taskStarted = false;
final Completer<TaskResult> _completer = new Completer<TaskResult>();
_TaskRunner(this.task) {
registerExtension('ext.cocoonRunTask',
(String method, Map<String, String> parameters) async {
TaskResult result = await run();
return new ServiceExtensionResponse.result(JSON.encode(result.toJson()));
});
registerExtension('ext.cocoonRunnerReady',
(String method, Map<String, String> parameters) async {
return new ServiceExtensionResponse.result('"ready"');
});
}
/// Signals that this task runner finished running the task.
Future<TaskResult> get whenDone => _completer.future;
Future<TaskResult> run() async {
try {
_taskStarted = true;
TaskResult result = await _performTask().timeout(taskTimeout);
_completer.complete(result);
return result;
} on TimeoutException catch (_) {
return new TaskResult.failure('Task timed out after $taskTimeout');
} finally {
forceQuitRunningProcesses();
_closeKeepAlivePort();
}
}
/// Causes the Dart VM to stay alive until a request to run the task is
/// received via the VM service protocol.
void keepVmAliveUntilTaskRunRequested() {
if (_taskStarted)
throw new StateError('Task already started.');
// Merely creating this port object will cause the VM to stay alive and keep
// the VM service server running until the port is disposed of.
_keepAlivePort = new RawReceivePort();
// Timeout if nothing bothers to connect and ask us to run the task.
const Duration taskStartTimeout = const Duration(seconds: 10);
_startTaskTimeout = new Timer(taskStartTimeout, () {
if (!_taskStarted) {
logger.severe('Task did not start in $taskStartTimeout.');
_closeKeepAlivePort();
exitCode = 1;
}
});
}
/// Disables the keep-alive port, allowing the VM to exit.
void _closeKeepAlivePort() {
_startTaskTimeout?.cancel();
_keepAlivePort?.close();
}
Future<TaskResult> _performTask() async {
try {
return await task();
} catch (taskError, taskErrorStack) {
String message = 'Task failed: $taskError';
if (taskErrorStack != null) {
message += '\n\n$taskErrorStack';
}
return new TaskResult.failure(message);
}
}
}
/// A result of running a single task.
class TaskResult {
/// Constructs a successful result.
TaskResult.success(this.data, {this.benchmarkScoreKeys: const <String>[]})
: this.succeeded = true,
this.message = 'success' {
const JsonEncoder prettyJson = const JsonEncoder.withIndent(' ');
if (benchmarkScoreKeys != null) {
for (String key in benchmarkScoreKeys) {
if (!data.containsKey(key)) {
throw 'Invalid Golem score key "$key". It does not exist in task '
'result data ${prettyJson.convert(data)}';
} else if (data[key] is! num) {
throw 'Invalid Golem score for key "$key". It is expected to be a num '
'but was ${data[key].runtimeType}: ${prettyJson.convert(data[key])}';
}
}
}
}
/// Constructs a successful result using JSON data stored in a file.
factory TaskResult.successFromFile(File file,
{List<String> benchmarkScoreKeys}) {
return new TaskResult.success(JSON.decode(file.readAsStringSync()),
benchmarkScoreKeys: benchmarkScoreKeys);
}
/// Constructs an unsuccessful result.
TaskResult.failure(this.message)
: this.succeeded = false,
this.data = null,
this.benchmarkScoreKeys = const <String>[];
/// Whether the task succeeded.
final bool succeeded;
/// Task-specific JSON data
final Map<String, dynamic> data;
/// Keys in [data] that store scores that will be submitted to Golem.
///
/// Each key is also part of a benchmark's name tracked by Golem.
/// A benchmark name is computed by combining [Task.name] with a key
/// separated by a dot. For example, if a task's name is
/// `"complex_layout__start_up"` and score key is
/// `"engineEnterTimestampMicros"`, the score will be submitted to Golem under
/// `"complex_layout__start_up.engineEnterTimestampMicros"`.
///
/// This convention reduces the amount of configuration that needs to be done
/// to submit benchmark scores to Golem.
final List<String> benchmarkScoreKeys;
/// Whether the task failed.
bool get failed => !succeeded;
/// Explains the result in a human-readable format.
final String message;
/// Serializes this task result to JSON format.
///
/// The JSON format is as follows:
///
/// {
/// "success": true|false,
/// "data": arbitrary JSON data valid only for successful results,
/// "benchmarkScoreKeys": [
/// contains keys into "data" that represent benchmarks scores, which
/// can be uploaded, for example. to golem, valid only for successful
/// results
/// ],
/// "reason": failure reason string valid only for unsuccessful results
/// }
Map<String, dynamic> toJson() {
Map<String, dynamic> json = <String, dynamic>{
'success': succeeded,
};
if (succeeded) {
json['data'] = data;
json['benchmarkScoreKeys'] = benchmarkScoreKeys;
} else {
json['reason'] = message;
}
return json;
}
}

View File

@@ -0,0 +1,127 @@
// Copyright 2016 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.
import 'package:meta/meta.dart';
import 'package:yaml/yaml.dart';
import 'utils.dart';
/// Loads manifest data from `manifest.yaml` file or from [yaml], if present.
Manifest loadTaskManifest([ String yaml ]) {
dynamic manifestYaml = yaml == null
? loadYaml(file('manifest.yaml').readAsStringSync())
: loadYamlNode(yaml);
_checkType(manifestYaml is Map, manifestYaml, 'Manifest', 'dictionary');
return _validateAndParseManifest(manifestYaml);
}
/// Contains CI task information.
class Manifest {
Manifest._(this.tasks);
/// CI tasks.
final List<ManifestTask> tasks;
}
/// A CI task.
class ManifestTask {
ManifestTask._({
@required this.name,
@required this.description,
@required this.stage,
@required this.requiredAgentCapabilities,
}) {
String taskName = 'task "$name"';
_checkIsNotBlank(name, 'Task name', taskName);
_checkIsNotBlank(description, 'Task description', taskName);
_checkIsNotBlank(stage, 'Task stage', taskName);
_checkIsNotBlank(requiredAgentCapabilities, 'requiredAgentCapabilities', taskName);
}
/// Task name as it appears on the dashboard.
final String name;
/// A human-readable description of the task.
final String description;
/// The stage this task should run in.
final String stage;
/// Capabilities required of the build agent to be able to perform this task.
final List<String> requiredAgentCapabilities;
}
/// Thrown when the manifest YAML is not valid.
class ManifestError extends Error {
ManifestError(this.message);
final String message;
@override
String toString() => '$ManifestError: $message';
}
// There's no good YAML validator, at least not for Dart, so we validate
// manually. It's not too much code and produces good error messages.
Manifest _validateAndParseManifest(Map<String, dynamic> manifestYaml) {
_checkKeys(manifestYaml, 'manifest', const <String>['tasks']);
return new Manifest._(_validateAndParseTasks(manifestYaml['tasks']));
}
List<ManifestTask> _validateAndParseTasks(dynamic tasksYaml) {
_checkType(tasksYaml is Map, tasksYaml, 'Value of "tasks"', 'dictionary');
return tasksYaml.keys.map((dynamic taskName) => _validateAndParseTask(taskName, tasksYaml[taskName])).toList();
}
ManifestTask _validateAndParseTask(dynamic taskName, dynamic taskYaml) {
_checkType(taskName is String, taskName, 'Task name', 'string');
_checkType(taskYaml is Map, taskYaml, 'Value of task "$taskName"', 'dictionary');
_checkKeys(taskYaml, 'Value of task "$taskName"', const <String>[
'description',
'stage',
'required_agent_capabilities',
]);
List<String> capabilities = _validateAndParseCapabilities(taskName, taskYaml['required_agent_capabilities']);
return new ManifestTask._(
name: taskName,
description: taskYaml['description'],
stage: taskYaml['stage'],
requiredAgentCapabilities: capabilities,
);
}
List<String> _validateAndParseCapabilities(String taskName, dynamic capabilitiesYaml) {
_checkType(capabilitiesYaml is List, capabilitiesYaml, 'required_agent_capabilities', 'list');
for (int i = 0; i < capabilitiesYaml.length; i++) {
dynamic capability = capabilitiesYaml[i];
_checkType(capability is String, capability, 'required_agent_capabilities[$i]', 'string');
}
return capabilitiesYaml;
}
void _checkType(bool isValid, dynamic value, String variableName, String typeName) {
if (!isValid) {
throw new ManifestError(
'$variableName must be a $typeName but was ${value.runtimeType}: $value',
);
}
}
void _checkIsNotBlank(dynamic value, String variableName, String ownerName) {
if (value == null || value.isEmpty) {
throw new ManifestError('$variableName must not be empty in $ownerName.');
}
}
void _checkKeys(Map<String, dynamic> map, String variableName, List<String> allowedKeys) {
for (String key in map.keys) {
if (!allowedKeys.contains(key)) {
throw new ManifestError(
'Unrecognized property "$key" in $variableName. '
'Allowed properties: ${allowedKeys.join(', ')}');
}
}
}

View File

@@ -0,0 +1,129 @@
// Copyright 2016 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.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:vm_service_client/vm_service_client.dart';
import 'package:flutter_devicelab/framework/utils.dart';
/// Slightly longer than task timeout that gives the task runner a chance to
/// clean-up before forcefully quitting it.
const Duration taskTimeoutWithGracePeriod = const Duration(minutes: 11);
/// Runs a task in a separate Dart VM and collects the result using the VM
/// service protocol.
///
/// [taskName] is the name of the task. The corresponding task executable is
/// expected to be found under `bin/tasks`.
Future<Map<String, dynamic>> runTask(String taskName) async {
String taskExecutable = 'bin/tasks/$taskName.dart';
if (!file(taskExecutable).existsSync())
throw 'Executable Dart file not found: $taskExecutable';
int vmServicePort = await _findAvailablePort();
Process runner = await startProcess(dartBin, <String>[
'--enable-vm-service=$vmServicePort',
'--no-pause-isolates-on-exit',
taskExecutable,
]);
bool runnerFinished = false;
runner.exitCode.then((_) {
runnerFinished = true;
});
StreamSubscription<String> stdoutSub = runner.stdout
.transform(new Utf8Decoder())
.transform(new LineSplitter())
.listen((String line) {
stdout.writeln('[$taskName] [STDOUT] $line');
});
StreamSubscription<String> stderrSub = runner.stderr
.transform(new Utf8Decoder())
.transform(new LineSplitter())
.listen((String line) {
stderr.writeln('[$taskName] [STDERR] $line');
});
String waitingFor = 'connection';
try {
VMIsolate isolate = await _connectToRunnerIsolate(vmServicePort);
waitingFor = 'task completion';
Map<String, dynamic> taskResult =
await isolate.invokeExtension('ext.cocoonRunTask').timeout(taskTimeoutWithGracePeriod);
waitingFor = 'task process to exit';
await runner.exitCode.timeout(const Duration(seconds: 1));
return taskResult;
} on TimeoutException catch (timeout) {
runner.kill(ProcessSignal.SIGINT);
return <String, dynamic>{
'success': false,
'reason': 'Timeout waiting for $waitingFor: ${timeout.message}',
};
} finally {
if (!runnerFinished)
runner.kill(ProcessSignal.SIGKILL);
await stdoutSub.cancel();
await stderrSub.cancel();
}
}
Future<VMIsolate> _connectToRunnerIsolate(int vmServicePort) async {
String url = 'ws://localhost:$vmServicePort/ws';
DateTime started = new DateTime.now();
// TODO(yjbanov): due to lack of imagination at the moment the handshake with
// the task process is very rudimentary and requires this small
// delay to let the task process open up the VM service port.
// Otherwise we almost always hit the non-ready case first and
// wait a whole 1 second, which is annoying.
await new Future<Null>.delayed(const Duration(milliseconds: 100));
while (true) {
try {
// Make sure VM server is up by successfully opening and closing a socket.
await (await WebSocket.connect(url)).close();
// Look up the isolate.
VMServiceClient client = new VMServiceClient.connect(url);
VM vm = await client.getVM();
VMIsolate isolate = vm.isolates.single;
String response = await isolate.invokeExtension('ext.cocoonRunnerReady');
if (response != 'ready') throw 'not ready yet';
return isolate;
} catch (error) {
const Duration connectionTimeout = const Duration(seconds: 2);
if (new DateTime.now().difference(started) > connectionTimeout) {
throw new TimeoutException(
'Failed to connect to the task runner process',
connectionTimeout,
);
}
print('VM service not ready yet: $error');
const Duration pauseBetweenRetries = const Duration(milliseconds: 200);
print('Will retry in $pauseBetweenRetries.');
await new Future<Null>.delayed(pauseBetweenRetries);
}
}
}
Future<int> _findAvailablePort() async {
int port = 20000;
while (true) {
try {
ServerSocket socket =
await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, port);
await socket.close();
return port;
} catch (_) {
port++;
}
}
}

View File

@@ -0,0 +1,412 @@
// Copyright (c) 2016 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.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:stack_trace/stack_trace.dart';
/// Virtual current working directory, which affect functions, such as [exec].
String cwd = Directory.current.path;
List<ProcessInfo> _runningProcesses = <ProcessInfo>[];
class ProcessInfo {
ProcessInfo(this.command, this.process);
final DateTime startTime = new DateTime.now();
final String command;
final Process process;
@override
String toString() {
return '''
command : $command
started : $startTime
pid : ${process.pid}
'''
.trim();
}
}
/// Result of a health check for a specific parameter.
class HealthCheckResult {
HealthCheckResult.success([this.details]) : succeeded = true;
HealthCheckResult.failure(this.details) : succeeded = false;
HealthCheckResult.error(dynamic error, dynamic stackTrace)
: succeeded = false,
details = 'ERROR: $error${'\n$stackTrace' ?? ''}';
final bool succeeded;
final String details;
@override
String toString() {
StringBuffer buf = new StringBuffer(succeeded ? 'succeeded' : 'failed');
if (details != null && details.trim().isNotEmpty) {
buf.writeln();
// Indent details by 4 spaces
for (String line in details.trim().split('\n')) {
buf.writeln(' $line');
}
}
return '$buf';
}
}
class BuildFailedError extends Error {
BuildFailedError(this.message);
final String message;
@override
String toString() => message;
}
void fail(String message) {
throw new BuildFailedError(message);
}
void rm(FileSystemEntity entity) {
if (entity.existsSync())
entity.deleteSync();
}
/// Remove recursively.
void rmTree(FileSystemEntity entity) {
if (entity.existsSync())
entity.deleteSync(recursive: true);
}
List<FileSystemEntity> ls(Directory directory) => directory.listSync();
Directory dir(String path) => new Directory(path);
File file(String path) => new File(path);
void copy(File sourceFile, Directory targetDirectory, {String name}) {
File target = file(
path.join(targetDirectory.path, name ?? path.basename(sourceFile.path)));
target.writeAsBytesSync(sourceFile.readAsBytesSync());
}
FileSystemEntity move(FileSystemEntity whatToMove,
{Directory to, String name}) {
return whatToMove
.renameSync(path.join(to.path, name ?? path.basename(whatToMove.path)));
}
/// Equivalent of `mkdir directory`.
void mkdir(Directory directory) {
directory.createSync();
}
/// Equivalent of `mkdir -p directory`.
void mkdirs(Directory directory) {
directory.createSync(recursive: true);
}
bool exists(FileSystemEntity entity) => entity.existsSync();
void section(String title) {
print('\n••• $title •••');
}
Future<String> getDartVersion() async {
// The Dart VM returns the version text to stderr.
ProcessResult result = Process.runSync(dartBin, <String>['--version']);
String version = result.stderr.trim();
// Convert:
// Dart VM version: 1.17.0-dev.2.0 (Tue May 3 12:14:52 2016) on "macos_x64"
// to:
// 1.17.0-dev.2.0
if (version.indexOf('(') != -1)
version = version.substring(0, version.indexOf('(')).trim();
if (version.indexOf(':') != -1)
version = version.substring(version.indexOf(':') + 1).trim();
return version.replaceAll('"', "'");
}
Future<String> getCurrentFlutterRepoCommit() {
if (!dir('${flutterDirectory.path}/.git').existsSync()) {
return null;
}
return inDirectory(flutterDirectory, () {
return eval('git', <String>['rev-parse', 'HEAD']);
});
}
Future<DateTime> getFlutterRepoCommitTimestamp(String commit) {
// git show -s --format=%at 4b546df7f0b3858aaaa56c4079e5be1ba91fbb65
return inDirectory(flutterDirectory, () async {
String unixTimestamp = await eval('git', <String>[
'show',
'-s',
'--format=%at',
commit,
]);
int secondsSinceEpoch = int.parse(unixTimestamp);
return new DateTime.fromMillisecondsSinceEpoch(secondsSinceEpoch * 1000);
});
}
Future<Process> startProcess(String executable, List<String> arguments,
{Map<String, String> env}) async {
String command = '$executable ${arguments?.join(" ") ?? ""}';
print('Executing: $command');
Process proc = await Process.start(executable, arguments,
environment: env, workingDirectory: cwd);
ProcessInfo procInfo = new ProcessInfo(command, proc);
_runningProcesses.add(procInfo);
proc.exitCode.then((_) {
_runningProcesses.remove(procInfo);
});
return proc;
}
Future<Null> forceQuitRunningProcesses() async {
if (_runningProcesses.isEmpty)
return;
// Give normally quitting processes a chance to report their exit code.
await new Future<Null>.delayed(const Duration(seconds: 1));
// Whatever's left, kill it.
for (ProcessInfo p in _runningProcesses) {
print('Force quitting process:\n$p');
if (!p.process.kill()) {
print('Failed to force quit process');
}
}
_runningProcesses.clear();
}
/// Executes a command and returns its exit code.
Future<int> exec(String executable, List<String> arguments,
{Map<String, String> env, bool canFail: false}) async {
Process proc = await startProcess(executable, arguments, env: env);
proc.stdout
.transform(UTF8.decoder)
.transform(const LineSplitter())
.listen(print);
proc.stderr
.transform(UTF8.decoder)
.transform(const LineSplitter())
.listen(stderr.writeln);
int exitCode = await proc.exitCode;
if (exitCode != 0 && !canFail)
fail('Executable failed with exit code $exitCode.');
return exitCode;
}
/// Executes a command and returns its standard output as a String.
///
/// Standard error is redirected to the current process' standard error stream.
Future<String> eval(String executable, List<String> arguments,
{Map<String, String> env, bool canFail: false}) async {
Process proc = await startProcess(executable, arguments, env: env);
proc.stderr.listen((List<int> data) {
stderr.add(data);
});
String output = await UTF8.decodeStream(proc.stdout);
int exitCode = await proc.exitCode;
if (exitCode != 0 && !canFail)
fail('Executable failed with exit code $exitCode.');
return output.trimRight();
}
Future<int> flutter(String command,
{List<String> options: const <String>[], bool canFail: false}) {
List<String> args = <String>[command]..addAll(options);
return exec(path.join(flutterDirectory.path, 'bin', 'flutter'), args,
canFail: canFail);
}
String get dartBin =>
path.join(flutterDirectory.path, 'bin', 'cache', 'dart-sdk', 'bin', 'dart');
Future<int> dart(List<String> args) => exec(dartBin, args);
Future<int> pub(String command) {
return exec(
path.join(flutterDirectory.path, 'bin', 'cache', 'dart-sdk', 'bin', 'pub'),
<String>[command]);
}
Future<dynamic> inDirectory(dynamic directory, Future<dynamic> action()) async {
String previousCwd = cwd;
try {
cd(directory);
return await action();
} finally {
cd(previousCwd);
}
}
void cd(dynamic directory) {
Directory d;
if (directory is String) {
cwd = directory;
d = dir(directory);
} else if (directory is Directory) {
cwd = directory.path;
d = directory;
} else {
throw 'Unsupported type ${directory.runtimeType} of $directory';
}
if (!d.existsSync())
throw 'Cannot cd into directory that does not exist: $directory';
}
Directory get flutterDirectory => dir('../..').absolute;
String requireEnvVar(String name) {
String value = Platform.environment[name];
if (value == null) fail('$name environment variable is missing. Quitting.');
return value;
}
dynamic/*=T*/ requireConfigProperty/*<T>*/(
Map<String, dynamic/*<T>*/ > map, String propertyName) {
if (!map.containsKey(propertyName))
fail('Configuration property not found: $propertyName');
return map[propertyName];
}
String jsonEncode(dynamic data) {
return new JsonEncoder.withIndent(' ').convert(data) + '\n';
}
Future<Null> getFlutter(String revision) async {
section('Get Flutter!');
if (exists(flutterDirectory)) {
rmTree(flutterDirectory);
}
await inDirectory(flutterDirectory.parent, () async {
await exec('git', <String>['clone', 'https://github.com/flutter/flutter.git']);
});
await inDirectory(flutterDirectory, () async {
await exec('git', <String>['checkout', revision]);
});
await flutter('config', options: <String>['--no-analytics']);
section('flutter doctor');
await flutter('doctor');
section('flutter update-packages');
await flutter('update-packages');
}
void checkNotNull(Object o1,
[Object o2 = 1,
Object o3 = 1,
Object o4 = 1,
Object o5 = 1,
Object o6 = 1,
Object o7 = 1,
Object o8 = 1,
Object o9 = 1,
Object o10 = 1]) {
if (o1 == null)
throw 'o1 is null';
if (o2 == null)
throw 'o2 is null';
if (o3 == null)
throw 'o3 is null';
if (o4 == null)
throw 'o4 is null';
if (o5 == null)
throw 'o5 is null';
if (o6 == null)
throw 'o6 is null';
if (o7 == null)
throw 'o7 is null';
if (o8 == null)
throw 'o8 is null';
if (o9 == null)
throw 'o9 is null';
if (o10 == null)
throw 'o10 is null';
}
/// Add benchmark values to a JSON results file.
///
/// If the file contains information about how long the benchmark took to run
/// (a `time` field), then return that info.
// TODO(yjbanov): move this data to __metadata__
num addBuildInfo(File jsonFile,
{num expected, String sdk, String commit, DateTime timestamp}) {
Map<String, dynamic> json;
if (jsonFile.existsSync())
json = JSON.decode(jsonFile.readAsStringSync());
else
json = <String, dynamic>{};
if (expected != null)
json['expected'] = expected;
if (sdk != null)
json['sdk'] = sdk;
if (commit != null)
json['commit'] = commit;
if (timestamp != null)
json['timestamp'] = timestamp.millisecondsSinceEpoch;
jsonFile.writeAsStringSync(jsonEncode(json));
// Return the elapsed time of the benchmark (if any).
return json['time'];
}
/// Splits [from] into lines and selects those that contain [pattern].
Iterable<String> grep(Pattern pattern, {@required String from}) {
return from.split('\n').where((String line) {
return line.contains(pattern);
});
}
/// Captures asynchronous stack traces thrown by [callback].
///
/// This is a convenience wrapper around [Chain] optimized for use with
/// `async`/`await`.
///
/// Example:
///
/// try {
/// await captureAsyncStacks(() { /* async things */ });
/// } catch (error, chain) {
///
/// }
Future<Null> runAndCaptureAsyncStacks(Future<Null> callback()) {
Completer<Null> completer = new Completer<Null>();
Chain.capture(() async {
await callback();
completer.complete();
}, onError: (dynamic error, Chain chain) async {
completer.completeError(error, chain);
});
return completer.future;
}