OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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. */ | 5 /** Common methods used by transfomers. */ |
6 library polymer.src.build.common; | 6 library polymer.src.build.common; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:math' show min, max; | 9 import 'dart:math' show min, max; |
10 | 10 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 var id = asset.id; | 97 var id = asset.id; |
98 return asset.readAsString().then((content) { | 98 return asset.readAsString().then((content) { |
99 return _parseHtml(content, id.path, transform.logger, | 99 return _parseHtml(content, id.path, transform.logger, |
100 checkDocType: options.isHtmlEntryPoint(id)); | 100 checkDocType: options.isHtmlEntryPoint(id)); |
101 }); | 101 }); |
102 } | 102 } |
103 | 103 |
104 Future<Document> readAsHtml(AssetId id, Transform transform) { | 104 Future<Document> readAsHtml(AssetId id, Transform transform) { |
105 var primaryId = transform.primaryInput.id; | 105 var primaryId = transform.primaryInput.id; |
106 bool samePackage = id.package == primaryId.package; | 106 bool samePackage = id.package == primaryId.package; |
107 var url = samePackage ? id.path | 107 var url = spanUrlFor(id, transform); |
108 : assetUrlFor(id, primaryId, transform.logger, allowAssetUrl: true); | |
109 return transform.readInputAsString(id).then((content) { | 108 return transform.readInputAsString(id).then((content) { |
110 return _parseHtml(content, url, transform.logger, | 109 return _parseHtml(content, url, transform.logger, |
111 checkDocType: samePackage && options.isHtmlEntryPoint(id)); | 110 checkDocType: samePackage && options.isHtmlEntryPoint(id)); |
112 }); | 111 }); |
113 } | 112 } |
114 | 113 |
| 114 /** |
| 115 * Gets the appropriate URL to use in a [Span] to produce messages |
| 116 * (e.g. warnings) for users. This will attempt to format the URL in the most |
| 117 * useful way: |
| 118 * |
| 119 * - If the asset is within the primary package, then use the [id.path], |
| 120 * the user will know it is a file from their own code. |
| 121 * - If the asset is from another package, then use [assetUrlFor], this will |
| 122 * likely be a "package:" url to the file in the other package, which is |
| 123 * enough for users to identify where the error is. |
| 124 */ |
| 125 String spanUrlFor(AssetId id, Transform transform) { |
| 126 var primaryId = transform.primaryInput.id; |
| 127 bool samePackage = id.package == primaryId.package; |
| 128 return samePackage ? id.path |
| 129 : assetUrlFor(id, primaryId, transform.logger, allowAssetUrl: true); |
| 130 } |
| 131 |
115 Future<bool> assetExists(AssetId id, Transform transform) => | 132 Future<bool> assetExists(AssetId id, Transform transform) => |
116 transform.getInput(id).then((_) => true).catchError((_) => false); | 133 transform.getInput(id).then((_) => true).catchError((_) => false); |
117 | 134 |
118 String toString() => 'polymer ($runtimeType)'; | 135 String toString() => 'polymer ($runtimeType)'; |
119 } | 136 } |
120 | 137 |
121 /** Transformer phases which should be applied to the Polymer package. */ | 138 /** Transformer phases which should be applied to the Polymer package. */ |
122 List<List<Transformer>> get phasesForPolymer => | 139 List<List<Transformer>> get phasesForPolymer => |
123 [[new ObservableTransformer(['lib/src/instance.dart'])]]; | 140 [[new ObservableTransformer(['lib/src/instance.dart'])]]; |
124 | 141 |
125 /** | 142 /** |
126 * Create an [AssetId] for a [url] seen in the [source] asset. By default this | 143 * Create an [AssetId] for a [url] seen in the [source] asset. By default this |
127 * is used to resolve relative urls that occur in HTML assets, including | 144 * is used to resolve relative urls that occur in HTML assets, including |
128 * cross-package urls of the form "packages/foo/bar.html". Dart-style "package:" | 145 * cross-package urls of the form "packages/foo/bar.html". Dart-style "package:" |
129 * urls are not resolved unless [source] is Dart file (has a .dart extension). | 146 * urls are not resolved unless [source] is Dart file (has a .dart extension). |
130 */ | 147 */ |
131 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) | 148 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) |
132 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span) { | 149 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span, |
| 150 {bool allowAbsolute: false}) { |
133 if (url == null || url == '') return null; | 151 if (url == null || url == '') return null; |
134 var uri = Uri.parse(url); | 152 var uri = Uri.parse(url); |
135 var urlBuilder = path.url; | 153 var urlBuilder = path.url; |
136 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { | 154 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { |
137 if (source.extension == '.dart' && uri.scheme == 'package') { | 155 if (source.extension == '.dart' && uri.scheme == 'package') { |
138 var index = uri.path.indexOf('/'); | 156 var index = uri.path.indexOf('/'); |
139 if (index != -1) { | 157 if (index != -1) { |
140 return new AssetId(uri.path.substring(0, index), | 158 return new AssetId(uri.path.substring(0, index), |
141 'lib${uri.path.substring(index)}'); | 159 'lib${uri.path.substring(index)}'); |
142 } | 160 } |
143 } | 161 } |
144 | 162 |
145 logger.error('absolute paths not allowed: "$url"', span: span); | 163 if (!allowAbsolute) { |
| 164 logger.error('absolute paths not allowed: "$url"', span: span); |
| 165 } |
146 return null; | 166 return null; |
147 } | 167 } |
148 | 168 |
149 var targetPath = urlBuilder.normalize( | 169 var targetPath = urlBuilder.normalize( |
150 urlBuilder.join(urlBuilder.dirname(source.path), url)); | 170 urlBuilder.join(urlBuilder.dirname(source.path), url)); |
151 var segments = urlBuilder.split(targetPath); | 171 var segments = urlBuilder.split(targetPath); |
152 var sourceSegments = urlBuilder.split(source.path); | 172 var sourceSegments = urlBuilder.split(source.path); |
153 assert (sourceSegments.length > 0); | 173 assert (sourceSegments.length > 0); |
154 var topFolder = sourceSegments[0]; | 174 var topFolder = sourceSegments[0]; |
155 var entryFolder = topFolder != 'lib' && topFolder != 'asset'; | 175 var entryFolder = topFolder != 'lib' && topFolder != 'asset'; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 return builder.relative(builder.join('/', id.path), | 259 return builder.relative(builder.join('/', id.path), |
240 from: builder.join('/', builder.dirname(sourceId.path))); | 260 from: builder.join('/', builder.dirname(sourceId.path))); |
241 } | 261 } |
242 | 262 |
243 | 263 |
244 /** Convert system paths to asset paths (asset paths are posix style). */ | 264 /** Convert system paths to asset paths (asset paths are posix style). */ |
245 String _systemToAssetPath(String assetPath) { | 265 String _systemToAssetPath(String assetPath) { |
246 if (path.Style.platform != path.Style.windows) return assetPath; | 266 if (path.Style.platform != path.Style.windows) return assetPath; |
247 return path.posix.joinAll(path.split(assetPath)); | 267 return path.posix.joinAll(path.split(assetPath)); |
248 } | 268 } |
OLD | NEW |