OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analyzer_cli.src.driver; | 5 library analyzer_cli.src.driver; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'package:analyzer/file_system/file_system.dart' as file_system; | 11 import 'package:analyzer/file_system/file_system.dart' as file_system; |
12 import 'package:analyzer/file_system/physical_file_system.dart'; | 12 import 'package:analyzer/file_system/physical_file_system.dart'; |
13 import 'package:analyzer/plugin/options.dart'; | 13 import 'package:analyzer/plugin/options.dart'; |
14 import 'package:analyzer/plugin/resolver_provider.dart'; | 14 import 'package:analyzer/plugin/resolver_provider.dart'; |
15 import 'package:analyzer/source/analysis_options_provider.dart'; | 15 import 'package:analyzer/source/analysis_options_provider.dart'; |
16 import 'package:analyzer/source/embedder.dart'; | |
17 import 'package:analyzer/source/package_map_provider.dart'; | 16 import 'package:analyzer/source/package_map_provider.dart'; |
18 import 'package:analyzer/source/package_map_resolver.dart'; | 17 import 'package:analyzer/source/package_map_resolver.dart'; |
19 import 'package:analyzer/source/pub_package_map_provider.dart'; | 18 import 'package:analyzer/source/pub_package_map_provider.dart'; |
20 import 'package:analyzer/source/sdk_ext.dart'; | 19 import 'package:analyzer/source/sdk_ext.dart'; |
| 20 import 'package:analyzer/src/context/builder.dart'; |
| 21 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
21 import 'package:analyzer/src/generated/constant.dart'; | 22 import 'package:analyzer/src/generated/constant.dart'; |
22 import 'package:analyzer/src/generated/engine.dart'; | 23 import 'package:analyzer/src/generated/engine.dart'; |
23 import 'package:analyzer/src/generated/error.dart'; | 24 import 'package:analyzer/src/generated/error.dart'; |
24 import 'package:analyzer/src/generated/interner.dart'; | 25 import 'package:analyzer/src/generated/interner.dart'; |
25 import 'package:analyzer/src/generated/java_engine.dart'; | 26 import 'package:analyzer/src/generated/java_engine.dart'; |
26 import 'package:analyzer/src/generated/java_io.dart'; | 27 import 'package:analyzer/src/generated/java_io.dart'; |
27 import 'package:analyzer/src/generated/sdk.dart'; | 28 import 'package:analyzer/src/generated/sdk.dart'; |
28 import 'package:analyzer/src/generated/sdk_io.dart'; | 29 import 'package:analyzer/src/generated/sdk_io.dart'; |
29 import 'package:analyzer/src/generated/source.dart'; | 30 import 'package:analyzer/src/generated/source.dart'; |
30 import 'package:analyzer/src/generated/source_io.dart'; | 31 import 'package:analyzer/src/generated/source_io.dart'; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 CommandLineOptions _previousOptions; | 88 CommandLineOptions _previousOptions; |
88 | 89 |
89 IncrementalAnalysisSession incrementalSession; | 90 IncrementalAnalysisSession incrementalSession; |
90 | 91 |
91 @override | 92 @override |
92 ResolverProvider packageResolverProvider; | 93 ResolverProvider packageResolverProvider; |
93 | 94 |
94 /// SDK instance. | 95 /// SDK instance. |
95 DartSdk sdk; | 96 DartSdk sdk; |
96 | 97 |
| 98 /** |
| 99 * The resource provider used to access the file system. |
| 100 */ |
| 101 file_system.ResourceProvider resourceProvider = |
| 102 PhysicalResourceProvider.INSTANCE; |
| 103 |
97 /// Collected analysis statistics. | 104 /// Collected analysis statistics. |
98 final AnalysisStats stats = new AnalysisStats(); | 105 final AnalysisStats stats = new AnalysisStats(); |
99 | 106 |
100 /// This Driver's current analysis context. | 107 /// This Driver's current analysis context. |
101 /// | 108 /// |
102 /// *Visible for testing.* | 109 /// *Visible for testing.* |
103 AnalysisContext get context => _context; | 110 AnalysisContext get context => _context; |
104 | 111 |
105 @override | 112 @override |
106 void set userDefinedPlugins(List<Plugin> plugins) { | 113 void set userDefinedPlugins(List<Plugin> plugins) { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 }; | 338 }; |
332 } | 339 } |
333 | 340 |
334 /// Decide on the appropriate method for resolving URIs based on the given | 341 /// Decide on the appropriate method for resolving URIs based on the given |
335 /// [options] and [customUrlMappings] settings, and return a | 342 /// [options] and [customUrlMappings] settings, and return a |
336 /// [SourceFactory] that has been configured accordingly. | 343 /// [SourceFactory] that has been configured accordingly. |
337 SourceFactory _chooseUriResolutionPolicy(CommandLineOptions options, | 344 SourceFactory _chooseUriResolutionPolicy(CommandLineOptions options, |
338 Map<file_system.Folder, YamlMap> embedderMap, _PackageInfo packageInfo) { | 345 Map<file_system.Folder, YamlMap> embedderMap, _PackageInfo packageInfo) { |
339 // Create a custom package resolver if one has been specified. | 346 // Create a custom package resolver if one has been specified. |
340 if (packageResolverProvider != null) { | 347 if (packageResolverProvider != null) { |
341 file_system.Folder folder = | 348 file_system.Folder folder = resourceProvider.getResource('.'); |
342 PhysicalResourceProvider.INSTANCE.getResource('.'); | |
343 UriResolver resolver = packageResolverProvider(folder); | 349 UriResolver resolver = packageResolverProvider(folder); |
344 if (resolver != null) { | 350 if (resolver != null) { |
345 UriResolver sdkResolver = new DartUriResolver(sdk); | 351 UriResolver sdkResolver = new DartUriResolver(sdk); |
346 | 352 |
347 // TODO(brianwilkerson) This doesn't handle sdk extensions. | 353 // TODO(brianwilkerson) This doesn't handle sdk extensions. |
348 List<UriResolver> resolvers = <UriResolver>[ | 354 List<UriResolver> resolvers = <UriResolver>[ |
349 sdkResolver, | 355 sdkResolver, |
350 resolver, | 356 resolver, |
351 new file_system.ResourceUriResolver(PhysicalResourceProvider.INSTANCE) | 357 new file_system.ResourceUriResolver(resourceProvider) |
352 ]; | 358 ]; |
353 return new SourceFactory(resolvers); | 359 return new SourceFactory(resolvers); |
354 } | 360 } |
355 } | 361 } |
356 | 362 |
357 UriResolver packageUriResolver; | 363 UriResolver packageUriResolver; |
358 | 364 |
359 if (options.packageRootPath != null) { | 365 if (options.packageRootPath != null) { |
360 JavaFile packageDirectory = new JavaFile(options.packageRootPath); | 366 JavaFile packageDirectory = new JavaFile(options.packageRootPath); |
361 packageUriResolver = new PackageUriResolver([packageDirectory]); | 367 packageUriResolver = new PackageUriResolver([packageDirectory]); |
362 } else if (options.packageConfigPath == null) { | 368 } else if (options.packageConfigPath == null) { |
363 // TODO(pq): remove? | 369 // TODO(pq): remove? |
364 if (packageInfo.packageMap == null) { | 370 if (packageInfo.packageMap == null) { |
365 // Fall back to pub list-package-dirs. | 371 // Fall back to pub list-package-dirs. |
366 PubPackageMapProvider pubPackageMapProvider = | 372 PubPackageMapProvider pubPackageMapProvider = |
367 new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk); | 373 new PubPackageMapProvider(resourceProvider, sdk); |
368 file_system.Resource cwd = | 374 file_system.Resource cwd = resourceProvider.getResource('.'); |
369 PhysicalResourceProvider.INSTANCE.getResource('.'); | |
370 PackageMapInfo packageMapInfo = | 375 PackageMapInfo packageMapInfo = |
371 pubPackageMapProvider.computePackageMap(cwd); | 376 pubPackageMapProvider.computePackageMap(cwd); |
372 Map<String, List<file_system.Folder>> packageMap = | 377 Map<String, List<file_system.Folder>> packageMap = |
373 packageMapInfo.packageMap; | 378 packageMapInfo.packageMap; |
374 | 379 |
375 // Only create a packageUriResolver if pub list-package-dirs succeeded. | 380 // Only create a packageUriResolver if pub list-package-dirs succeeded. |
376 // If it failed, that's not a problem; it simply means we have no way | 381 // If it failed, that's not a problem; it simply means we have no way |
377 // to resolve packages. | 382 // to resolve packages. |
378 if (packageMapInfo.packageMap != null) { | 383 if (packageMapInfo.packageMap != null) { |
379 packageUriResolver = new PackageMapUriResolver( | 384 packageUriResolver = |
380 PhysicalResourceProvider.INSTANCE, packageMap); | 385 new PackageMapUriResolver(resourceProvider, packageMap); |
381 } | 386 } |
382 } | 387 } |
383 } | 388 } |
384 | 389 |
385 // Now, build our resolver list. | 390 // Now, build our resolver list. |
386 List<UriResolver> resolvers = []; | 391 List<UriResolver> resolvers = []; |
387 | 392 |
388 // 'dart:' URIs come first. | 393 // 'dart:' URIs come first. |
389 | 394 |
390 // Setup embedding. | 395 // Setup embedding. |
391 EmbedderSdk embedderSdk = new EmbedderSdk(embedderMap); | 396 EmbedderSdk embedderSdk = new EmbedderSdk(resourceProvider, embedderMap); |
392 if (embedderSdk.libraryMap.size() == 0) { | 397 if (embedderSdk.libraryMap.size() == 0) { |
393 // The embedder uri resolver has no mappings. Use the default Dart SDK | 398 // The embedder uri resolver has no mappings. Use the default Dart SDK |
394 // uri resolver. | 399 // uri resolver. |
395 resolvers.add(new DartUriResolver(sdk)); | 400 resolvers.add(new DartUriResolver(sdk)); |
396 } else { | 401 } else { |
397 // The embedder uri resolver has mappings, use it instead of the default | 402 // The embedder uri resolver has mappings, use it instead of the default |
398 // Dart SDK uri resolver. | 403 // Dart SDK uri resolver. |
399 resolvers.add(new DartUriResolver(embedderSdk)); | 404 resolvers.add(new DartUriResolver(embedderSdk)); |
400 } | 405 } |
401 | 406 |
402 // Next SdkExts. | 407 // Next SdkExts. |
403 if (packageInfo.packageMap != null) { | 408 if (packageInfo.packageMap != null) { |
404 resolvers.add(new SdkExtUriResolver(packageInfo.packageMap)); | 409 resolvers.add(new SdkExtUriResolver(packageInfo.packageMap)); |
405 } | 410 } |
406 | 411 |
407 // Then package URIs. | 412 // Then package URIs. |
408 if (packageUriResolver != null) { | 413 if (packageUriResolver != null) { |
409 resolvers.add(packageUriResolver); | 414 resolvers.add(packageUriResolver); |
410 } | 415 } |
411 | 416 |
412 // Finally files. | 417 // Finally files. |
413 resolvers.add( | 418 resolvers.add(new file_system.ResourceUriResolver(resourceProvider)); |
414 new file_system.ResourceUriResolver(PhysicalResourceProvider.INSTANCE)); | |
415 | 419 |
416 return new SourceFactory(resolvers, packageInfo.packages); | 420 return new SourceFactory(resolvers, packageInfo.packages); |
417 } | 421 } |
418 | 422 |
419 /// Collect all analyzable files at [filePath], recursively if it's a | 423 /// Collect all analyzable files at [filePath], recursively if it's a |
420 /// directory, ignoring links. | 424 /// directory, ignoring links. |
421 Iterable<File> _collectFiles(String filePath) { | 425 Iterable<File> _collectFiles(String filePath) { |
422 List<File> files = <File>[]; | 426 List<File> files = <File>[]; |
423 File file = new File(filePath); | 427 File file = new File(filePath); |
424 if (file.existsSync()) { | 428 if (file.existsSync()) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 packages = new MapPackages(map); | 543 packages = new MapPackages(map); |
540 packageMap = _getPackageMap(packages); | 544 packageMap = _getPackageMap(packages); |
541 } catch (e) { | 545 } catch (e) { |
542 printAndFail( | 546 printAndFail( |
543 'Unable to read package config data from $packageConfigPath: $e'); | 547 'Unable to read package config data from $packageConfigPath: $e'); |
544 } | 548 } |
545 } else if (options.packageRootPath != null) { | 549 } else if (options.packageRootPath != null) { |
546 packageMap = _PackageRootPackageMapBuilder | 550 packageMap = _PackageRootPackageMapBuilder |
547 .buildPackageMap(options.packageRootPath); | 551 .buildPackageMap(options.packageRootPath); |
548 } else { | 552 } else { |
549 file_system.Resource cwd = | 553 file_system.Resource cwd = resourceProvider.getResource('.'); |
550 PhysicalResourceProvider.INSTANCE.getResource('.'); | |
551 // Look for .packages. | 554 // Look for .packages. |
552 packages = _discoverPackagespec(new Uri.directory(cwd.path)); | 555 packages = _discoverPackagespec(new Uri.directory(cwd.path)); |
553 packageMap = _getPackageMap(packages); | 556 packageMap = _getPackageMap(packages); |
554 } | 557 } |
555 | 558 |
556 return new _PackageInfo(packages, packageMap); | 559 return new _PackageInfo(packages, packageMap); |
557 } | 560 } |
558 | 561 |
559 Map<String, List<file_system.Folder>> _getPackageMap(Packages packages) { | 562 Map<String, List<file_system.Folder>> _getPackageMap(Packages packages) { |
560 if (packages == null) { | 563 if (packages == null) { |
561 return null; | 564 return null; |
562 } | 565 } |
563 | 566 |
564 Map<String, List<file_system.Folder>> folderMap = | 567 Map<String, List<file_system.Folder>> folderMap = |
565 new Map<String, List<file_system.Folder>>(); | 568 new Map<String, List<file_system.Folder>>(); |
566 packages.asMap().forEach((String packagePath, Uri uri) { | 569 packages.asMap().forEach((String packagePath, Uri uri) { |
567 folderMap[packagePath] = [ | 570 folderMap[packagePath] = [resourceProvider.getFolder(path.fromUri(uri))]; |
568 PhysicalResourceProvider.INSTANCE.getFolder(path.fromUri(uri)) | |
569 ]; | |
570 }); | 571 }); |
571 return folderMap; | 572 return folderMap; |
572 } | 573 } |
573 | 574 |
574 bool _hasSdkExt(Iterable<List<file_system.Folder>> folders) { | 575 bool _hasSdkExt(Iterable<List<file_system.Folder>> folders) { |
575 if (folders != null) { | 576 if (folders != null) { |
576 //TODO: ideally share this traversal with SdkExtUriResolver | 577 //TODO: ideally share this traversal with SdkExtUriResolver |
577 for (Iterable<file_system.Folder> libDirs in folders) { | 578 for (Iterable<file_system.Folder> libDirs in folders) { |
578 if (libDirs.any((file_system.Folder libDir) => | 579 if (libDirs.any((file_system.Folder libDir) => |
579 libDir.getChild(SdkExtUriResolver.SDK_EXT_NAME).exists)) { | 580 libDir.getChild(SdkExtUriResolver.SDK_EXT_NAME).exists)) { |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 for (var package in packages) { | 832 for (var package in packages) { |
832 var packageName = path.basename(package.path); | 833 var packageName = path.basename(package.path); |
833 var realPath = package.resolveSymbolicLinksSync(); | 834 var realPath = package.resolveSymbolicLinksSync(); |
834 result[packageName] = [ | 835 result[packageName] = [ |
835 PhysicalResourceProvider.INSTANCE.getFolder(realPath) | 836 PhysicalResourceProvider.INSTANCE.getFolder(realPath) |
836 ]; | 837 ]; |
837 } | 838 } |
838 return result; | 839 return result; |
839 } | 840 } |
840 } | 841 } |
OLD | NEW |