OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Common methods used by transfomers for dealing with asset IDs. | 5 /// Common methods used by transfomers for dealing with asset IDs. |
6 library code_transformers.assets; | 6 library code_transformers.assets; |
7 | 7 |
8 import 'dart:math' show min, max; | 8 import 'dart:math' show min, max; |
9 | 9 |
10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
12 import 'package:source_span/source_span.dart'; | 12 import 'package:source_span/source_span.dart'; |
13 | 13 |
| 14 import 'messages/build_logger.dart'; |
| 15 import 'src/messages.dart'; |
| 16 |
14 /// Create an [AssetId] for a [url] seen in the [source] asset. | 17 /// Create an [AssetId] for a [url] seen in the [source] asset. |
15 /// | 18 /// |
16 /// By default this is used to resolve relative urls that occur in HTML assets, | 19 /// By default this is used to resolve relative urls that occur in HTML assets, |
17 /// including cross-package urls of the form "packages/foo/bar.html". Dart | 20 /// including cross-package urls of the form "packages/foo/bar.html". Dart |
18 /// "package:" urls are not resolved unless [source] is Dart file (has a .dart | 21 /// "package:" urls are not resolved unless [source] is Dart file (has a .dart |
19 /// extension). | 22 /// extension). |
20 /// | 23 /// |
21 /// This function returns null if [url] can't be resolved. We log a warning | 24 /// This function returns null if [url] can't be resolved. We log a warning |
22 /// message in [logger] if we know the reason why resolution failed. This | 25 /// message in [logger] if we know the reason why resolution failed. This |
23 /// happens, for example, when using incorrect paths to reach into another | 26 /// happens, for example, when using incorrect paths to reach into another |
24 /// package, or when [errorsOnAbsolute] is true and the url seems to be | 27 /// package, or when [errorsOnAbsolute] is true and the url seems to be |
25 /// absolute. | 28 /// absolute. |
26 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) | 29 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) |
27 AssetId uriToAssetId(AssetId source, String url, TransformLogger logger, | 30 AssetId uriToAssetId(AssetId source, String url, TransformLogger logger, |
28 SourceSpan span, {bool errorOnAbsolute: true}) { | 31 SourceSpan span, {bool errorOnAbsolute: true}) { |
29 if (url == null || url == '') return null; | 32 if (url == null || url == '') return null; |
30 var uri = Uri.parse(url); | 33 var uri = Uri.parse(url); |
31 var urlBuilder = path.url; | 34 var urlBuilder = path.url; |
32 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { | 35 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { |
33 if (source.extension == '.dart' && uri.scheme == 'package') { | 36 if (source.extension == '.dart' && uri.scheme == 'package') { |
34 var index = uri.path.indexOf('/'); | 37 var index = uri.path.indexOf('/'); |
35 if (index != -1) { | 38 if (index != -1) { |
36 return new AssetId(uri.path.substring(0, index), | 39 return new AssetId(uri.path.substring(0, index), |
37 'lib${uri.path.substring(index)}'); | 40 'lib${uri.path.substring(index)}'); |
38 } | 41 } |
39 } | 42 } |
40 | 43 |
41 if (errorOnAbsolute) { | 44 if (errorOnAbsolute) { |
42 logger.warning('absolute paths not allowed: "$url"', span: span); | 45 var msg = NO_ABSOLUTE_PATHS.create({'url': url}); |
| 46 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span); |
43 } | 47 } |
44 return null; | 48 return null; |
45 } | 49 } |
46 | 50 |
47 var targetPath = urlBuilder.normalize( | 51 var targetPath = urlBuilder.normalize( |
48 urlBuilder.join(urlBuilder.dirname(source.path), url)); | 52 urlBuilder.join(urlBuilder.dirname(source.path), url)); |
49 var segments = urlBuilder.split(targetPath); | 53 var segments = urlBuilder.split(targetPath); |
50 var sourceSegments = urlBuilder.split(source.path); | 54 var sourceSegments = urlBuilder.split(source.path); |
51 assert (sourceSegments.length > 0); | 55 assert (sourceSegments.length > 0); |
52 var topFolder = sourceSegments[0]; | 56 var topFolder = sourceSegments[0]; |
(...skipping 21 matching lines...) Expand all Loading... |
74 // folder lives (otherwise the app would not work in Dartium). Since | 78 // folder lives (otherwise the app would not work in Dartium). Since |
75 // [targetPath] has been normalized, "packages" or "assets" should be at | 79 // [targetPath] has been normalized, "packages" or "assets" should be at |
76 // index 1. | 80 // index 1. |
77 return _extractOtherPackageId(1, segments, logger, span); | 81 return _extractOtherPackageId(1, segments, logger, span); |
78 } else { | 82 } else { |
79 var prefix = segments[index]; | 83 var prefix = segments[index]; |
80 var fixedSegments = []; | 84 var fixedSegments = []; |
81 fixedSegments.addAll(sourceSegments.map((_) => '..')); | 85 fixedSegments.addAll(sourceSegments.map((_) => '..')); |
82 fixedSegments.addAll(segments.sublist(index)); | 86 fixedSegments.addAll(segments.sublist(index)); |
83 var fixedUrl = urlBuilder.joinAll(fixedSegments); | 87 var fixedUrl = urlBuilder.joinAll(fixedSegments); |
84 logger.warning('Invalid url to reach to another package: $url. Path ' | 88 var msg = INVALID_URL_TO_OTHER_PACKAGE.create( |
85 'reaching to other packages must first reach up all the ' | 89 {'url': url, 'prefix': prefix, 'fixedUrl': fixedUrl}); |
86 'way to the $prefix folder. For example, try changing the url above ' | 90 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span); |
87 'to: $fixedUrl', span: span); | |
88 return null; | 91 return null; |
89 } | 92 } |
90 } | 93 } |
91 | 94 |
92 // Otherwise, resolve as a path in the same package. | 95 // Otherwise, resolve as a path in the same package. |
93 return new AssetId(source.package, targetPath); | 96 return new AssetId(source.package, targetPath); |
94 } | 97 } |
95 | 98 |
96 AssetId _extractOtherPackageId(int index, List segments, | 99 AssetId _extractOtherPackageId(int index, List segments, |
97 TransformLogger logger, SourceSpan span) { | 100 TransformLogger logger, SourceSpan span) { |
98 if (index >= segments.length) return null; | 101 if (index >= segments.length) return null; |
99 var prefix = segments[index]; | 102 var prefix = segments[index]; |
100 if (prefix != 'packages' && prefix != 'assets') return null; | 103 if (prefix != 'packages' && prefix != 'assets') return null; |
101 var folder = prefix == 'packages' ? 'lib' : 'asset'; | 104 var folder = prefix == 'packages' ? 'lib' : 'asset'; |
102 if (segments.length < index + 3) { | 105 if (segments.length < index + 3) { |
103 logger.warning("incomplete $prefix/ path. It should have at least 3 " | 106 var msg = INVALID_PREFIX_PATH.create({'prefix': prefix, 'folder': folder}); |
104 "segments $prefix/name/path-from-name's-$folder-dir", span: span); | 107 logger.warning(logger is BuildLogger ? msg : msg.snippet, span: span); |
105 return null; | 108 return null; |
106 } | 109 } |
107 return new AssetId(segments[index + 1], | 110 return new AssetId(segments[index + 1], |
108 path.url.join(folder, path.url.joinAll(segments.sublist(index + 2)))); | 111 path.url.join(folder, path.url.joinAll(segments.sublist(index + 2)))); |
109 } | 112 } |
OLD | NEW |