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 | |
5 library dev_compiler.src.transformer.uri_resolver; | |
6 | |
7 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; | |
8 import 'package:analyzer/src/generated/source.dart' show Source, SourceFactory; | |
9 import 'package:barback/barback.dart' show AssetId; | |
10 import 'package:code_transformers/resolver.dart' | |
11 show DartUriResolverProxy, DirectoryBasedDartSdkProxy; | |
12 import 'package:cli_util/cli_util.dart' as cli_util; | |
13 import 'package:path/path.dart' as path; | |
14 | |
15 import 'asset_source.dart'; | |
16 | |
17 typedef AssetSource AssetSourceGetter(AssetId id); | |
18 | |
19 /// Builds a package URI that corresponds to [id]. This is roughly the inverse | |
20 /// function of [resolveAssetId]. | |
21 /// | |
22 /// Note that if [id] points to a file outside the package's `lib` folder, that | |
23 /// file must be under `web` and the returned URI will not strictly correspond | |
24 /// to classic package URIs (but it will be invertible by [resolveAssetId]). | |
25 String assetIdToUri(AssetId id) { | |
26 var p = id.path; | |
27 if (p.startsWith('lib/web/')) { | |
28 throw new ArgumentError('Cannot convert $id to an unambiguous package uri'); | |
29 } | |
30 if (p.startsWith('lib/')) { | |
31 p = p.substring('lib/'.length); | |
32 } else if (!p.startsWith('web/')) { | |
33 throw new ArgumentError('Unexpected path in $id (expected {lib,web}/*'); | |
34 } | |
35 // Note: if the file is under web/, then we leave it as it is: resolveAssetId | |
36 // does the inverse transform. | |
37 return 'package:${id.package}/$p'; | |
38 } | |
39 | |
40 /// Gets the [AssetId] that corresponds to [uri]. | |
41 /// | |
42 /// If [fromAssetId] is not `null` and if [uri] is a relative URI, then | |
43 /// [fromAssetId] will be used to resolve the create a relative URI. In other | |
44 /// cases, this is roughly the inverse function of [assetIdToUri]. | |
45 AssetId resolveAssetId(Uri uri, {AssetId fromAssetId}) { | |
46 if (uri.scheme == 'dart') return null; | |
47 | |
48 if (uri.scheme == 'package') { | |
49 var segments = uri.pathSegments.toList(); | |
50 var package = segments[0]; | |
51 if (segments[1] == 'web') { | |
52 return new AssetId(package, path.url.joinAll(segments.skip(1))); | |
53 } else { | |
54 segments[0] = 'lib'; | |
55 return new AssetId(package, path.url.joinAll(segments)); | |
56 } | |
57 } | |
58 | |
59 if (uri.scheme == null || uri.scheme == '') { | |
60 if (fromAssetId == null) { | |
61 throw new ArgumentError('No asset to resolve relative URI from.'); | |
62 } | |
63 return new AssetId(fromAssetId.package, | |
64 path.normalize(path.join(path.dirname(fromAssetId.path), uri.path))); | |
65 } | |
66 | |
67 throw new ArgumentError('Unexpected uri: $uri (uri.scheme = ${uri.scheme})'); | |
68 } | |
69 | |
70 class _DdcUriResolver extends DartUriResolverProxy { | |
71 AssetSourceGetter _getAssetSource; | |
72 | |
73 _DdcUriResolver(DartSdk sdk, this._getAssetSource) : super(sdk); | |
74 | |
75 @override | |
76 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | |
77 return uri.scheme == 'package' | |
78 ? _getAssetSource(resolveAssetId(uri)) | |
79 : super.resolveAbsolute(uri, actualUri); | |
80 } | |
81 } | |
82 | |
83 String get dartSdkDirectory => cli_util.getSdkDir()?.path; | |
84 | |
85 SourceFactory createSourceFactory(AssetSourceGetter getAssetSource) { | |
86 var sdk = new DirectoryBasedDartSdkProxy(dartSdkDirectory); | |
87 return new SourceFactory([new _DdcUriResolver(sdk, getAssetSource)]); | |
88 } | |
OLD | NEW |