This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformers): Fix relative URL lookup for transformers, and web
Extracts logic used in the TypeRelativeUriGenerator to be used in other transformers. This allows other transformers such as the ExpressionGenerator and later the TemplateCacheGenerator to find relative resources. Fixes asset resolution according to Barback rules which are defined here: http://goo.gl/YDMRc2 specifically library assets are defined with packages being in the URI and all other resources are considered web resources. Closes #1649, #1651
- Loading branch information
Showing
4 changed files
with
107 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
lib/tools/transformer/transformer_resource_url_resolver.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
library angular.tools.transformer.angular_file_resolver; | ||
|
||
import 'package:analyzer/src/generated/element.dart'; | ||
import 'package:code_transformers/resolver.dart'; | ||
import 'package:barback/barback.dart'; | ||
|
||
/// Resolver used to find absolute resources when in Transformers by combining | ||
/// AST element locations and relative URIs. | ||
class TransformerResourceUrlResolver { | ||
final Resolver _resolver; | ||
final AssetId _primaryAsset; | ||
|
||
TransformerResourceUrlResolver(this._resolver, this._primaryAsset); | ||
|
||
Uri findUriOfElement(Element type) { | ||
var uri = _resolver.getImportUri(type.library, from: _primaryAsset); | ||
var acceptable = ( | ||
(uri.isAbsolute && uri.scheme == 'package') || | ||
(uri.toString() == uri.path)); | ||
if (!acceptable) { | ||
var errMsg = 'ERROR: Type "$type" has unsupported URI $uri'; | ||
throw errMsg; | ||
} | ||
if (uri.scheme != "package") { | ||
// this is guaranteed to be a relative URL (e.g. type defined in a path | ||
// imported file) | ||
var path = _primaryAsset.path; | ||
if (!path.startsWith('web/')) { | ||
var errMsg = 'ERROR: Type "$type" is imported as a path not under web.'; | ||
throw errMsg; | ||
} | ||
uri = Uri.parse(path.substring('web/'.length)).resolve(uri.path); | ||
} | ||
return uri; | ||
} | ||
|
||
/// Given a AST [type] and [uri] if [uri] is relative combines it with the uri | ||
/// of the element to make an absolute location relative to types uri. | ||
/// Note: This logic should match [ResourceUrlResolver], but is separate as | ||
/// transformers and Mirrors have different APIs to identify elements, and | ||
/// calculate their URIs. | ||
String combineWithElement(Element type, String uri) { | ||
if (uri != null) { | ||
if (uri.startsWith("/")) return uri; | ||
if (uri.startsWith("packages/")) return "/" + uri; | ||
} | ||
|
||
var typeUri = findUriOfElement(type); | ||
|
||
if (uri == null) { | ||
uri = typeUri.path; | ||
} | ||
// If it's not absolute, then resolve it first | ||
Uri resolved = typeUri.resolve(uri); | ||
|
||
if (resolved.scheme == 'package') { | ||
return '/packages/${resolved.path}'; | ||
} else { | ||
return resolved.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters