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 | 2 |
3 // for details. All rights reserved. Use of this source code is governed by a | 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. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 import 'dart:io' show Directory, File; | |
6 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; | 7 import 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; |
7 import 'package:analyzer/file_system/physical_file_system.dart' | 8 import 'package:analyzer/file_system/physical_file_system.dart' |
8 show PhysicalResourceProvider; | 9 show PhysicalResourceProvider; |
9 import 'package:args/args.dart' show ArgParser, ArgResults; | 10 import 'package:args/args.dart' show ArgParser, ArgResults; |
10 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | 11 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; |
11 import 'package:analyzer/src/generated/engine.dart' | 12 import 'package:analyzer/src/generated/engine.dart' |
12 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; | 13 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; |
13 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 14 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
14 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 15 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
15 import 'package:analyzer/src/generated/source_io.dart' | 16 import 'package:analyzer/src/generated/source_io.dart' |
16 show | 17 show |
17 CustomUriResolver, | 18 CustomUriResolver, |
18 DartUriResolver, | 19 DartUriResolver, |
19 PackageUriResolver, | 20 PackageUriResolver, |
20 SourceFactory, | 21 SourceFactory, |
21 UriResolver; | 22 UriResolver; |
22 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 23 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
23 show | 24 show |
24 InSummaryPackageUriResolver, | 25 InSummaryPackageUriResolver, |
25 InputPackagesResultProvider, | 26 InputPackagesResultProvider, |
26 SummaryDataStore; | 27 SummaryDataStore; |
27 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; | 28 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; |
28 import 'package:cli_util/cli_util.dart' show getSdkDir; | 29 import 'package:cli_util/cli_util.dart' show getSdkDir; |
30 import 'package:package_config/discovery.dart' show findPackagesFromFile; | |
31 import 'package:package_config/packages.dart' show Packages; | |
32 import 'package:package_config/packages_file.dart' as pkgfile show parse; | |
33 import 'package:package_config/src/packages_impl.dart'; | |
29 import 'package:path/path.dart' as path; | 34 import 'package:path/path.dart' as path; |
30 | 35 |
31 import 'multi_package_resolver.dart' show MultiPackageResolver; | 36 import 'multi_package_resolver.dart' show MultiPackageResolver; |
32 | 37 |
33 /// Options used to set up Source URI resolution in the analysis context. | 38 /// Options used to set up Source URI resolution in the analysis context. |
34 class AnalyzerOptions { | 39 class AnalyzerOptions { |
35 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" | 40 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" |
36 final Map<String, String> customUrlMappings; | 41 final Map<String, String> customUrlMappings; |
37 | 42 |
38 /// Package root when resolving 'package:' urls the standard way. | 43 /// Package root directory when resolving 'package:' urls, mutually exclusive |
44 /// with [packageConfig]. | |
39 final String packageRoot; | 45 final String packageRoot; |
40 | 46 |
47 /// Package spec file, typically called `.packages`, mutually exclusive with | |
48 /// [packageRoot]. | |
49 final String packageConfig; | |
50 | |
41 /// List of summary file paths. | 51 /// List of summary file paths. |
42 final List<String> summaryPaths; | 52 final List<String> summaryPaths; |
43 | 53 |
44 /// List of paths used for the multi-package resolver. | 54 /// List of paths used for the multi-package resolver. |
45 final List<String> packagePaths; | 55 final List<String> packagePaths; |
46 | 56 |
47 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't | 57 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't |
48 /// be determined | 58 /// be determined |
49 final String dartSdkPath; | 59 final String dartSdkPath; |
50 | 60 |
51 /// Path to the dart-sdk summary. If this is set, it will be used in favor | 61 /// Path to the dart-sdk summary. If this is set, it will be used in favor |
52 /// of the unsummarized one. | 62 /// of the unsummarized one. |
53 final String dartSdkSummaryPath; | 63 final String dartSdkSummaryPath; |
54 | 64 |
55 AnalyzerOptions( | 65 AnalyzerOptions( |
56 {this.summaryPaths: const [], | 66 {this.summaryPaths: const [], |
57 String dartSdkPath, | 67 String dartSdkPath, |
58 this.dartSdkSummaryPath, | 68 this.dartSdkSummaryPath, |
59 this.customUrlMappings: const {}, | 69 this.customUrlMappings: const {}, |
60 this.packageRoot: 'packages/', | 70 this.packageRoot, |
71 this.packageConfig, | |
61 this.packagePaths: const []}) | 72 this.packagePaths: const []}) |
62 : dartSdkPath = dartSdkPath ?? getSdkDir().path; | 73 : dartSdkPath = dartSdkPath ?? getSdkDir().path; |
63 | 74 |
64 AnalyzerOptions.fromArguments(ArgResults args) | 75 AnalyzerOptions.fromArguments(ArgResults args) |
65 : summaryPaths = args['summary'] as List<String>, | 76 : summaryPaths = args['summary'] as List<String>, |
66 dartSdkPath = args['dart-sdk'] ?? getSdkDir().path, | 77 dartSdkPath = args['dart-sdk'] ?? getSdkDir().path, |
67 dartSdkSummaryPath = args['dart-sdk-summary'], | 78 dartSdkSummaryPath = args['dart-sdk-summary'], |
68 customUrlMappings = _parseUrlMappings(args['url-mapping']), | 79 customUrlMappings = _parseUrlMappings(args['url-mapping']), |
69 packageRoot = args['package-root'], | 80 packageRoot = args['package-root'], |
81 packageConfig = args['packages'], | |
70 packagePaths = (args['package-paths'] as String)?.split(',') ?? []; | 82 packagePaths = (args['package-paths'] as String)?.split(',') ?? []; |
71 | 83 |
72 /// Whether to resolve 'package:' uris using the multi-package resolver. | 84 /// Whether to resolve 'package:' uris using the multi-package resolver. |
73 bool get useMultiPackage => packagePaths.isNotEmpty; | 85 bool get useMultiPackage => packagePaths.isNotEmpty; |
74 | 86 |
75 static ArgParser addArguments(ArgParser parser) { | 87 static ArgParser addArguments(ArgParser parser) { |
76 return parser | 88 return parser |
77 ..addOption('summary', | 89 ..addOption('summary', |
78 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) | 90 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) |
79 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) | 91 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) |
80 ..addOption('dart-sdk-summary', | 92 ..addOption('dart-sdk-summary', |
81 help: 'Dart SDK Summary Path', defaultsTo: null) | 93 help: 'Dart SDK Summary Path', defaultsTo: null) |
94 ..addOption('packages', | |
95 help: 'Path to the package resolution configuration file, which ' | |
96 'supplies a mapping of package names to paths. This option ' | |
97 'cannot be used with --package-root.') | |
nweiz
2016/07/27 23:15:11
It would be good to explicitly throw an error if b
| |
82 ..addOption('package-root', | 98 ..addOption('package-root', |
83 abbr: 'p', | 99 abbr: 'p', |
84 help: 'Package root to resolve "package:" imports', | 100 help: 'Path to a package root directory (deprecated). This option ' |
85 defaultsTo: 'packages/') | 101 'cannot be used with --packages.') |
86 ..addOption('url-mapping', | 102 ..addOption('url-mapping', |
87 help: '--url-mapping=libraryUri,/path/to/library.dart uses \n' | 103 help: '--url-mapping=libraryUri,/path/to/library.dart uses \n' |
88 'library.dart as the source for an import of of "libraryUri".', | 104 'library.dart as the source for an import of of "libraryUri".', |
89 allowMultiple: true, | 105 allowMultiple: true, |
90 splitCommas: false) | 106 splitCommas: false) |
91 ..addOption('package-paths', | 107 ..addOption('package-paths', |
92 help: 'use a list of directories to resolve "package:" imports'); | 108 help: 'use a list of directories to resolve "package:" imports'); |
93 } | 109 } |
94 | 110 |
95 static Map<String, String> _parseUrlMappings(Iterable argument) { | 111 static Map<String, String> _parseUrlMappings(Iterable argument) { |
96 var mappings = <String, String>{}; | 112 var mappings = <String, String>{}; |
97 for (var mapping in argument) { | 113 for (var mapping in argument) { |
98 var splitMapping = mapping.split(','); | 114 var splitMapping = mapping.split(','); |
99 if (splitMapping.length >= 2) { | 115 if (splitMapping.length >= 2) { |
100 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); | 116 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); |
101 } | 117 } |
102 } | 118 } |
103 return mappings; | 119 return mappings; |
104 } | 120 } |
105 } | 121 } |
106 | 122 |
107 /// Creates an [AnalysisContext] with dev_compiler type rules and inference, | 123 /// Creates an [AnalysisContext] with dev_compiler type rules and inference, |
108 /// using [createSourceFactory] to set up its [SourceFactory]. | 124 /// using [createSourceFactory] to set up its [SourceFactory]. |
109 AnalysisContext createAnalysisContextWithSources(AnalyzerOptions options, | 125 AnalysisContext createAnalysisContextWithSources(AnalyzerOptions options, |
110 {DartUriResolver sdkResolver, List<UriResolver> fileResolvers}) { | 126 {DartUriResolver sdkResolver, |
127 List<UriResolver> fileResolvers, | |
128 Packages packages}) { | |
111 AnalysisEngine.instance.processRequiredPlugins(); | 129 AnalysisEngine.instance.processRequiredPlugins(); |
112 | 130 |
113 sdkResolver ??= | 131 sdkResolver ??= |
114 createSdkPathResolver(options.dartSdkSummaryPath, options.dartSdkPath); | 132 createSdkPathResolver(options.dartSdkSummaryPath, options.dartSdkPath); |
115 | 133 |
116 // Read the summaries. | 134 // Read the summaries. |
117 SummaryDataStore summaryData; | 135 SummaryDataStore summaryData; |
118 if (options.summaryPaths.isNotEmpty) { | 136 if (options.summaryPaths.isNotEmpty) { |
119 summaryData = new SummaryDataStore(options.summaryPaths); | 137 summaryData = new SummaryDataStore(options.summaryPaths); |
120 } | 138 } |
121 | 139 |
122 var srcFactory = _createSourceFactory(options, | 140 var srcFactory = _createSourceFactory(options, |
123 sdkResolver: sdkResolver, | 141 sdkResolver: sdkResolver, |
124 fileResolvers: fileResolvers, | 142 fileResolvers: fileResolvers, |
143 packages: packages, | |
125 summaryData: summaryData); | 144 summaryData: summaryData); |
126 | 145 |
127 var context = createAnalysisContext(); | 146 var context = createAnalysisContext(); |
128 context.sourceFactory = srcFactory; | 147 context.sourceFactory = srcFactory; |
129 if (summaryData != null) { | 148 if (summaryData != null) { |
130 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; | 149 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; |
131 context.resultProvider = | 150 context.resultProvider = |
132 new InputPackagesResultProvider(context, summaryData); | 151 new InputPackagesResultProvider(context, summaryData); |
133 } | 152 } |
134 return context; | 153 return context; |
(...skipping 11 matching lines...) Expand all Loading... | |
146 /// Creates a SourceFactory configured by the [options]. | 165 /// Creates a SourceFactory configured by the [options]. |
147 /// | 166 /// |
148 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] | 167 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] |
149 /// to entirely override the DartUriResolver. | 168 /// to entirely override the DartUriResolver. |
150 /// | 169 /// |
151 /// If supplied, [fileResolvers] will override the default `file:` and | 170 /// If supplied, [fileResolvers] will override the default `file:` and |
152 /// `package:` URI resolvers. | 171 /// `package:` URI resolvers. |
153 SourceFactory _createSourceFactory(AnalyzerOptions options, | 172 SourceFactory _createSourceFactory(AnalyzerOptions options, |
154 {DartUriResolver sdkResolver, | 173 {DartUriResolver sdkResolver, |
155 List<UriResolver> fileResolvers, | 174 List<UriResolver> fileResolvers, |
175 Packages packages, | |
156 SummaryDataStore summaryData}) { | 176 SummaryDataStore summaryData}) { |
157 var resolvers = <UriResolver>[]; | 177 var resolvers = <UriResolver>[]; |
158 if (options.customUrlMappings.isNotEmpty) { | 178 if (options.customUrlMappings.isNotEmpty) { |
159 resolvers.add(new CustomUriResolver(options.customUrlMappings)); | 179 resolvers.add(new CustomUriResolver(options.customUrlMappings)); |
160 } | 180 } |
161 resolvers.add(sdkResolver); | 181 resolvers.add(sdkResolver); |
162 if (summaryData != null) { | 182 if (summaryData != null) { |
163 resolvers.add(new InSummaryPackageUriResolver(summaryData)); | 183 resolvers.add(new InSummaryPackageUriResolver(summaryData)); |
164 } | 184 } |
165 | 185 |
166 if (fileResolvers == null) fileResolvers = createFileResolvers(options); | 186 if (fileResolvers == null) { |
167 resolvers.addAll(fileResolvers); | 187 resolvers.add(new ResourceUriResolver(PhysicalResourceProvider.INSTANCE)); |
168 return new SourceFactory(resolvers); | 188 } else { |
169 } | 189 resolvers.addAll(fileResolvers); |
170 | 190 } |
171 List<UriResolver> createFileResolvers(AnalyzerOptions options) { | 191 if (packages == null) { |
172 return [ | 192 if (options.useMultiPackage) { |
173 new ResourceUriResolver(PhysicalResourceProvider.INSTANCE), | 193 resolvers.add(new MultiPackageResolver(options.packagePaths)); |
174 options.useMultiPackage | 194 } else if (options.packageConfig != null) { |
175 ? new MultiPackageResolver(options.packagePaths) | 195 var f = new File(options.packageConfig).absolute; |
176 : new PackageUriResolver([new JavaFile(options.packageRoot)]) | 196 packages = new MapPackages(pkgfile.parse(f.readAsBytesSync(), f.uri)); |
177 ]; | 197 } else if (options.packageRoot != null) { |
198 resolvers | |
199 .add(new PackageUriResolver([new JavaFile(options.packageRoot)])); | |
200 } else { | |
201 // TODO(jmesserly): should we use the input file paths or base directory | |
202 // instead? | |
203 packages = findPackagesFromFile(Directory.current.uri); | |
Jennifer Messerly
2016/07/27 22:56:16
is using .packages the right default behavior now?
nweiz
2016/07/27 23:15:11
I believe the default is:
* If a packages/ direct
Brian Wilkerson
2016/07/29 17:07:40
Almost. A '.packages' file in the initial director
| |
204 } | |
205 } | |
206 return new SourceFactory(resolvers, packages); | |
178 } | 207 } |
179 | 208 |
180 DirectoryBasedDartSdk _createDirectoryBasedDartSdk(String sdkPath) { | 209 DirectoryBasedDartSdk _createDirectoryBasedDartSdk(String sdkPath) { |
181 var sdk = new DirectoryBasedDartSdk( | 210 var sdk = new DirectoryBasedDartSdk( |
182 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true); | 211 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true); |
183 sdk.useSummary = true; | 212 sdk.useSummary = true; |
184 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; | 213 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; |
185 return sdk; | 214 return sdk; |
186 } | 215 } |
187 | 216 |
188 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. | 217 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. |
189 DartUriResolver createSdkPathResolver(String sdkSummaryPath, String sdkPath) { | 218 DartUriResolver createSdkPathResolver(String sdkSummaryPath, String sdkPath) { |
190 var sdk = (sdkSummaryPath != null) | 219 var sdk = (sdkSummaryPath != null) |
191 ? new SummaryBasedDartSdk(sdkSummaryPath, true) | 220 ? new SummaryBasedDartSdk(sdkSummaryPath, true) |
192 : _createDirectoryBasedDartSdk(sdkPath); | 221 : _createDirectoryBasedDartSdk(sdkPath); |
193 return new DartUriResolver(sdk); | 222 return new DartUriResolver(sdk); |
194 } | 223 } |
OLD | NEW |