| Index: runtime/bin/builtin.dart
|
| diff --git a/runtime/bin/builtin.dart b/runtime/bin/builtin.dart
|
| index 716ec71c3a0cff95b97e457f1104fe1d9057ca08..818acaf7c7ab5ee7b7e3cce51573baac6c2008c5 100644
|
| --- a/runtime/bin/builtin.dart
|
| +++ b/runtime/bin/builtin.dart
|
| @@ -5,28 +5,56 @@
|
| library builtin;
|
| import 'dart:io';
|
|
|
| -int _httpRequestResponseCode = 0;
|
| -String _httpRequestStatusString;
|
| +// Corelib 'print' implementation.
|
| +void _print(arg) {
|
| + _Logger._printString(arg.toString());
|
| +}
|
| +
|
| +
|
| +class _Logger {
|
| + static void _printString(String s) native "Logger_PrintString";
|
| +}
|
| +
|
| +
|
| +_getPrintClosure() => _print;
|
| +
|
| +
|
| +void _logResolution(String msg) {
|
| + final enabled = false;
|
| + if (enabled) {
|
| + _Logger._printString(msg);
|
| + }
|
| +}
|
| +
|
| +
|
| +var _httpRequestResponseCode = 0;
|
| +var _httpRequestStatusString;
|
| var _httpRequestResponse;
|
|
|
| +_getHttpRequestResponseCode() => _httpRequestResponseCode;
|
| +_getHttpRequestStatusString() => _httpRequestStatusString;
|
| +_getHttpRequestResponse() => _httpRequestResponse;
|
| +
|
| void _requestCompleted(HttpClientResponseBody body) {
|
| _httpRequestResponseCode = body.statusCode;
|
| _httpRequestStatusString = '${body.statusCode} ${body.reasonPhrase}';
|
| _httpRequestResponse = null;
|
| - if (body.statusCode != 200 || body.type == "json") {
|
| + if (body.statusCode != 200 || body.type == 'json') {
|
| return;
|
| }
|
| _httpRequestResponse = body.body;
|
| }
|
|
|
| +
|
| void _requestFailed(error) {
|
| _httpRequestResponseCode = 0;
|
| _httpRequestStatusString = error.toString();
|
| _httpRequestResponse = null;
|
| }
|
|
|
| -HttpClient _client = new HttpClient();
|
| +
|
| void _makeHttpRequest(String uri) {
|
| + var _client = new HttpClient();
|
| _httpRequestResponseCode = 0;
|
| _httpRequestStatusString = null;
|
| _httpRequestResponse = null;
|
| @@ -41,76 +69,94 @@ void _makeHttpRequest(String uri) {
|
| });
|
| }
|
|
|
| -// Corelib 'print' implementation.
|
| -void _print(arg) {
|
| - _Logger._printString(arg.toString());
|
| -}
|
|
|
| -class _Logger {
|
| - static void _printString(String s) native "Logger_PrintString";
|
| +// Are we running on Windows?
|
| +var _isWindows = false;
|
| +// The current working directory
|
| +var _workingDirectoryUri;
|
| +// The URI that the entry point script was loaded from. Remembered so that
|
| +// package imports can be resolved relative to it.
|
| +var _entryPointScript;
|
| +// The directory to look in to resolve "package:" scheme URIs.
|
| +var _packageRoot;
|
| +
|
| +
|
| +void _setWindows() {
|
| + _isWindows = true;
|
| }
|
|
|
| -_getPrintClosure() => _print;
|
|
|
| -// The URI that the entrypoint script was loaded from. Remembered so that
|
| -// package imports can be resolved relative to it.
|
| -var _entrypoint;
|
| +_sanitizeWindowsPath(path) {
|
| + // For Windows we need to massage the paths a bit according to
|
| + // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
|
| + //
|
| + // Convert
|
| + // C:\one\two\three
|
| + // to
|
| + // /C:/one/two/three
|
|
|
| -// The directory to look in to resolve "package:" scheme URIs.
|
| -var _packageRoot;
|
| + if (_isWindows == false) {
|
| + // Do nothing when not running Windows.
|
| + return path;
|
| + }
|
|
|
| -void _logResolution(String msg) {
|
| - final enabled = false;
|
| - if (enabled) {
|
| - _Logger._printString(msg);
|
| + var fixedPath = "${path.replaceAll('\\', '/')}";
|
| +
|
| + if ((path.length > 2) && (path[1] == ':')) {
|
| + // Path begins with a drive letter.
|
| + return '/$fixedPath';
|
| + }
|
| +
|
| + return fixedPath;
|
| +}
|
| +
|
| +_enforceTrailingSlash(uri) {
|
| + // Ensure we have a trailing slash character.
|
| + if (!uri.endsWith('/')) {
|
| + return '$uri/';
|
| }
|
| + return uri;
|
| }
|
|
|
| +
|
| +void _setWorkingDirectory(cwd) {
|
| + cwd = _sanitizeWindowsPath(cwd);
|
| + cwd = _enforceTrailingSlash(cwd);
|
| + _workingDirectoryUri = new Uri(scheme: 'file', path: cwd);
|
| + _logResolution('# Working Directory: $cwd');
|
| +}
|
| +
|
| +
|
| _setPackageRoot(String packageRoot) {
|
| - // TODO(mattsh) - refactor windows drive and path handling code
|
| - // so it can be used here if needed.
|
| - _packageRoot = packageRoot;
|
| + packageRoot = _enforceTrailingSlash(packageRoot);
|
| + _packageRoot = _workingDirectoryUri.resolve(packageRoot);
|
| + _logResolution('# Package root: $packageRoot -> $_packageRoot');
|
| }
|
|
|
| -String _resolveScriptUri(String cwd, String scriptName, bool isWindows) {
|
| - var scriptUri = Uri.parse(scriptName);
|
| - if (scriptUri.scheme == 'http') {
|
| - _entrypoint = scriptUri;
|
| - _logResolution("# Resolved script to: $_entrypoint");
|
| - return _entrypoint.toString();
|
| - }
|
| - _logResolution("# Current working directory: $cwd");
|
| - _logResolution("# ScriptName: $scriptName");
|
| - if (isWindows) {
|
| - // For Windows we need to massage the paths a bit according to
|
| - // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
|
| - //
|
| - // Convert
|
| - // C:\one\two\three
|
| - // to
|
| - // /C:/one/two/three
|
| - cwd = "/${cwd.replaceAll('\\', '/')}";
|
| - _logResolution("## cwd: $cwd");
|
| - if ((scriptName.length > 2) && (scriptName[1] == ":")) {
|
| - // This is an absolute path.
|
| - scriptName = "/${scriptName.replaceAll('\\', '/')}";
|
| - } else {
|
| - scriptName = scriptName.replaceAll('\\', '/');
|
| - }
|
| - _logResolution("## scriptName: $scriptName");
|
| +
|
| +String _resolveScriptUri(String scriptName) {
|
| + if (_workingDirectoryUri == null) {
|
| + throw 'No current working directory set.';
|
| }
|
| - var base =
|
| - new Uri(scheme: "file",
|
| - path: cwd.endsWith("/") ? cwd : "$cwd/");
|
| - _entrypoint = base.resolve(scriptName);
|
| - _logResolution("# Resolved script to: $_entrypoint");
|
| + scriptName = _sanitizeWindowsPath(scriptName);
|
|
|
| - return _entrypoint.toString();
|
| + var scriptUri = Uri.parse(scriptName);
|
| + if (scriptUri.scheme != '') {
|
| + // Script has a scheme, assume that it is fully formed.
|
| + _entryPointScript = scriptUri;
|
| + } else {
|
| + // Script does not have a scheme, assume that it is a path,
|
| + // resolve it against the working directory.
|
| + _entryPointScript = _workingDirectoryUri.resolve(scriptName);
|
| + }
|
| + _logResolution('# Resolved entry point to: $_entryPointScript');
|
| + return _entryPointScript.toString();
|
| }
|
|
|
| +
|
| String _resolveUri(String base, String userString) {
|
| var baseUri = Uri.parse(base);
|
| - _logResolution("# Resolving: $userString from $base");
|
| + _logResolution('# Resolving: $userString from $base');
|
|
|
| var uri = Uri.parse(userString);
|
| var resolved;
|
| @@ -125,18 +171,18 @@ String _resolveUri(String base, String userString) {
|
| // package URI path part.
|
| path = _filePathFromPackageUri(resolved);
|
| }
|
| - resolved = new Uri(scheme: "dart-ext", path: path);
|
| + resolved = new Uri(scheme: 'dart-ext', path: path);
|
| } else {
|
| resolved = baseUri.resolve(userString);
|
| }
|
| - _logResolution("# Resolved to: $resolved");
|
| + _logResolution('# Resolved to: $resolved');
|
| return resolved.toString();
|
| }
|
|
|
|
|
| -String _filePathFromUri(String userUri, bool isWindows) {
|
| +String _filePathFromUri(String userUri) {
|
| var uri = Uri.parse(userUri);
|
| - _logResolution("# Getting file path from: $uri");
|
| + _logResolution('# Getting file path from: $uri');
|
|
|
| var path;
|
| switch (uri.scheme) {
|
| @@ -154,40 +200,43 @@ String _filePathFromUri(String userUri, bool isWindows) {
|
| break;
|
| default:
|
| // Only handling file and package URIs in standalone binary.
|
| - _logResolution("# Unknown scheme (${uri.scheme}) in $uri.");
|
| - throw "Not a known scheme: $uri";
|
| + _logResolution('# Unknown scheme (${uri.scheme}) in $uri.');
|
| + throw 'Not a known scheme: $uri';
|
| }
|
|
|
| - if (isWindows && path.startsWith("/")) {
|
| + if (_isWindows && path.startsWith('/')) {
|
| // For Windows we need to massage the paths a bit according to
|
| // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
|
| //
|
| // Drop the leading / before the drive letter.
|
| path = path.substring(1);
|
| - _logResolution("# path: $path");
|
| + _logResolution('# Path: Removed leading / -> $path');
|
| }
|
|
|
| return path;
|
| }
|
|
|
| +
|
| String _filePathFromFileUri(Uri uri) {
|
| if (!uri.host.isEmpty) {
|
| throw "URIs using the 'file:' scheme may not contain a host.";
|
| }
|
|
|
| - _logResolution("# Path: ${uri.path}");
|
| + _logResolution('# Path: $uri -> ${uri.path}');
|
| return uri.path;
|
| }
|
|
|
| +
|
| String _filePathFromOtherUri(Uri uri) {
|
| if (!uri.host.isEmpty) {
|
| - throw "URIs whose paths are used as file paths may not contain a host.";
|
| + throw 'URIs whose paths are used as file paths may not contain a host.';
|
| }
|
|
|
| - _logResolution("# Path: ${uri.path}");
|
| + _logResolution('# Path: $uri -> ${uri.path}');
|
| return uri.path;
|
| }
|
|
|
| +
|
| String _filePathFromPackageUri(Uri uri) {
|
| if (!uri.host.isEmpty) {
|
| var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host;
|
| @@ -198,22 +247,27 @@ String _filePathFromPackageUri(Uri uri) {
|
| "'$right', not '$wrong'.";
|
| }
|
|
|
| + var packageUri;
|
| var path;
|
| if (_packageRoot != null) {
|
| - path = "${_packageRoot}${uri.path}";
|
| + // Resolve against package root.
|
| + packageUri = _packageRoot.resolve(uri.path);
|
| } else {
|
| - if (_entrypoint.scheme == 'http') {
|
| - path = _entrypoint.resolve('packages/${uri.path}').toString();
|
| - } else {
|
| - path = _entrypoint.resolve('packages/${uri.path}').path;
|
| - }
|
| + // Resolve against working directory.
|
| + packageUri = _entryPointScript.resolve('packages/${uri.path}');
|
| }
|
|
|
| - _logResolution("# Package: $path");
|
| + if (packageUri.scheme == 'file') {
|
| + path = packageUri.path;
|
| + } else {
|
| + path = packageUri.toString();
|
| + }
|
| + _logResolution('# Package: $uri -> $path');
|
| return path;
|
| }
|
|
|
| +
|
| String _filePathFromHttpUri(Uri uri) {
|
| - _logResolution('# Path: $uri');
|
| + _logResolution('# Path: $uri -> $uri');
|
| return uri.toString();
|
| }
|
|
|