OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library kernel.application_root; |
| 5 |
| 6 import 'package:path/path.dart' as pathlib; |
| 7 |
| 8 /// Resolves URIs with the `app` scheme. |
| 9 /// |
| 10 /// These are used internally in kernel to represent file paths relative to |
| 11 /// some application root. This is done to avoid storing irrelevant paths, such |
| 12 /// as the path to the home directory of the user who compiled a given file. |
| 13 class ApplicationRoot { |
| 14 static const String scheme = 'app'; |
| 15 |
| 16 final String path; |
| 17 |
| 18 ApplicationRoot(this.path) { |
| 19 assert(path == null || pathlib.isAbsolute(path)); |
| 20 } |
| 21 |
| 22 ApplicationRoot.none() : path = null; |
| 23 |
| 24 /// Converts `app` URIs to absolute `file` URIs. |
| 25 Uri absoluteUri(Uri uri) { |
| 26 if (path == null) return uri; |
| 27 if (uri.scheme == ApplicationRoot.scheme) { |
| 28 return new Uri(scheme: 'file', path: pathlib.join(this.path, uri.path)); |
| 29 } else { |
| 30 return uri; |
| 31 } |
| 32 } |
| 33 |
| 34 /// Converts `file` URIs to `app` URIs. |
| 35 Uri relativeUri(Uri uri) { |
| 36 if (path == null) return uri; |
| 37 if (uri.scheme == 'file' && pathlib.isWithin(this.path, uri.path)) { |
| 38 return new Uri( |
| 39 scheme: ApplicationRoot.scheme, |
| 40 path: pathlib.relative(uri.path, from: this.path)); |
| 41 } else { |
| 42 return uri; |
| 43 } |
| 44 } |
| 45 } |
OLD | NEW |