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

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

Issue 2425423009: Split out options from ContextBuilder (Closed)
Patch Set: Created 4 years, 2 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
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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.src.context.context_builder; 5 library analyzer.src.context.context_builder;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:core'; 8 import 'dart:core';
9 9
10 import 'package:analyzer/context/declared_variables.dart'; 10 import 'package:analyzer/context/declared_variables.dart';
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 * they can be shared across contexts. 62 * they can be shared across contexts.
63 */ 63 */
64 final DartSdkManager sdkManager; 64 final DartSdkManager sdkManager;
65 65
66 /** 66 /**
67 * The cache containing the contents of overlaid files. 67 * The cache containing the contents of overlaid files.
68 */ 68 */
69 final ContentCache contentCache; 69 final ContentCache contentCache;
70 70
71 /** 71 /**
72 * The options used by the context builder.
73 */
74 final ContextBuilderOptions builderOptions;
75
76 /**
72 * The resolver provider used to create a package: URI resolver, or `null` if 77 * The resolver provider used to create a package: URI resolver, or `null` if
73 * the normal (Package Specification DEP) lookup mechanism is to be used. 78 * the normal (Package Specification DEP) lookup mechanism is to be used.
74 */ 79 */
75 @deprecated 80 @deprecated
76 ResolverProvider packageResolverProvider; 81 ResolverProvider packageResolverProvider;
77 82
78 /** 83 /**
79 * The resolver provider used to create a file: URI resolver, or `null` if 84 * The resolver provider used to create a file: URI resolver, or `null` if
80 * the normal file URI resolver is to be used. 85 * the normal file URI resolver is to be used.
81 */ 86 */
82 @deprecated 87 @deprecated
83 ResolverProvider fileResolverProvider; 88 ResolverProvider fileResolverProvider;
84 89
85 /** 90 /**
86 * The file path of the .packages file that should be used in place of any
87 * file found using the normal (Package Specification DEP) lookup mechanism,
88 * or `null` if the normal lookup mechanism should be used.
89 */
90 String defaultPackageFilePath;
91
92 /**
93 * The file path of the packages directory that should be used in place of any
94 * file found using the normal (Package Specification DEP) lookup mechanism,
95 * or `null` if the normal lookup mechanism should be used.
96 */
97 String defaultPackagesDirectoryPath;
98
99 /**
100 * The file path of the file containing the summary of the SDK that should be
101 * used to "analyze" the SDK. This option should only be specified by
102 * command-line tools such as 'dartanalyzer' or 'ddc'.
103 */
104 String dartSdkSummaryPath;
105
106 /**
107 * The file path of the analysis options file that should be used in place of
108 * any file in the root directory or a parent of the root directory, or `null`
109 * if the normal lookup mechanism should be used.
110 */
111 String defaultAnalysisOptionsFilePath;
112
113 /**
114 * The default analysis options that should be used unless some or all of them
115 * are overridden in the analysis options file, or `null` if the default
116 * defaults should be used.
117 */
118 AnalysisOptions defaultOptions;
119
120 /**
121 * A table mapping variable names to values for the declared variables, or
122 * `null` if no additional variables should be declared.
123 */
124 Map<String, String> declaredVariables;
125
126 /**
127 * The manager of pub package summaries.
128 */
129 PubSummaryManager pubSummaryManager;
130
131 /**
132 * Initialize a newly created builder to be ready to build a context rooted in 91 * Initialize a newly created builder to be ready to build a context rooted in
133 * the directory with the given [rootDirectoryPath]. 92 * the directory with the given [rootDirectoryPath].
134 */ 93 */
135 ContextBuilder(this.resourceProvider, this.sdkManager, this.contentCache); 94 ContextBuilder(this.resourceProvider, this.sdkManager, this.contentCache,
95 {ContextBuilderOptions options})
96 : builderOptions = options ?? new ContextBuilderOptions();
136 97
137 /** 98 /**
138 * Return an analysis context that is configured correctly to analyze code in 99 * Return an analysis context that is configured correctly to analyze code in
139 * the directory with the given [path]. 100 * the directory with the given [path].
140 * 101 *
141 * *Note:* This method is not yet fully implemented and should not be used. 102 * *Note:* This method is not yet fully implemented and should not be used.
142 */ 103 */
143 AnalysisContext buildContext(String path) { 104 AnalysisContext buildContext(String path) {
144 InternalAnalysisContext context = 105 InternalAnalysisContext context =
145 AnalysisEngine.instance.createAnalysisContext(); 106 AnalysisEngine.instance.createAnalysisContext();
146 AnalysisOptions options = getAnalysisOptions(context, path); 107 AnalysisOptions options = getAnalysisOptions(context, path);
147 context.contentCache = contentCache; 108 context.contentCache = contentCache;
148 context.sourceFactory = createSourceFactory(path, options); 109 context.sourceFactory = createSourceFactory(path, options);
149 context.analysisOptions = options; 110 context.analysisOptions = options;
150 context.name = path; 111 context.name = path;
151 //_processAnalysisOptions(context, optionMap); 112 //_processAnalysisOptions(context, optionMap);
152 declareVariables(context); 113 declareVariables(context);
153 configureSummaries(context); 114 configureSummaries(context);
154 return context; 115 return context;
155 } 116 }
156 117
157 /** 118 /**
158 * Configure the context to make use of summaries. 119 * Configure the context to make use of summaries.
159 */ 120 */
160 void configureSummaries(InternalAnalysisContext context) { 121 void configureSummaries(InternalAnalysisContext context) {
161 if (pubSummaryManager != null) { 122 PubSummaryManager manager = builderOptions.pubSummaryManager;
162 List<LinkedPubPackage> linkedBundles = 123 if (manager != null) {
163 pubSummaryManager.getLinkedBundles(context); 124 List<LinkedPubPackage> linkedBundles = manager.getLinkedBundles(context);
164 if (linkedBundles.isNotEmpty) { 125 if (linkedBundles.isNotEmpty) {
165 SummaryDataStore store = new SummaryDataStore([]); 126 SummaryDataStore store = new SummaryDataStore([]);
166 for (LinkedPubPackage package in linkedBundles) { 127 for (LinkedPubPackage package in linkedBundles) {
167 store.addBundle(null, package.unlinked); 128 store.addBundle(null, package.unlinked);
168 store.addBundle(null, package.linked); 129 store.addBundle(null, package.linked);
169 } 130 }
170 context.resultProvider = 131 context.resultProvider =
171 new InputPackagesResultProvider(context, store); 132 new InputPackagesResultProvider(context, store);
172 } 133 }
173 } 134 }
(...skipping 30 matching lines...) Expand all
204 // } 165 // }
205 // } on Exception catch (e) { 166 // } on Exception catch (e) {
206 // optionsProcessors.forEach((OptionsProcessor p) => p.onError(e)); 167 // optionsProcessors.forEach((OptionsProcessor p) => p.onError(e));
207 // } 168 // }
208 // } 169 // }
209 170
210 /** 171 /**
211 * Return an analysis options object containing the default option values. 172 * Return an analysis options object containing the default option values.
212 */ 173 */
213 AnalysisOptions createDefaultOptions() { 174 AnalysisOptions createDefaultOptions() {
175 AnalysisOptions defaultOptions = builderOptions.defaultOptions;
214 if (defaultOptions == null) { 176 if (defaultOptions == null) {
215 return new AnalysisOptionsImpl(); 177 return new AnalysisOptionsImpl();
216 } 178 }
217 return new AnalysisOptionsImpl.from(defaultOptions); 179 return new AnalysisOptionsImpl.from(defaultOptions);
218 } 180 }
219 181
220 Packages createPackageMap(String rootDirectoryPath) { 182 Packages createPackageMap(String rootDirectoryPath) {
221 if (defaultPackageFilePath != null) { 183 String filePath = builderOptions.defaultPackageFilePath;
222 File configFile = resourceProvider.getFile(defaultPackageFilePath); 184 if (filePath != null) {
185 File configFile = resourceProvider.getFile(filePath);
223 List<int> bytes = configFile.readAsBytesSync(); 186 List<int> bytes = configFile.readAsBytesSync();
224 Map<String, Uri> map = parse(bytes, configFile.toUri()); 187 Map<String, Uri> map = parse(bytes, configFile.toUri());
225 resolveSymbolicLinks(map); 188 resolveSymbolicLinks(map);
226 return new MapPackages(map); 189 return new MapPackages(map);
227 } else if (defaultPackagesDirectoryPath != null) { 190 }
228 Folder folder = resourceProvider.getFolder(defaultPackagesDirectoryPath); 191 String directoryPath = builderOptions.defaultPackagesDirectoryPath;
192 if (directoryPath != null) {
193 Folder folder = resourceProvider.getFolder(directoryPath);
229 return getPackagesFromFolder(folder); 194 return getPackagesFromFolder(folder);
230 } 195 }
231 return findPackagesFromFile(rootDirectoryPath); 196 return findPackagesFromFile(rootDirectoryPath);
232 } 197 }
233 198
234 SourceFactory createSourceFactory(String rootPath, AnalysisOptions options) { 199 SourceFactory createSourceFactory(String rootPath, AnalysisOptions options) {
235 BazelWorkspace bazelWorkspace = 200 BazelWorkspace bazelWorkspace =
236 BazelWorkspace.find(resourceProvider, rootPath); 201 BazelWorkspace.find(resourceProvider, rootPath);
237 if (bazelWorkspace != null) { 202 if (bazelWorkspace != null) {
238 List<UriResolver> resolvers = <UriResolver>[ 203 List<UriResolver> resolvers = <UriResolver>[
(...skipping 12 matching lines...) Expand all
251 new ResourceUriResolver(resourceProvider) 216 new ResourceUriResolver(resourceProvider)
252 ]; 217 ];
253 return new SourceFactory(resolvers, packages, resourceProvider); 218 return new SourceFactory(resolvers, packages, resourceProvider);
254 } 219 }
255 220
256 /** 221 /**
257 * Add any [declaredVariables] to the list of declared variables used by the 222 * Add any [declaredVariables] to the list of declared variables used by the
258 * given [context]. 223 * given [context].
259 */ 224 */
260 void declareVariables(InternalAnalysisContext context) { 225 void declareVariables(InternalAnalysisContext context) {
261 if (declaredVariables != null && declaredVariables.isNotEmpty) { 226 Map<String, String> variables = builderOptions.declaredVariables;
227 if (variables != null && variables.isNotEmpty) {
262 DeclaredVariables contextVariables = context.declaredVariables; 228 DeclaredVariables contextVariables = context.declaredVariables;
263 declaredVariables.forEach((String variableName, String value) { 229 variables.forEach((String variableName, String value) {
264 contextVariables.define(variableName, value); 230 contextVariables.define(variableName, value);
265 }); 231 });
266 } 232 }
267 } 233 }
268 234
269 /** 235 /**
270 * Finds a package resolution strategy for the directory at the given absolute 236 * Finds a package resolution strategy for the directory at the given absolute
271 * [path]. 237 * [path].
272 * 238 *
273 * This function first tries to locate a `.packages` file in the directory. If 239 * This function first tries to locate a `.packages` file in the directory. If
(...skipping 11 matching lines...) Expand all
285 resolveSymbolicLinks(map); 251 resolveSymbolicLinks(map);
286 return new MapPackages(map); 252 return new MapPackages(map);
287 } else if (location is Folder) { 253 } else if (location is Folder) {
288 return getPackagesFromFolder(location); 254 return getPackagesFromFolder(location);
289 } 255 }
290 return Packages.noPackages; 256 return Packages.noPackages;
291 } 257 }
292 258
293 /** 259 /**
294 * Return the SDK that should be used to analyze code. Use the given 260 * Return the SDK that should be used to analyze code. Use the given
295 * [packageMap] and [options] to locate the SDK. 261 * [packageMap] and [analysisOptions] to locate the SDK.
296 */ 262 */
297 DartSdk findSdk( 263 DartSdk findSdk(
298 Map<String, List<Folder>> packageMap, AnalysisOptions options) { 264 Map<String, List<Folder>> packageMap, AnalysisOptions analysisOptions) {
299 if (dartSdkSummaryPath != null) { 265 String summaryPath = builderOptions.dartSdkSummaryPath;
300 return new SummaryBasedDartSdk(dartSdkSummaryPath, options.strongMode); 266 if (summaryPath != null) {
267 return new SummaryBasedDartSdk(summaryPath, analysisOptions.strongMode);
301 } else if (packageMap != null) { 268 } else if (packageMap != null) {
302 SdkExtensionFinder extFinder = new SdkExtensionFinder(packageMap); 269 SdkExtensionFinder extFinder = new SdkExtensionFinder(packageMap);
303 List<String> extFilePaths = extFinder.extensionFilePaths; 270 List<String> extFilePaths = extFinder.extensionFilePaths;
304 EmbedderYamlLocator locator = new EmbedderYamlLocator(packageMap); 271 EmbedderYamlLocator locator = new EmbedderYamlLocator(packageMap);
305 Map<Folder, YamlMap> embedderYamls = locator.embedderYamls; 272 Map<Folder, YamlMap> embedderYamls = locator.embedderYamls;
306 EmbedderSdk embedderSdk = 273 EmbedderSdk embedderSdk =
307 new EmbedderSdk(resourceProvider, embedderYamls); 274 new EmbedderSdk(resourceProvider, embedderYamls);
308 if (embedderSdk.sdkLibraries.length > 0) { 275 if (embedderSdk.sdkLibraries.length > 0) {
309 // 276 //
310 // There is an embedder file that defines the content of the SDK and 277 // There is an embedder file that defines the content of the SDK and
311 // there might be an extension file that extends it. 278 // there might be an extension file that extends it.
312 // 279 //
313 List<String> paths = <String>[]; 280 List<String> paths = <String>[];
314 for (Folder folder in embedderYamls.keys) { 281 for (Folder folder in embedderYamls.keys) {
315 paths.add(folder 282 paths.add(folder
316 .getChildAssumingFile(EmbedderYamlLocator.EMBEDDER_FILE_NAME) 283 .getChildAssumingFile(EmbedderYamlLocator.EMBEDDER_FILE_NAME)
317 .path); 284 .path);
318 } 285 }
319 paths.addAll(extFilePaths); 286 paths.addAll(extFilePaths);
320 SdkDescription description = new SdkDescription(paths, options); 287 SdkDescription description = new SdkDescription(paths, analysisOptions);
321 DartSdk dartSdk = sdkManager.getSdk(description, () { 288 DartSdk dartSdk = sdkManager.getSdk(description, () {
322 if (extFilePaths.isNotEmpty) { 289 if (extFilePaths.isNotEmpty) {
323 embedderSdk.addExtensions(extFinder.urlMappings); 290 embedderSdk.addExtensions(extFinder.urlMappings);
324 } 291 }
325 embedderSdk.analysisOptions = options; 292 embedderSdk.analysisOptions = analysisOptions;
326 embedderSdk.useSummary = sdkManager.canUseSummaries; 293 embedderSdk.useSummary = sdkManager.canUseSummaries;
327 return embedderSdk; 294 return embedderSdk;
328 }); 295 });
329 return dartSdk; 296 return dartSdk;
330 } else if (extFilePaths != null && extFilePaths.isNotEmpty) { 297 } else if (extFilePaths != null && extFilePaths.isNotEmpty) {
331 // 298 //
332 // We have an extension file, but no embedder file. 299 // We have an extension file, but no embedder file.
333 // 300 //
334 String sdkPath = sdkManager.defaultSdkDirectory; 301 String sdkPath = sdkManager.defaultSdkDirectory;
335 List<String> paths = <String>[sdkPath]; 302 List<String> paths = <String>[sdkPath];
336 paths.addAll(extFilePaths); 303 paths.addAll(extFilePaths);
337 SdkDescription description = new SdkDescription(paths, options); 304 SdkDescription description = new SdkDescription(paths, analysisOptions);
338 return sdkManager.getSdk(description, () { 305 return sdkManager.getSdk(description, () {
339 FolderBasedDartSdk sdk = new FolderBasedDartSdk( 306 FolderBasedDartSdk sdk = new FolderBasedDartSdk(
340 resourceProvider, resourceProvider.getFolder(sdkPath)); 307 resourceProvider, resourceProvider.getFolder(sdkPath));
341 if (extFilePaths.isNotEmpty) { 308 if (extFilePaths.isNotEmpty) {
342 sdk.addExtensions(extFinder.urlMappings); 309 sdk.addExtensions(extFinder.urlMappings);
343 } 310 }
344 sdk.analysisOptions = options; 311 sdk.analysisOptions = analysisOptions;
345 sdk.useSummary = sdkManager.canUseSummaries; 312 sdk.useSummary = sdkManager.canUseSummaries;
346 return sdk; 313 return sdk;
347 }); 314 });
348 } 315 }
349 } 316 }
350 String sdkPath = sdkManager.defaultSdkDirectory; 317 String sdkPath = sdkManager.defaultSdkDirectory;
351 SdkDescription description = new SdkDescription(<String>[sdkPath], options); 318 SdkDescription description =
319 new SdkDescription(<String>[sdkPath], analysisOptions);
352 return sdkManager.getSdk(description, () { 320 return sdkManager.getSdk(description, () {
353 FolderBasedDartSdk sdk = new FolderBasedDartSdk(resourceProvider, 321 FolderBasedDartSdk sdk = new FolderBasedDartSdk(resourceProvider,
354 resourceProvider.getFolder(sdkPath), options.strongMode); 322 resourceProvider.getFolder(sdkPath), analysisOptions.strongMode);
355 sdk.analysisOptions = options; 323 sdk.analysisOptions = analysisOptions;
356 sdk.useSummary = sdkManager.canUseSummaries; 324 sdk.useSummary = sdkManager.canUseSummaries;
357 return sdk; 325 return sdk;
358 }); 326 });
359 } 327 }
360 328
361 /** 329 /**
362 * Return the analysis options that should be used when the given [context] is 330 * Return the analysis options that should be used when the given [context] is
363 * used to analyze code in the directory with the given [path]. 331 * used to analyze code in the directory with the given [path].
364 */ 332 */
365 AnalysisOptions getAnalysisOptions(AnalysisContext context, String path) { 333 AnalysisOptions getAnalysisOptions(AnalysisContext context, String path) {
(...skipping 13 matching lines...) Expand all
379 } 347 }
380 } 348 }
381 return options; 349 return options;
382 } 350 }
383 351
384 /** 352 /**
385 * Return the analysis options file that should be used when analyzing code in 353 * Return the analysis options file that should be used when analyzing code in
386 * the directory with the given [path]. 354 * the directory with the given [path].
387 */ 355 */
388 File getOptionsFile(String path) { 356 File getOptionsFile(String path) {
389 if (defaultAnalysisOptionsFilePath != null) { 357 String filePath = builderOptions.defaultAnalysisOptionsFilePath;
390 return resourceProvider.getFile(defaultAnalysisOptionsFilePath); 358 if (filePath != null) {
359 return resourceProvider.getFile(filePath);
391 } 360 }
392 Folder root = resourceProvider.getFolder(path); 361 Folder root = resourceProvider.getFolder(path);
393 for (Folder folder = root; folder != null; folder = folder.parent) { 362 for (Folder folder = root; folder != null; folder = folder.parent) {
394 File file = 363 File file =
395 folder.getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_FILE); 364 folder.getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_FILE);
396 if (file.exists) { 365 if (file.exists) {
397 return file; 366 return file;
398 } 367 }
399 file = folder 368 file = folder
400 .getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); 369 .getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 if (packagesCfgFile != null) { 466 if (packagesCfgFile != null) {
498 return packagesCfgFile; 467 return packagesCfgFile;
499 } 468 }
500 parentDir = parentDir.parent; 469 parentDir = parentDir.parent;
501 } 470 }
502 return null; 471 return null;
503 } 472 }
504 } 473 }
505 474
506 /** 475 /**
476 * Options used by a [ContextBuilder].
477 */
478 class ContextBuilderOptions {
479 /**
480 * The file path of the file containing the summary of the SDK that should be
481 * used to "analyze" the SDK. This option should only be specified by
482 * command-line tools such as 'dartanalyzer' or 'ddc'.
483 */
484 String dartSdkSummaryPath;
485
486 /**
487 * The file path of the analysis options file that should be used in place of
488 * any file in the root directory or a parent of the root directory, or `null`
489 * if the normal lookup mechanism should be used.
490 */
491 String defaultAnalysisOptionsFilePath;
492
493 /**
494 * A table mapping variable names to values for the declared variables, or
495 * `null` if no additional variables should be declared.
496 */
497 Map<String, String> declaredVariables;
498
499 /**
500 * The default analysis options that should be used unless some or all of them
501 * are overridden in the analysis options file, or `null` if the default
502 * defaults should be used.
503 */
504 AnalysisOptions defaultOptions;
505
506 /**
507 * The file path of the .packages file that should be used in place of any
508 * file found using the normal (Package Specification DEP) lookup mechanism,
509 * or `null` if the normal lookup mechanism should be used.
510 */
511 String defaultPackageFilePath;
512
513 /**
514 * The file path of the packages directory that should be used in place of any
515 * file found using the normal (Package Specification DEP) lookup mechanism,
516 * or `null` if the normal lookup mechanism should be used.
517 */
518 String defaultPackagesDirectoryPath;
519
520 /**
521 * The manager of pub package summaries.
522 */
523 PubSummaryManager pubSummaryManager;
524
525 /**
526 * Initialize a newly created set of options
527 */
528 ContextBuilderOptions();
529 }
530
531 /**
507 * Given a package map, check in each package's lib directory for the existence 532 * Given a package map, check in each package's lib directory for the existence
508 * of an `_embedder.yaml` file. If the file contains a top level YamlMap, it 533 * of an `_embedder.yaml` file. If the file contains a top level YamlMap, it
509 * will be added to the [embedderYamls] map. 534 * will be added to the [embedderYamls] map.
510 */ 535 */
511 class EmbedderYamlLocator { 536 class EmbedderYamlLocator {
512 /** 537 /**
513 * The name of the embedder files being searched for. 538 * The name of the embedder files being searched for.
514 */ 539 */
515 static const String EMBEDDER_FILE_NAME = '_embedder.yaml'; 540 static const String EMBEDDER_FILE_NAME = '_embedder.yaml';
516 541
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 String _readEmbedderYaml(Folder libDir) { 615 String _readEmbedderYaml(Folder libDir) {
591 File file = libDir.getChild(EMBEDDER_FILE_NAME); 616 File file = libDir.getChild(EMBEDDER_FILE_NAME);
592 try { 617 try {
593 return file.readAsStringSync(); 618 return file.readAsStringSync();
594 } on FileSystemException { 619 } on FileSystemException {
595 // File can't be read. 620 // File can't be read.
596 return null; 621 return null;
597 } 622 }
598 } 623 }
599 } 624 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/context_manager_test.dart ('k') | pkg/analyzer/test/src/context/builder_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698