introduce globals scoped to the app's context

This commit is contained in:
Devon Carew
2016-02-06 19:19:50 -08:00
parent b2c710dd8d
commit 2dbceafbc6
42 changed files with 315 additions and 178 deletions

View File

@@ -3,9 +3,8 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
final AppContext _defaultContext = new _DefaultAppContext();
final AppContext _defaultContext = new AppContext();
/// A singleton for application functionality. This singleton can be different
/// on a per-Zone basis.
@@ -14,57 +13,42 @@ AppContext get context {
return currentContext == null ? _defaultContext : currentContext;
}
/// Display an error level message to the user. Commands should use this if they
/// fail in some way.
void printError(String message, [StackTrace stackTrace]) => context.printError(message, stackTrace);
class AppContext {
Map<Type, dynamic> _instances = <Type, dynamic>{};
/// Display normal output of the command. This should be used for things like
/// progress messages, success messages, or just normal command output.
void printStatus(String message) => context.printStatus(message);
dynamic getVariable(Type type) {
if (_instances.containsKey(type))
return _instances[type];
/// Use this for verbose tracing output. Users can turn this output on in order
/// to help diagnose issues with the toolchain or with their setup.
void printTrace(String message) => context.printTrace(message);
abstract class AppContext {
bool get verbose;
set verbose(bool value);
void printError(String message, [StackTrace stackTrace]);
void printStatus(String message);
void printTrace(String message);
}
class _DefaultAppContext implements AppContext {
DateTime _startTime = new DateTime.now();
bool _verbose = false;
bool get verbose => _verbose;
set verbose(bool value) {
_verbose = value;
AppContext parent = _calcParent(Zone.current);
return parent?.getVariable(type);
}
void printError(String message, [StackTrace stackTrace]) {
stderr.writeln(_prefix + message);
if (stackTrace != null)
stderr.writeln(stackTrace);
void setVariable(Type type, dynamic instance) {
_instances[type] = instance;
}
void printStatus(String message) {
print(_prefix + message);
dynamic operator[](Type type) => getVariable(type);
void operator[]=(Type type, dynamic instance) => setVariable(type, instance);
AppContext _calcParent(Zone zone) {
if (this == _defaultContext)
return null;
Zone parentZone = zone.parent;
if (parentZone == null)
return _defaultContext;
AppContext deps = parentZone['context'];
if (deps == this) {
return _calcParent(parentZone);
} else {
return deps != null ? deps : _defaultContext;
}
}
void printTrace(String message) {
if (_verbose)
print('$_prefix- $message');
}
String get _prefix {
if (!_verbose)
return '';
Duration elapsed = new DateTime.now().difference(_startTime);
return '[${elapsed.inMilliseconds.toString().padLeft(4)} ms] ';
dynamic runInZone(dynamic method()) {
return runZoned(method, zoneValues: {'context': this});
}
}

View File

@@ -0,0 +1,22 @@
// 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 '../device.dart';
import 'context.dart';
import 'logger.dart';
DeviceManager get deviceManager => context[DeviceManager];
Logger get logger => context[Logger];
/// Display an error level message to the user. Commands should use this if they
/// fail in some way.
void printError(String message, [StackTrace stackTrace]) => logger.printError(message, stackTrace);
/// Display normal output of the command. This should be used for things like
/// progress messages, success messages, or just normal command output.
void printStatus(String message) => logger.printStatus(message);
/// Use this for verbose tracing output. Users can turn this output on in order
/// to help diagnose issues with the toolchain or with their setup.
void printTrace(String message) => logger.printTrace(message);

View File

@@ -0,0 +1,65 @@
// 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:io';
abstract class Logger {
bool verbose = false;
/// Display an error level message to the user. Commands should use this if they
/// fail in some way.
void printError(String message, [StackTrace stackTrace]);
/// Display normal output of the command. This should be used for things like
/// progress messages, success messages, or just normal command output.
void printStatus(String message);
/// Use this for verbose tracing output. Users can turn this output on in order
/// to help diagnose issues with the toolchain or with their setup.
void printTrace(String message);
}
class StdoutLogger implements Logger {
DateTime _startTime = new DateTime.now();
bool verbose = false;
void printError(String message, [StackTrace stackTrace]) {
stderr.writeln(_prefix + message);
if (stackTrace != null)
stderr.writeln(stackTrace);
}
void printStatus(String message) {
print(_prefix + message);
}
void printTrace(String message) {
if (verbose)
print('$_prefix- $message');
}
String get _prefix {
if (!verbose)
return '';
Duration elapsed = new DateTime.now().difference(_startTime);
return '[${elapsed.inMilliseconds.toString().padLeft(4)} ms] ';
}
}
class BufferLogger implements Logger {
StringBuffer _error = new StringBuffer();
StringBuffer _status = new StringBuffer();
StringBuffer _trace = new StringBuffer();
bool verbose = false;
String get errorText => _error.toString();
String get statusText => _status.toString();
String get traceText => _trace.toString();
void printError(String message, [StackTrace stackTrace]) => _error.writeln(message);
void printStatus(String message) => _status.writeln(message);
void printTrace(String message) => _trace.writeln(message);
}

View File

@@ -6,7 +6,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'context.dart';
import 'globals.dart';
typedef String StringConverter(String string);