OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 |
| 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. |
| 5 |
| 6 import 'package:args/args.dart' show ArgParser, ArgResults; |
| 7 import 'package:analyzer/src/generated/engine.dart' |
| 8 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; |
| 9 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 10 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
| 11 import 'package:analyzer/src/generated/source_io.dart' |
| 12 show |
| 13 CustomUriResolver, |
| 14 DartUriResolver, |
| 15 FileUriResolver, |
| 16 PackageUriResolver, |
| 17 SourceFactory, |
| 18 UriResolver; |
| 19 import 'package:cli_util/cli_util.dart' show getSdkDir; |
| 20 |
| 21 import 'dart_sdk.dart' show MockDartSdk, mockSdkSources; |
| 22 import 'multi_package_resolver.dart' show MultiPackageResolver; |
| 23 |
| 24 /// Options used to set up Source URI resolution in the analysis context. |
| 25 class AnalyzerOptions { |
| 26 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" |
| 27 final Map<String, String> customUrlMappings; |
| 28 |
| 29 /// Package root when resolving 'package:' urls the standard way. |
| 30 final String packageRoot; |
| 31 |
| 32 /// List of summary file paths. |
| 33 final List<String> summaryPaths; |
| 34 |
| 35 /// List of paths used for the multi-package resolver. |
| 36 final List<String> packagePaths; |
| 37 |
| 38 /// Whether to use a mock-sdk during compilation. |
| 39 final bool useMockSdk; |
| 40 |
| 41 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't |
| 42 /// be determined |
| 43 final String dartSdkPath; |
| 44 |
| 45 AnalyzerOptions( |
| 46 {this.summaryPaths: const [], |
| 47 this.useMockSdk: false, |
| 48 String dartSdkPath, |
| 49 this.customUrlMappings: const {}, |
| 50 this.packageRoot: 'packages/', |
| 51 this.packagePaths: const []}) |
| 52 : dartSdkPath = dartSdkPath ?? getSdkDir().path; |
| 53 |
| 54 AnalyzerOptions.fromArguments(ArgResults args) |
| 55 : summaryPaths = args['summary'], |
| 56 useMockSdk = false, |
| 57 dartSdkPath = args['dart-sdk'] ?? getSdkDir().path, |
| 58 customUrlMappings = _parseUrlMappings(args['url-mapping']), |
| 59 packageRoot = args['package-root'], |
| 60 packagePaths = args['package-paths']?.split(',') ?? []; |
| 61 |
| 62 /// Whether to resolve 'package:' uris using the multi-package resolver. |
| 63 bool get useMultiPackage => packagePaths.isNotEmpty; |
| 64 |
| 65 static ArgParser addArguments(ArgParser parser) { |
| 66 return parser |
| 67 ..addOption('summary', |
| 68 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) |
| 69 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) |
| 70 ..addOption('package-root', |
| 71 abbr: 'p', |
| 72 help: 'Package root to resolve "package:" imports', |
| 73 defaultsTo: 'packages/') |
| 74 ..addOption('url-mapping', |
| 75 help: '--url-mapping=libraryUri,/path/to/library.dart uses \n' |
| 76 'library.dart as the source for an import of of "libraryUri".', |
| 77 allowMultiple: true, |
| 78 splitCommas: false) |
| 79 ..addOption('package-paths', |
| 80 help: 'use a list of directories to resolve "package:" imports'); |
| 81 } |
| 82 |
| 83 static Map<String, String> _parseUrlMappings(Iterable argument) { |
| 84 var mappings = <String, String>{}; |
| 85 for (var mapping in argument) { |
| 86 var splitMapping = mapping.split(','); |
| 87 if (splitMapping.length >= 2) { |
| 88 mappings[splitMapping[0]] = splitMapping[1]; |
| 89 } |
| 90 } |
| 91 return mappings; |
| 92 } |
| 93 } |
| 94 |
| 95 /// Creates an [AnalysisContext] with dev_compiler type rules and inference, |
| 96 /// using [createSourceFactory] to set up its [SourceFactory]. |
| 97 AnalysisContext createAnalysisContextWithSources(AnalyzerOptions options, |
| 98 {DartUriResolver sdkResolver, List<UriResolver> fileResolvers}) { |
| 99 AnalysisEngine.instance.processRequiredPlugins(); |
| 100 var srcFactory = createSourceFactory(options, |
| 101 sdkResolver: sdkResolver, fileResolvers: fileResolvers); |
| 102 return createAnalysisContext()..sourceFactory = srcFactory; |
| 103 } |
| 104 |
| 105 /// Creates an analysis context that contains our restricted typing rules. |
| 106 AnalysisContext createAnalysisContext() { |
| 107 var res = AnalysisEngine.instance.createAnalysisContext(); |
| 108 res.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; |
| 109 return res; |
| 110 } |
| 111 |
| 112 /// Creates a SourceFactory configured by the [options]. |
| 113 /// |
| 114 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] |
| 115 /// to entirely override the DartUriResolver. |
| 116 /// |
| 117 /// If supplied, [fileResolvers] will override the default `file:` and |
| 118 /// `package:` URI resolvers. |
| 119 SourceFactory createSourceFactory(AnalyzerOptions options, |
| 120 {DartUriResolver sdkResolver, List<UriResolver> fileResolvers}) { |
| 121 sdkResolver ??= options.useMockSdk |
| 122 ? createMockSdkResolver(mockSdkSources) |
| 123 : createSdkPathResolver(options.dartSdkPath); |
| 124 |
| 125 var resolvers = <UriResolver>[]; |
| 126 if (options.customUrlMappings.isNotEmpty) { |
| 127 resolvers.add(new CustomUriResolver(options.customUrlMappings)); |
| 128 } |
| 129 resolvers.add(sdkResolver); |
| 130 if (fileResolvers == null) fileResolvers = createFileResolvers(options); |
| 131 resolvers.addAll(fileResolvers); |
| 132 return new SourceFactory(resolvers); |
| 133 } |
| 134 |
| 135 List<UriResolver> createFileResolvers(AnalyzerOptions options) { |
| 136 return [ |
| 137 new FileUriResolver(), |
| 138 options.useMultiPackage |
| 139 ? new MultiPackageResolver(options.packagePaths) |
| 140 : new PackageUriResolver([new JavaFile(options.packageRoot)]) |
| 141 ]; |
| 142 } |
| 143 |
| 144 /// Creates a [DartUriResolver] that uses a mock 'dart:' library contents. |
| 145 DartUriResolver createMockSdkResolver(Map<String, String> mockSources) => |
| 146 new MockDartSdk(mockSources, reportMissing: true).resolver; |
| 147 |
| 148 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. |
| 149 DartUriResolver createSdkPathResolver(String sdkPath) { |
| 150 var sdk = new DirectoryBasedDartSdk( |
| 151 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true); |
| 152 sdk.useSummary = true; |
| 153 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; |
| 154 return new DartUriResolver(sdk); |
| 155 } |
OLD | NEW |