Files
sdk/pkg/kernel/lib/application_root.dart
T
Asger Feldthaus 709c1e0b75 Store library paths relative to a given application root folder.
In kernel, library import URIs now support an "app" scheme as an
alternative to the "file" scheme, representing a path relative to
the application root.

dartk takes an --app-root flag giving the application root. If none
is given, file URIs are used instead.

The intention is that kernel binaries should not carry irrelevant
path information, such as the path to the home directory of the
user who compiled a given file.

It is not the intention that end-users should see an app URI.
Import paths are currently not shown to users at all, and if we need
to do this, they should be translated to file paths first.

In theory we could stick to file URIs with relative paths, but the Uri
class from dart:core makes this difficult, as certain operations on it
assume that file paths should be absolute.

Source mapping URIs are not yet affected by this change.

R=kmillikin@google.com

Committed: https://github.com/dart-lang/sdk/commit/60adb852ad706ecf9424c77d47fa05583d543def

Review URL: https://codereview.chromium.org/2532053005 .

Reverted: https://github.com/dart-lang/sdk/commit/bb540416f27c39d75ee7f243899939fc37a2cd03
2016-11-30 10:39:48 +01:00

46 lines
1.4 KiB
Dart

// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library kernel.application_root;
import 'package:path/path.dart' as pathlib;
/// Resolves URIs with the `app` scheme.
///
/// These are used internally in kernel to represent file paths relative to
/// some application root. This is done to avoid storing irrelevant paths, such
/// as the path to the home directory of the user who compiled a given file.
class ApplicationRoot {
static const String scheme = 'app';
final String path;
ApplicationRoot(this.path) {
assert(path == null || pathlib.isAbsolute(path));
}
ApplicationRoot.none() : path = null;
/// Converts `app` URIs to absolute `file` URIs.
Uri absoluteUri(Uri uri) {
if (path == null) return uri;
if (uri.scheme == ApplicationRoot.scheme) {
return new Uri(scheme: 'file', path: pathlib.join(this.path, uri.path));
} else {
return uri;
}
}
/// Converts `file` URIs to `app` URIs.
Uri relativeUri(Uri uri) {
if (path == null) return uri;
if (uri.scheme == 'file' && pathlib.isWithin(this.path, uri.path)) {
return new Uri(
scheme: ApplicationRoot.scheme,
path: pathlib.relative(uri.path, from: this.path));
} else {
return uri;
}
}
}