| 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 15 matching lines...) Expand all Loading... |
| 370 | 372 |
| 371 String get shortName => path.basename(assetId.path); | 373 String get shortName => path.basename(assetId.path); |
| 372 | 374 |
| 373 UriKind get uriKind { | 375 UriKind get uriKind { |
| 374 if (assetId.path.startsWith('lib/')) return UriKind.PACKAGE_URI; | 376 if (assetId.path.startsWith('lib/')) return UriKind.PACKAGE_URI; |
| 375 return UriKind.FILE_URI; | 377 return UriKind.FILE_URI; |
| 376 } | 378 } |
| 377 | 379 |
| 378 bool get isInSystemLibrary => false; | 380 bool get isInSystemLibrary => false; |
| 379 | 381 |
| 380 Source resolveRelative(Uri relativeUri) { | 382 Uri resolveRelative(Uri relativeUri) { |
| 381 var id = _resolve(assetId, relativeUri.toString(), _logger, null); | 383 var id = _resolve(assetId, relativeUri.toString(), _logger, null); |
| 382 if (id == null) return null; | 384 if (id == null) return uri.resolveUri(relativeUri); |
| 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.uri; |
| 390 } | 392 } |
| 391 | 393 |
| 392 /// For logging errors. | 394 /// For logging errors. |
| 393 SourceSpan _getSpan(AstNode node, [String contents]) => | 395 SourceSpan _getSpan(AstNode node, [String contents]) => |
| 394 _getSourceFile(contents).span(node.offset, node.end); | 396 _getSourceFile(contents).span(node.offset, node.end); |
| 395 /// For logging errors. | 397 /// For logging errors. |
| 396 SourceFile _getSourceFile([String contents]) { | 398 SourceFile _getSourceFile([String contents]) { |
| 397 var uri = getSourceUri(); | 399 var uri = getSourceUri(); |
| 398 var path = uri != null ? uri.toString() : assetId.path; | 400 var path = uri != null ? uri.toString() : assetId.path; |
| 399 return new SourceFile(contents != null ? contents : rawContents, url: path); | 401 return new SourceFile(contents != null ? contents : rawContents, url: path); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 415 return Uri.parse('package:${assetId.package}/${assetId.path.substring(4)}'); | 417 return Uri.parse('package:${assetId.package}/${assetId.path.substring(4)}'); |
| 416 } | 418 } |
| 417 } | 419 } |
| 418 | 420 |
| 419 /// Implementation of Analyzer's UriResolver for Barback based assets. | 421 /// Implementation of Analyzer's UriResolver for Barback based assets. |
| 420 class _AssetUriResolver implements UriResolver { | 422 class _AssetUriResolver implements UriResolver { |
| 421 final ResolverImpl _resolver; | 423 final ResolverImpl _resolver; |
| 422 _AssetUriResolver(this._resolver); | 424 _AssetUriResolver(this._resolver); |
| 423 | 425 |
| 424 Source resolveAbsolute(Uri uri) { | 426 Source resolveAbsolute(Uri uri) { |
| 425 var assetId = _resolve(null, uri.toString(), logger, null); | 427 assert(uri.scheme != 'dart'); |
| 426 if (assetId == null) { | 428 var assetId; |
| 427 logger.error('Unable to resolve asset ID for "$uri"'); | 429 if (uri.scheme == 'asset') { |
| 428 return null; | 430 var parts = path.split(uri.path); |
| 431 assetId = new AssetId(parts[0], path.joinAll(parts.skip(1))); |
| 432 } else { |
| 433 assetId = _resolve(null, uri.toString(), logger, null); |
| 434 if (assetId == null) { |
| 435 logger.error('Unable to resolve asset ID for "$uri"'); |
| 436 return null; |
| 437 } |
| 429 } | 438 } |
| 430 var source = _resolver.sources[assetId]; | 439 var source = _resolver.sources[assetId]; |
| 431 // Analyzer expects that sources which are referenced but do not exist yet | 440 // Analyzer expects that sources which are referenced but do not exist yet |
| 432 // still exist, so just make an empty source. | 441 // still exist, so just make an empty source. |
| 433 if (source == null) { | 442 if (source == null) { |
| 434 source = new _AssetBasedSource(assetId, _resolver); | 443 source = new _AssetBasedSource(assetId, _resolver); |
| 435 _resolver.sources[assetId] = source; | 444 _resolver.sources[assetId] = source; |
| 436 } | 445 } |
| 437 return source; | 446 return source; |
| 438 } | 447 } |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 | 548 |
| 540 void apply(ChangeSet changeSet) { | 549 void apply(ChangeSet changeSet) { |
| 541 if (!source.updateContents(content)) return; | 550 if (!source.updateContents(content)) return; |
| 542 if (source._revision == 1 && source._contents != null) { | 551 if (source._revision == 1 && source._contents != null) { |
| 543 changeSet.addedSource(source); | 552 changeSet.addedSource(source); |
| 544 } else { | 553 } else { |
| 545 changeSet.changedSource(source); | 554 changeSet.changedSource(source); |
| 546 } | 555 } |
| 547 } | 556 } |
| 548 } | 557 } |
| OLD | NEW |