Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: lib/src/analyzer/context.dart

Issue 1928843002: Add explicit-sources flag and create an ExplicitSourceResolver for analyzer. Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: reuse the same flag, make it a boolean to use explicit resolver instead Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:args/args.dart' show ArgParser, ArgResults; 6 import 'package:args/args.dart' show ArgParser, ArgResults;
7 import 'package:analyzer/src/generated/engine.dart' 7 import 'package:analyzer/src/generated/engine.dart'
8 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; 8 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl;
9 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; 9 import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
10 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; 10 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk;
11 import 'package:analyzer/src/generated/source_io.dart' 11 import 'package:analyzer/src/generated/source_io.dart'
12 show 12 show
13 CustomUriResolver, 13 CustomUriResolver,
14 DartUriResolver, 14 DartUriResolver,
15 ExplicitSourceResolver,
15 FileUriResolver, 16 FileUriResolver,
16 PackageUriResolver, 17 PackageUriResolver,
17 SourceFactory, 18 SourceFactory,
18 UriResolver; 19 UriResolver;
19 import 'package:analyzer/src/summary/package_bundle_reader.dart' 20 import 'package:analyzer/src/summary/package_bundle_reader.dart'
20 show 21 show
21 InSummaryPackageUriResolver, 22 InSummaryPackageUriResolver,
22 InputPackagesResultProvider, 23 InputPackagesResultProvider,
23 SummaryDataStore; 24 SummaryDataStore;
24 import 'package:cli_util/cli_util.dart' show getSdkDir; 25 import 'package:cli_util/cli_util.dart' show getSdkDir;
25 import 'package:path/path.dart' as path; 26 import 'package:path/path.dart' as path;
26 27
27 import 'dart_sdk.dart' show MockDartSdk, mockSdkSources; 28 import 'dart_sdk.dart' show MockDartSdk, mockSdkSources;
28 import 'multi_package_resolver.dart' show MultiPackageResolver; 29 import 'multi_package_resolver.dart' show MultiPackageResolver;
29 30
30 /// Options used to set up Source URI resolution in the analysis context. 31 /// Options used to set up Source URI resolution in the analysis context.
31 class AnalyzerOptions { 32 class AnalyzerOptions {
32 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" 33 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart".
33 final Map<String, String> customUrlMappings; 34 final Map<String, String> customUrlMappings;
34 35
36 /// Whether the [customUrlMappings] are all inclusive, in which case an error
37 /// should be produced if a lookup fails.
38 final bool allInclusive;
39
35 /// Package root when resolving 'package:' urls the standard way. 40 /// Package root when resolving 'package:' urls the standard way.
36 final String packageRoot; 41 final String packageRoot;
37 42
38 /// List of summary file paths. 43 /// List of summary file paths.
39 final List<String> summaryPaths; 44 final List<String> summaryPaths;
40 45
41 /// List of paths used for the multi-package resolver. 46 /// List of paths used for the multi-package resolver.
42 final List<String> packagePaths; 47 final List<String> packagePaths;
43 48
44 /// Whether to use a mock-sdk during compilation. 49 /// Whether to use a mock-sdk during compilation.
45 final bool useMockSdk; 50 final bool useMockSdk;
46 51
47 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't 52 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't
48 /// be determined 53 /// be determined
49 final String dartSdkPath; 54 final String dartSdkPath;
50 55
51 AnalyzerOptions( 56 AnalyzerOptions(
52 {this.summaryPaths: const [], 57 {this.summaryPaths: const [],
53 this.useMockSdk: false, 58 this.useMockSdk: false,
54 String dartSdkPath, 59 String dartSdkPath,
55 this.customUrlMappings: const {}, 60 this.customUrlMappings: const {},
61 this.allInclusive: false,
56 this.packageRoot: 'packages/', 62 this.packageRoot: 'packages/',
57 this.packagePaths: const []}) 63 this.packagePaths: const []})
58 : dartSdkPath = dartSdkPath ?? getSdkDir().path; 64 : dartSdkPath = dartSdkPath ?? getSdkDir().path;
59 65
60 AnalyzerOptions.fromArguments(ArgResults args) 66 AnalyzerOptions.fromArguments(ArgResults args)
61 : summaryPaths = args['summary'], 67 : summaryPaths = args['summary'],
62 useMockSdk = false, 68 useMockSdk = false,
63 dartSdkPath = args['dart-sdk'] ?? getSdkDir().path, 69 dartSdkPath = args['dart-sdk'] ?? getSdkDir().path,
64 customUrlMappings = _parseUrlMappings(args['url-mapping']), 70 customUrlMappings = _parseUrlMappings(args['url-mapping']),
71 allInclusive = args['all-inclusive'],
65 packageRoot = args['package-root'], 72 packageRoot = args['package-root'],
66 packagePaths = args['package-paths']?.split(',') ?? []; 73 packagePaths = args['package-paths']?.split(',') ?? [];
67 74
68 /// Whether to resolve 'package:' uris using the multi-package resolver. 75 /// Whether to resolve 'package:' uris using the multi-package resolver.
69 bool get useMultiPackage => packagePaths.isNotEmpty; 76 bool get useMultiPackage => packagePaths.isNotEmpty;
70 77
71 static ArgParser addArguments(ArgParser parser) { 78 static ArgParser addArguments(ArgParser parser) {
72 return parser 79 return parser
73 ..addOption('summary', 80 ..addOption('summary',
74 abbr: 's', help: 'summary file(s) to include', allowMultiple: true) 81 abbr: 's', help: 'summary file(s) to include', allowMultiple: true)
75 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) 82 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null)
76 ..addOption('package-root', 83 ..addOption('package-root',
77 abbr: 'p', 84 abbr: 'p',
78 help: 'Package root to resolve "package:" imports', 85 help: 'Package root to resolve "package:" imports',
79 defaultsTo: 'packages/') 86 defaultsTo: 'packages/')
80 ..addOption('url-mapping', 87 ..addOption('url-mapping',
81 help: '--url-mapping=libraryUri,/path/to/library.dart uses \n' 88 help: '--url-mapping=libraryUri,/path/to/library.dart uses \n'
82 'library.dart as the source for an import of of "libraryUri".', 89 'library.dart as the source for an import of of "libraryUri".',
83 allowMultiple: true, 90 allowMultiple: true,
84 splitCommas: false) 91 splitCommas: false)
92 ..addFlag('all-inclusive',
93 help: 'whether all `package:` uris are explicitly listed \n'
94 'via the url-mapping flag.',
95 defaultsTo: false)
85 ..addOption('package-paths', 96 ..addOption('package-paths',
86 help: 'use a list of directories to resolve "package:" imports'); 97 help: 'use a list of directories to resolve "package:" imports');
87 } 98 }
88 99
89 static Map<String, String> _parseUrlMappings(Iterable argument) { 100 static Map<String, String> _parseUrlMappings(Iterable argument) {
90 var mappings = <String, String>{}; 101 var mappings = <String, String>{};
91 for (var mapping in argument) { 102 for (var mapping in argument) {
92 var splitMapping = mapping.split(','); 103 var splitMapping = mapping.split(',');
93 if (splitMapping.length >= 2) { 104 if (splitMapping.length >= 2) {
94 mappings[splitMapping[0]] = path.absolute(splitMapping[1]); 105 mappings[splitMapping[0]] = path.absolute(splitMapping[1]);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver] 152 /// Use [options.useMockSdk] to specify the SDK mode, or use [sdkResolver]
142 /// to entirely override the DartUriResolver. 153 /// to entirely override the DartUriResolver.
143 /// 154 ///
144 /// If supplied, [fileResolvers] will override the default `file:` and 155 /// If supplied, [fileResolvers] will override the default `file:` and
145 /// `package:` URI resolvers. 156 /// `package:` URI resolvers.
146 SourceFactory _createSourceFactory(AnalyzerOptions options, 157 SourceFactory _createSourceFactory(AnalyzerOptions options,
147 {DartUriResolver sdkResolver, 158 {DartUriResolver sdkResolver,
148 List<UriResolver> fileResolvers, 159 List<UriResolver> fileResolvers,
149 SummaryDataStore summaryData}) { 160 SummaryDataStore summaryData}) {
150 var resolvers = <UriResolver>[]; 161 var resolvers = <UriResolver>[];
151 if (options.customUrlMappings.isNotEmpty) { 162 if (!options.allInclusive && options.customUrlMappings.isNotEmpty) {
152 resolvers.add(new CustomUriResolver(options.customUrlMappings)); 163 resolvers.add(new CustomUriResolver(options.customUrlMappings));
153 } 164 }
154 resolvers.add(sdkResolver); 165 resolvers.add(sdkResolver);
155 if (summaryData != null) { 166 if (summaryData != null) {
156 resolvers.add(new InSummaryPackageUriResolver(summaryData)); 167 resolvers.add(new InSummaryPackageUriResolver(summaryData));
157 } 168 }
158 169
159 if (fileResolvers == null) fileResolvers = createFileResolvers(options); 170 if (fileResolvers == null) fileResolvers = createFileResolvers(options);
160 resolvers.addAll(fileResolvers); 171 resolvers.addAll(fileResolvers);
161 return new SourceFactory(resolvers); 172 return new SourceFactory(resolvers);
162 } 173 }
163 174
164 List<UriResolver> createFileResolvers(AnalyzerOptions options) { 175 List<UriResolver> createFileResolvers(AnalyzerOptions options) {
165 return [ 176 var resolvers = <UriResolver>[];
166 new FileUriResolver(), 177 if (options.allInclusive) {
167 options.useMultiPackage 178 resolvers.add(new ExplicitSourceResolver(
168 ? new MultiPackageResolver(options.packagePaths) 179 _createUriToFileMap(options.customUrlMappings)));
169 : new PackageUriResolver([new JavaFile(options.packageRoot)]) 180 }
170 ]; 181 resolvers.add(new FileUriResolver());
182 resolvers.add(options.useMultiPackage
183 ? new MultiPackageResolver(options.packagePaths)
184 : new PackageUriResolver([new JavaFile(options.packageRoot)]));
185 return resolvers;
186 }
187
188 // TODO(sigmund): delete, this was adapted from analyzer_cli, ideally this
189 // should be shared code in package:analyzer.
190 Map<Uri, JavaFile> _createUriToFileMap(Map<String, String> mapping) {
191 var uriToFileMap = <Uri, JavaFile>{};
192 mapping.forEach((key, value) {
193 uriToFileMap[Uri.parse(key)] = new JavaFile(value);
194 });
195 return uriToFileMap;
171 } 196 }
172 197
173 /// Creates a [DartUriResolver] that uses a mock 'dart:' library contents. 198 /// Creates a [DartUriResolver] that uses a mock 'dart:' library contents.
174 DartUriResolver createMockSdkResolver(Map<String, String> mockSources) => 199 DartUriResolver createMockSdkResolver(Map<String, String> mockSources) =>
175 new MockDartSdk(mockSources, reportMissing: true).resolver; 200 new MockDartSdk(mockSources, reportMissing: true).resolver;
176 201
177 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath]. 202 /// Creates a [DartUriResolver] that uses the SDK at the given [sdkPath].
178 DartUriResolver createSdkPathResolver(String sdkPath) { 203 DartUriResolver createSdkPathResolver(String sdkPath) {
179 var sdk = new DirectoryBasedDartSdk( 204 var sdk = new DirectoryBasedDartSdk(
180 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true); 205 new JavaFile(sdkPath), /*useDart2jsPaths:*/ true);
181 sdk.useSummary = true; 206 sdk.useSummary = true;
182 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true; 207 sdk.analysisOptions = new AnalysisOptionsImpl()..strongMode = true;
183 return new DartUriResolver(sdk); 208 return new DartUriResolver(sdk);
184 } 209 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698