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 |
| 11 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/error.dart'; |
| 13 import 'package:analyzer/src/generated/parser.dart'; |
| 14 import 'package:analyzer/src/generated/scanner.dart'; |
11 import 'package:barback/barback.dart'; | 15 import 'package:barback/barback.dart'; |
12 import 'package:html5lib/dom.dart' show Document; | 16 import 'package:html5lib/dom.dart' show Document; |
13 import 'package:html5lib/parser.dart' show HtmlParser; | 17 import 'package:html5lib/parser.dart' show HtmlParser; |
14 import 'package:path/path.dart' as path; | 18 import 'package:path/path.dart' as path; |
15 import 'package:observe/transformer.dart' show ObservableTransformer; | 19 import 'package:observe/transformer.dart' show ObservableTransformer; |
16 import 'package:source_maps/span.dart' show Span; | 20 import 'package:source_maps/span.dart' show Span; |
17 | 21 |
18 /// Parses an HTML file [contents] and returns a DOM-like tree. Adds emitted | 22 /// Parses an HTML file [contents] and returns a DOM-like tree. Adds emitted |
19 /// error/warning to [logger]. | 23 /// error/warning to [logger]. |
20 Document _parseHtml(String contents, String sourcePath, TransformLogger logger, | 24 Document _parseHtml(String contents, String sourcePath, TransformLogger logger, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 Future<Document> readAsHtml(AssetId id, Transform transform) { | 98 Future<Document> readAsHtml(AssetId id, Transform transform) { |
95 var primaryId = transform.primaryInput.id; | 99 var primaryId = transform.primaryInput.id; |
96 bool samePackage = id.package == primaryId.package; | 100 bool samePackage = id.package == primaryId.package; |
97 var url = spanUrlFor(id, transform); | 101 var url = spanUrlFor(id, transform); |
98 return transform.readInputAsString(id).then((content) { | 102 return transform.readInputAsString(id).then((content) { |
99 return _parseHtml(content, url, transform.logger, | 103 return _parseHtml(content, url, transform.logger, |
100 checkDocType: samePackage && options.isHtmlEntryPoint(id)); | 104 checkDocType: samePackage && options.isHtmlEntryPoint(id)); |
101 }); | 105 }); |
102 } | 106 } |
103 | 107 |
104 /// Gets the appropriate URL to use in a [Span] to produce messages | |
105 /// (e.g. warnings) for users. This will attempt to format the URL in the most | |
106 /// useful way: | |
107 /// | |
108 /// - If the asset is within the primary package, then use the [id.path], | |
109 /// the user will know it is a file from their own code. | |
110 /// - If the asset is from another package, then use [assetUrlFor], this will | |
111 /// likely be a "package:" url to the file in the other package, which is | |
112 /// enough for users to identify where the error is. | |
113 String spanUrlFor(AssetId id, Transform transform) { | |
114 var primaryId = transform.primaryInput.id; | |
115 bool samePackage = id.package == primaryId.package; | |
116 return samePackage ? id.path | |
117 : assetUrlFor(id, primaryId, transform.logger, allowAssetUrl: true); | |
118 } | |
119 | |
120 Future<bool> assetExists(AssetId id, Transform transform) => | 108 Future<bool> assetExists(AssetId id, Transform transform) => |
121 transform.getInput(id).then((_) => true).catchError((_) => false); | 109 transform.getInput(id).then((_) => true).catchError((_) => false); |
122 | 110 |
123 String toString() => 'polymer ($runtimeType)'; | 111 String toString() => 'polymer ($runtimeType)'; |
124 } | 112 } |
125 | 113 |
| 114 /// Gets the appropriate URL to use in a [Span] to produce messages |
| 115 /// (e.g. warnings) for users. This will attempt to format the URL in the most |
| 116 /// useful way: |
| 117 /// |
| 118 /// - If the asset is within the primary package, then use the [id.path], |
| 119 /// the user will know it is a file from their own code. |
| 120 /// - If the asset is from another package, then use [assetUrlFor], this will |
| 121 /// likely be a "package:" url to the file in the other package, which is |
| 122 /// enough for users to identify where the error is. |
| 123 String spanUrlFor(AssetId id, Transform transform) { |
| 124 var primaryId = transform.primaryInput.id; |
| 125 bool samePackage = id.package == primaryId.package; |
| 126 return samePackage ? id.path |
| 127 : assetUrlFor(id, primaryId, transform.logger, allowAssetUrl: true); |
| 128 } |
| 129 |
126 /// Transformer phases which should be applied to the Polymer package. | 130 /// Transformer phases which should be applied to the Polymer package. |
127 List<List<Transformer>> get phasesForPolymer => | 131 List<List<Transformer>> get phasesForPolymer => |
128 [[new ObservableTransformer(['lib/src/instance.dart'])]]; | 132 [[new ObservableTransformer(['lib/src/instance.dart'])]]; |
129 | 133 |
130 /// Create an [AssetId] for a [url] seen in the [source] asset. By default this | 134 /// Create an [AssetId] for a [url] seen in the [source] asset. By default this |
131 /// is used to resolve relative urls that occur in HTML assets, including | 135 /// is used to resolve relative urls that occur in HTML assets, including |
132 /// cross-package urls of the form "packages/foo/bar.html". Dart "package:" | 136 /// cross-package urls of the form "packages/foo/bar.html". Dart "package:" |
133 /// urls are not resolved unless [source] is Dart file (has a .dart extension). | 137 /// urls are not resolved unless [source] is Dart file (has a .dart extension). |
134 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) | 138 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) |
135 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span, | 139 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span, |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 return builder.relative(builder.join('/', id.path), | 247 return builder.relative(builder.join('/', id.path), |
244 from: builder.join('/', builder.dirname(sourceId.path))); | 248 from: builder.join('/', builder.dirname(sourceId.path))); |
245 } | 249 } |
246 | 250 |
247 | 251 |
248 /// Convert system paths to asset paths (asset paths are posix style). | 252 /// Convert system paths to asset paths (asset paths are posix style). |
249 String _systemToAssetPath(String assetPath) { | 253 String _systemToAssetPath(String assetPath) { |
250 if (path.Style.platform != path.Style.windows) return assetPath; | 254 if (path.Style.platform != path.Style.windows) return assetPath; |
251 return path.posix.joinAll(path.split(assetPath)); | 255 return path.posix.joinAll(path.split(assetPath)); |
252 } | 256 } |
| 257 |
| 258 |
| 259 /// Parse [code] using analyzer. |
| 260 CompilationUnit parseCompilationUnit(String code) { |
| 261 var errorListener = new _ErrorCollector(); |
| 262 var reader = new CharSequenceReader(code); |
| 263 var scanner = new Scanner(null, reader, errorListener); |
| 264 var token = scanner.tokenize(); |
| 265 var parser = new Parser(null, errorListener); |
| 266 return parser.parseCompilationUnit(token); |
| 267 } |
| 268 |
| 269 class _ErrorCollector extends AnalysisErrorListener { |
| 270 final errors = <AnalysisError>[]; |
| 271 onError(error) => errors.add(error); |
| 272 } |
OLD | NEW |