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 'package:analyzer/file_system/file_system.dart' show ResourceUriResolver; |
| 7 import 'package:analyzer/file_system/physical_file_system.dart' |
| 8 show PhysicalResourceProvider; |
6 import 'package:args/args.dart' show ArgParser, ArgResults; | 9 import 'package:args/args.dart' show ArgParser, ArgResults; |
7 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; | 10 import 'package:analyzer/src/context/context.dart' show AnalysisContextImpl; |
8 import 'package:analyzer/src/generated/engine.dart' | 11 import 'package:analyzer/src/generated/engine.dart' |
9 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; | 12 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; |
10 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 13 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
11 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 14 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
12 import 'package:analyzer/src/generated/source_io.dart' | 15 import 'package:analyzer/src/generated/source_io.dart' |
13 show | 16 show |
14 CustomUriResolver, | 17 CustomUriResolver, |
15 DartUriResolver, | 18 DartUriResolver, |
16 FileUriResolver, | |
17 PackageUriResolver, | 19 PackageUriResolver, |
18 SourceFactory, | 20 SourceFactory, |
19 UriResolver; | 21 UriResolver; |
20 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 22 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
21 show | 23 show |
22 InSummaryPackageUriResolver, | 24 InSummaryPackageUriResolver, |
23 InputPackagesResultProvider, | 25 InputPackagesResultProvider, |
24 SummaryDataStore; | 26 SummaryDataStore; |
25 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; | 27 import 'package:analyzer/src/summary/summary_sdk.dart' show SummaryBasedDartSdk; |
26 import 'package:cli_util/cli_util.dart' show getSdkDir; | 28 import 'package:cli_util/cli_util.dart' show getSdkDir; |
27 import 'package:path/path.dart' as path; | 29 import 'package:path/path.dart' as path; |
28 | 30 |
29 import 'dart_sdk.dart' show MockDartSdk, mockSdkSources; | |
30 import 'multi_package_resolver.dart' show MultiPackageResolver; | 31 import 'multi_package_resolver.dart' show MultiPackageResolver; |
31 | 32 |
32 /// Options used to set up Source URI resolution in the analysis context. | 33 /// Options used to set up Source URI resolution in the analysis context. |
33 class AnalyzerOptions { | 34 class AnalyzerOptions { |
34 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" | 35 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" |
35 final Map<String, String> customUrlMappings; | 36 final Map<String, String> customUrlMappings; |
36 | 37 |
37 /// Package root when resolving 'package:' urls the standard way. | 38 /// Package root when resolving 'package:' urls the standard way. |
38 final String packageRoot; | 39 final String packageRoot; |
39 | 40 |
40 /// List of summary file paths. | 41 /// List of summary file paths. |
41 final List<String> summaryPaths; | 42 final List<String> summaryPaths; |
42 | 43 |
43 /// List of paths used for the multi-package resolver. | 44 /// List of paths used for the multi-package resolver. |
44 final List<String> packagePaths; | 45 final List<String> packagePaths; |
45 | 46 |
46 /// Whether to use a mock-sdk during compilation. | |
47 final bool useMockSdk; | |
48 | |
49 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't | 47 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't |
50 /// be determined | 48 /// be determined |
51 final String dartSdkPath; | 49 final String dartSdkPath; |
52 | 50 |
53 /// Path to the dart-sdk summary. If this is set, it will be used in favor | 51 /// Path to the dart-sdk summary. If this is set, it will be used in favor |
54 /// of the unsummarized one. | 52 /// of the unsummarized one. |
55 final String dartSdkSummaryPath; | 53 final String dartSdkSummaryPath; |
56 | 54 |
57 AnalyzerOptions( | 55 AnalyzerOptions( |
58 {this.summaryPaths: const [], | 56 {this.summaryPaths: const [], |
59 this.useMockSdk: false, | |
60 String dartSdkPath, | 57 String dartSdkPath, |
61 this.dartSdkSummaryPath, | 58 this.dartSdkSummaryPath, |
62 this.customUrlMappings: const {}, | 59 this.customUrlMappings: const {}, |
63 this.packageRoot: 'packages/', | 60 this.packageRoot: 'packages/', |
64 this.packagePaths: const []}) | 61 this.packagePaths: const []}) |
65 : dartSdkPath = dartSdkPath ?? getSdkDir().path; | 62 : dartSdkPath = dartSdkPath ?? getSdkDir().path; |
66 | 63 |
67 AnalyzerOptions.fromArguments(ArgResults args) | 64 AnalyzerOptions.fromArguments(ArgResults args) |
68 : summaryPaths = args['summary'] as List<String>, | 65 : summaryPaths = args['summary'] as List<String>, |
69 useMockSdk = false, | |
70 dartSdkPath = args['dart-sdk'] ?? getSdkDir().path, | 66 dartSdkPath = args['dart-sdk'] ?? getSdkDir().path, |
71 dartSdkSummaryPath = args['dart-sdk-summary'], | 67 dartSdkSummaryPath = args['dart-sdk-summary'], |
72 customUrlMappings = _parseUrlMappings(args['url-mapping']), | 68 customUrlMappings = _parseUrlMappings(args['url-mapping']), |
73 packageRoot = args['package-root'], | 69 packageRoot = args['package-root'], |
74 packagePaths = (args['package-paths'] as String)?.split(',') ?? []; | 70 packagePaths = (args['package-paths'] as String)?.split(',') ?? []; |
75 | 71 |
76 /// Whether to resolve 'package:' uris using the multi-package resolver. | 72 /// Whether to resolve 'package:' uris using the multi-package resolver. |
77 bool get useMultiPackage => packagePaths.isNotEmpty; | 73 bool get useMultiPackage => packagePaths.isNotEmpty; |
78 | 74 |
79 static ArgParser addArguments(ArgParser parser) { | 75 static ArgParser addArguments(ArgParser parser) { |
(...skipping 27 matching lines...) Expand all Loading... |
107 return mappings; | 103 return mappings; |
108 } | 104 } |
109 } | 105 } |
110 | 106 |
111 /// Creates an [AnalysisContext] with dev_compiler type rules and inference, | 107 /// Creates an [AnalysisContext] with dev_compiler type rules and inference, |
112 /// using [createSourceFactory] to set up its [SourceFactory]. | 108 /// using [createSourceFactory] to set up its [SourceFactory]. |
113 AnalysisContext createAnalysisContextWithSources(AnalyzerOptions options, | 109 AnalysisContext createAnalysisContextWithSources(AnalyzerOptions options, |
114 {DartUriResolver sdkResolver, List<UriResolver> fileResolvers}) { | 110 {DartUriResolver sdkResolver, List<UriResolver> fileResolvers}) { |
115 AnalysisEngine.instance.processRequiredPlugins(); | 111 AnalysisEngine.instance.processRequiredPlugins(); |
116 | 112 |
117 sdkResolver ??= options.useMockSdk | 113 sdkResolver ??= |
118 ? createMockSdkResolver(mockSdkSources) | 114 createSdkPathResolver(options.dartSdkSummaryPath, options.dartSdkPath); |
119 : createSdkPathResolver(options.dartSdkSummaryPath, options.dartSdkPath); | |
120 | 115 |
121 // Read the summaries. | 116 // Read the summaries. |
122 SummaryDataStore summaryData; | 117 SummaryDataStore summaryData; |
123 if (options.summaryPaths.isNotEmpty) { | 118 if (options.summaryPaths.isNotEmpty) { |
124 summaryData = new SummaryDataStore(options.summaryPaths); | 119 summaryData = new SummaryDataStore(options.summaryPaths); |
125 } | 120 } |
126 | 121 |
127 var srcFactory = _createSourceFactory(options, | 122 var srcFactory = _createSourceFactory(options, |
128 sdkResolver: sdkResolver, | 123 sdkResolver: sdkResolver, |
129 fileResolvers: fileResolvers, | 124 fileResolvers: fileResolvers, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 resolvers.add(new InSummaryPackageUriResolver(summaryData)); | 163 resolvers.add(new InSummaryPackageUriResolver(summaryData)); |
169 } | 164 } |
170 | 165 |
171 if (fileResolvers == null) fileResolvers = createFileResolvers(options); | 166 if (fileResolvers == null) fileResolvers = createFileResolvers(options); |
172 resolvers.addAll(fileResolvers); | 167 resolvers.addAll(fileResolvers); |
173 return new SourceFactory(resolvers); | 168 return new SourceFactory(resolvers); |
174 } | 169 } |
175 | 170 |
176 List<UriResolver> createFileResolvers(AnalyzerOptions options) { | 171 List<UriResolver> createFileResolvers(AnalyzerOptions options) { |
177 return [ | 172 return [ |
178 new FileUriResolver(), | 173 new ResourceUriResolver(PhysicalResourceProvider.INSTANCE), |
179 options.useMultiPackage | 174 options.useMultiPackage |
180 ? new MultiPackageResolver(options.packagePaths) | 175 ? new MultiPackageResolver(options.packagePaths) |
181 : new PackageUriResolver([new JavaFile(options.packageRoot)]) | 176 : new PackageUriResolver([new JavaFile(options.packageRoot)]) |
182 ]; | 177 ]; |
183 } | 178 } |
184 | 179 |
185 /// Creates a [DartUriResolver] that uses a mock 'dart:' library contents. | |
186 DartUriResolver createMockSdkResolver(Map<String, String> mockSources) => | |
187 new MockDartSdk(mockSources, reportMissing: true).resolver; | |
188 | |
189 DirectoryBasedDartSdk _createDirectoryBasedDartSdk(String sdkPath) { | 180 DirectoryBasedDartSdk _createDirectoryBasedDartSdk(String sdkPath) { |
190 var sdk = new DirectoryBasedDartSdk( | 181 var sdk = new DirectoryBasedDartSdk( |
191 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true); | 182 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true); |
192 sdk.useSummary = true; | 183 sdk.useSummary = true; |
193 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; | 184 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; |
194 return sdk; | 185 return sdk; |
195 } | 186 } |
196 | 187 |
197 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. | 188 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. |
198 DartUriResolver createSdkPathResolver(String sdkSummaryPath, String sdkPath) { | 189 DartUriResolver createSdkPathResolver(String sdkSummaryPath, String sdkPath) { |
199 var sdk = (sdkSummaryPath != null) | 190 var sdk = (sdkSummaryPath != null) |
200 ? new SummaryBasedDartSdk(sdkSummaryPath, true) | 191 ? new SummaryBasedDartSdk(sdkSummaryPath, true) |
201 : _createDirectoryBasedDartSdk(sdkPath); | 192 : _createDirectoryBasedDartSdk(sdkPath); |
202 return new DartUriResolver(sdk); | 193 return new DartUriResolver(sdk); |
203 } | 194 } |
OLD | NEW |