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(pathlib.isAbsolute(path)); | |
20 } | |
21 | |
22 /// Resolves a given file path or URI to a relative URI. | |
23 Uri resolve(String pathString) { | |
24 var uri = Uri.parse(pathString); | |
25 if (uri.hasScheme) return relativeUri(uri); | |
26 return new Uri( | |
27 scheme: ApplicationRoot.scheme, | |
28 path: pathlib.relative(uri.path, from: this.path)); | |
29 } | |
30 | |
31 /// Converts `app` URIs to absolute `file` URIs. | |
32 Uri absoluteUri(Uri uri) { | |
33 if (uri.scheme == ApplicationRoot.scheme) { | |
34 return new Uri(scheme: 'file', path: pathlib.join(this.path, uri.path)); | |
35 } else { | |
36 return uri; | |
37 } | |
38 } | |
39 | |
40 /// Converts `file` URIs to `app` URIs. | |
41 Uri relativeUri(Uri uri) { | |
42 if (uri.scheme == 'file') { | |
43 return new Uri( | |
44 scheme: ApplicationRoot.scheme, | |
45 path: pathlib.relative(uri.path, from: this.path)); | |
46 } else { | |
47 return uri; | |
48 } | |
49 } | |
50 } | |
OLD | NEW |