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 library code_transformer.src.resolver_impl; | 5 library code_transformer.src.resolver_impl; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'package:analyzer/analyzer.dart' show parseDirectives; | 8 import 'package:analyzer/analyzer.dart' show parseDirectives; |
9 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
10 import 'package:analyzer/src/generated/constant.dart' show ConstantEvaluator, | 10 import 'package:analyzer/src/generated/constant.dart' show ConstantEvaluator, |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 /// Contents of the file. | 335 /// Contents of the file. |
336 TimestampedData<String> get contents { | 336 TimestampedData<String> get contents { |
337 if (!exists()) throw new StateError('$assetId does not exist'); | 337 if (!exists()) throw new StateError('$assetId does not exist'); |
338 | 338 |
339 return new TimestampedData<String>(modificationStamp, _contents); | 339 return new TimestampedData<String>(modificationStamp, _contents); |
340 } | 340 } |
341 | 341 |
342 /// Contents of the file. | 342 /// Contents of the file. |
343 String get rawContents => _contents; | 343 String get rawContents => _contents; |
344 | 344 |
| 345 Uri get uri => Uri.parse('asset:${assetId.package}/${assetId.path}'); |
| 346 |
345 /// Logger for the current transform. | 347 /// Logger for the current transform. |
346 /// | 348 /// |
347 /// Only valid while the resolver is updating assets. | 349 /// Only valid while the resolver is updating assets. |
348 TransformLogger get _logger => _resolver._currentTransform.logger; | 350 TransformLogger get _logger => _resolver._currentTransform.logger; |
349 | 351 |
350 /// Gets all imports/parts/exports which resolve to assets (non-Dart files). | 352 /// Gets all imports/parts/exports which resolve to assets (non-Dart files). |
351 Iterable<AssetId> get dependentAssets => _dependentAssets; | 353 Iterable<AssetId> get dependentAssets => _dependentAssets; |
352 | 354 |
353 bool exists() => _contents != null; | 355 bool exists() => _contents != null; |
354 | 356 |
(...skipping 27 matching lines...) Expand all Loading... |
382 if (id == null) return null; | 384 if (id == null) return null; |
383 | 385 |
384 // The entire AST should have been parsed and loaded at this point. | 386 // The entire AST should have been parsed and loaded at this point. |
385 var source = _resolver.sources[id]; | 387 var source = _resolver.sources[id]; |
386 if (source == null) { | 388 if (source == null) { |
387 _logger.error('Could not load asset $id'); | 389 _logger.error('Could not load asset $id'); |
388 } | 390 } |
389 return source; | 391 return source; |
390 } | 392 } |
391 | 393 |
| 394 Uri resolveRelativeUri(Uri relativeUri) { |
| 395 var id = _resolve(assetId, relativeUri.toString(), _logger, null); |
| 396 if (id == null) return uri.resolveUri(relativeUri); |
| 397 |
| 398 // The entire AST should have been parsed and loaded at this point. |
| 399 var source = _resolver.sources[id]; |
| 400 if (source == null) { |
| 401 _logger.error('Could not load asset $id'); |
| 402 } |
| 403 return source.uri; |
| 404 } |
| 405 |
392 /// For logging errors. | 406 /// For logging errors. |
393 SourceSpan _getSpan(AstNode node, [String contents]) => | 407 SourceSpan _getSpan(AstNode node, [String contents]) => |
394 _getSourceFile(contents).span(node.offset, node.end); | 408 _getSourceFile(contents).span(node.offset, node.end); |
395 /// For logging errors. | 409 /// For logging errors. |
396 SourceFile _getSourceFile([String contents]) { | 410 SourceFile _getSourceFile([String contents]) { |
397 var uri = getSourceUri(); | 411 var uri = getSourceUri(); |
398 var path = uri != null ? uri.toString() : assetId.path; | 412 var path = uri != null ? uri.toString() : assetId.path; |
399 return new SourceFile(contents != null ? contents : rawContents, url: path); | 413 return new SourceFile(contents != null ? contents : rawContents, url: path); |
400 } | 414 } |
401 | 415 |
(...skipping 13 matching lines...) Expand all Loading... |
415 return Uri.parse('package:${assetId.package}/${assetId.path.substring(4)}'); | 429 return Uri.parse('package:${assetId.package}/${assetId.path.substring(4)}'); |
416 } | 430 } |
417 } | 431 } |
418 | 432 |
419 /// Implementation of Analyzer's UriResolver for Barback based assets. | 433 /// Implementation of Analyzer's UriResolver for Barback based assets. |
420 class _AssetUriResolver implements UriResolver { | 434 class _AssetUriResolver implements UriResolver { |
421 final ResolverImpl _resolver; | 435 final ResolverImpl _resolver; |
422 _AssetUriResolver(this._resolver); | 436 _AssetUriResolver(this._resolver); |
423 | 437 |
424 Source resolveAbsolute(Uri uri) { | 438 Source resolveAbsolute(Uri uri) { |
425 var assetId = _resolve(null, uri.toString(), logger, null); | 439 assert(uri.scheme != 'dart'); |
426 if (assetId == null) { | 440 var assetId; |
427 logger.error('Unable to resolve asset ID for "$uri"'); | 441 if (uri.scheme == 'asset') { |
428 return null; | 442 var parts = path.split(uri.path); |
| 443 assetId = new AssetId(parts[0], path.joinAll(parts.skip(1))); |
| 444 } else { |
| 445 assetId = _resolve(null, uri.toString(), logger, null); |
| 446 if (assetId == null) { |
| 447 logger.error('Unable to resolve asset ID for "$uri"'); |
| 448 return null; |
| 449 } |
429 } | 450 } |
430 var source = _resolver.sources[assetId]; | 451 var source = _resolver.sources[assetId]; |
431 // Analyzer expects that sources which are referenced but do not exist yet | 452 // Analyzer expects that sources which are referenced but do not exist yet |
432 // still exist, so just make an empty source. | 453 // still exist, so just make an empty source. |
433 if (source == null) { | 454 if (source == null) { |
434 source = new _AssetBasedSource(assetId, _resolver); | 455 source = new _AssetBasedSource(assetId, _resolver); |
435 _resolver.sources[assetId] = source; | 456 _resolver.sources[assetId] = source; |
436 } | 457 } |
437 return source; | 458 return source; |
438 } | 459 } |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 | 560 |
540 void apply(ChangeSet changeSet) { | 561 void apply(ChangeSet changeSet) { |
541 if (!source.updateContents(content)) return; | 562 if (!source.updateContents(content)) return; |
542 if (source._revision == 1 && source._contents != null) { | 563 if (source._revision == 1 && source._contents != null) { |
543 changeSet.addedSource(source); | 564 changeSet.addedSource(source); |
544 } else { | 565 } else { |
545 changeSet.changedSource(source); | 566 changeSet.changedSource(source); |
546 } | 567 } |
547 } | 568 } |
548 } | 569 } |
OLD | NEW |