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

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

Issue 2660173002: implement default analysis options in bazel (Closed)
Patch Set: Created 3 years, 10 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';
11 import 'package:analyzer/file_system/file_system.dart'; 11 import 'package:analyzer/file_system/file_system.dart';
12 import 'package:analyzer/plugin/resolver_provider.dart'; 12 import 'package:analyzer/plugin/resolver_provider.dart';
13 import 'package:analyzer/source/analysis_options_provider.dart'; 13 import 'package:analyzer/source/analysis_options_provider.dart';
14 import 'package:analyzer/source/package_map_resolver.dart'; 14 import 'package:analyzer/source/package_map_resolver.dart';
15 import 'package:analyzer/src/command_line/arguments.dart' 15 import 'package:analyzer/src/command_line/arguments.dart'
16 show applyAnalysisOptionFlags; 16 show applyAnalysisOptionFlags;
17 import 'package:analyzer/src/dart/analysis/byte_store.dart'; 17 import 'package:analyzer/src/dart/analysis/byte_store.dart';
18 import 'package:analyzer/src/dart/analysis/driver.dart' 18 import 'package:analyzer/src/dart/analysis/driver.dart'
19 show AnalysisDriver, AnalysisDriverScheduler, PerformanceLog; 19 show AnalysisDriver, AnalysisDriverScheduler, PerformanceLog;
20 import 'package:analyzer/src/dart/analysis/file_state.dart'; 20 import 'package:analyzer/src/dart/analysis/file_state.dart';
21 import 'package:analyzer/src/dart/sdk/sdk.dart'; 21 import 'package:analyzer/src/dart/sdk/sdk.dart';
22 import 'package:analyzer/src/generated/bazel.dart'; 22 import 'package:analyzer/src/generated/bazel.dart';
23 import 'package:analyzer/src/generated/engine.dart'; 23 import 'package:analyzer/src/generated/engine.dart';
24 import 'package:analyzer/src/generated/gn.dart'; 24 import 'package:analyzer/src/generated/gn.dart';
25 import 'package:analyzer/src/generated/sdk.dart'; 25 import 'package:analyzer/src/generated/sdk.dart';
26 import 'package:analyzer/src/generated/source.dart'; 26 import 'package:analyzer/src/generated/source.dart';
27 import 'package:analyzer/src/generated/workspace.dart';
27 import 'package:analyzer/src/summary/package_bundle_reader.dart'; 28 import 'package:analyzer/src/summary/package_bundle_reader.dart';
28 import 'package:analyzer/src/summary/pub_summary.dart'; 29 import 'package:analyzer/src/summary/pub_summary.dart';
29 import 'package:analyzer/src/summary/summary_sdk.dart'; 30 import 'package:analyzer/src/summary/summary_sdk.dart';
30 import 'package:analyzer/src/task/options.dart'; 31 import 'package:analyzer/src/task/options.dart';
31 import 'package:args/args.dart'; 32 import 'package:args/args.dart';
32 import 'package:package_config/packages.dart'; 33 import 'package:package_config/packages.dart';
33 import 'package:package_config/packages_file.dart'; 34 import 'package:package_config/packages_file.dart';
34 import 'package:package_config/src/packages_impl.dart'; 35 import 'package:package_config/src/packages_impl.dart';
35 import 'package:path/src/context.dart'; 36 import 'package:path/src/context.dart';
36 import 'package:yaml/yaml.dart'; 37 import 'package:yaml/yaml.dart';
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 } 239 }
239 String directoryPath = builderOptions.defaultPackagesDirectoryPath; 240 String directoryPath = builderOptions.defaultPackagesDirectoryPath;
240 if (directoryPath != null) { 241 if (directoryPath != null) {
241 Folder folder = resourceProvider.getFolder(directoryPath); 242 Folder folder = resourceProvider.getFolder(directoryPath);
242 return getPackagesFromFolder(folder); 243 return getPackagesFromFolder(folder);
243 } 244 }
244 return findPackagesFromFile(rootDirectoryPath); 245 return findPackagesFromFile(rootDirectoryPath);
245 } 246 }
246 247
247 SourceFactory createSourceFactory(String rootPath, AnalysisOptions options) { 248 SourceFactory createSourceFactory(String rootPath, AnalysisOptions options) {
248 BazelWorkspace bazelWorkspace = 249 Workspace workspace = createWorkspace(rootPath);
249 BazelWorkspace.find(resourceProvider, rootPath); 250 DartSdk sdk = findSdk(workspace.packageMap, options);
250 if (bazelWorkspace != null) { 251 return workspace.createSourceFactory(sdk);
251 List<UriResolver> resolvers = <UriResolver>[ 252 }
252 new DartUriResolver(findSdk(null, options)),
253 new BazelPackageUriResolver(bazelWorkspace),
254 new BazelFileUriResolver(bazelWorkspace)
255 ];
256 return new SourceFactory(resolvers, null, resourceProvider);
257 }
258 253
259 GnWorkspace gnWorkspace = GnWorkspace.find(resourceProvider, rootPath); 254 Workspace createWorkspace(String rootPath) {
260 if (gnWorkspace != null) { 255 Workspace workspace = BazelWorkspace.find(resourceProvider, rootPath);
261 DartSdk sdk = findSdk(gnWorkspace.packageMap, options); 256 workspace ??= GnWorkspace.find(resourceProvider, rootPath);
262 List<UriResolver> resolvers = <UriResolver>[ 257 return workspace ?? _BasicWorkspace.find(resourceProvider, rootPath, this);
263 new DartUriResolver(sdk),
264 new GnPackageUriResolver(gnWorkspace),
265 new GnFileUriResolver(gnWorkspace)
266 ];
267 return new SourceFactory(resolvers, null, resourceProvider);
268 }
269
270 Packages packages = createPackageMap(rootPath);
271 Map<String, List<Folder>> packageMap = convertPackagesToMap(packages);
272 List<UriResolver> resolvers = <UriResolver>[
273 new DartUriResolver(findSdk(packageMap, options)),
274 new PackageMapUriResolver(resourceProvider, packageMap),
275 new ResourceUriResolver(resourceProvider)
276 ];
277 return new SourceFactory(resolvers, packages, resourceProvider);
278 } 258 }
279 259
280 /** 260 /**
281 * Add any [declaredVariables] to the list of declared variables used by the 261 * Add any [declaredVariables] to the list of declared variables used by the
282 * given [context]. 262 * given [context].
283 */ 263 */
284 void declareVariables(InternalAnalysisContext context) { 264 void declareVariables(InternalAnalysisContext context) {
285 Map<String, String> variables = builderOptions.declaredVariables; 265 Map<String, String> variables = builderOptions.declaredVariables;
286 if (variables != null && variables.isNotEmpty) { 266 if (variables != null && variables.isNotEmpty) {
287 DeclaredVariables contextVariables = context.declaredVariables; 267 DeclaredVariables contextVariables = context.declaredVariables;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 sdk.useSummary = sdkManager.canUseSummaries; 377 sdk.useSummary = sdkManager.canUseSummaries;
398 return sdk; 378 return sdk;
399 }); 379 });
400 } 380 }
401 381
402 /** 382 /**
403 * Return the analysis options that should be used to analyze code in the 383 * Return the analysis options that should be used to analyze code in the
404 * directory with the given [path]. 384 * directory with the given [path].
405 */ 385 */
406 AnalysisOptions getAnalysisOptions(String path) { 386 AnalysisOptions getAnalysisOptions(String path) {
387 // TODO(danrubel) restructure so that we don't create a workspace
388 // both here and in createSourceFactory
389 Workspace workspace = createWorkspace(path);
390 SourceFactory sourceFactory = workspace.createSourceFactory(null);
391 AnalysisOptionsProvider optionsProvider =
392 new AnalysisOptionsProvider(sourceFactory);
393
407 AnalysisOptionsImpl options = createDefaultOptions(); 394 AnalysisOptionsImpl options = createDefaultOptions();
408 File optionsFile = getOptionsFile(path); 395 File optionsFile = getOptionsFile(path);
396 Map<String, YamlNode> optionMap;
397
409 if (optionsFile != null) { 398 if (optionsFile != null) {
410 // TODO(danrubel) restructure so that we don't recalculate the package map
411 // more than once per path.
412 Packages packages = createPackageMap(path);
413 Map<String, List<Folder>> packageMap = convertPackagesToMap(packages);
414 List<UriResolver> resolvers = <UriResolver>[
415 new ResourceUriResolver(resourceProvider),
416 new PackageMapUriResolver(resourceProvider, packageMap),
417 ];
418 SourceFactory sourceFactory =
419 new SourceFactory(resolvers, packages, resourceProvider);
420 try { 399 try {
421 Map<String, YamlNode> optionMap = 400 optionMap = optionsProvider.getOptionsFromFile(optionsFile);
422 new AnalysisOptionsProvider(sourceFactory)
423 .getOptionsFromFile(optionsFile);
424 applyToAnalysisOptions(options, optionMap);
425 } catch (_) { 401 } catch (_) {
426 // Ignore exceptions thrown while trying to load the options file. 402 // Ignore exceptions thrown while trying to load the options file.
427 } 403 }
404 } else {
405 // Search for the default analysis options
406 Source source;
407 // TODO(danrubel) check for flutter and use default flutter options
408 source =
Brian Wilkerson 2017/01/30 14:56:05 Merge initialization with declaration?
danrubel 2017/01/30 19:00:44 Done.
409 sourceFactory.forUri('package:dart.analysis_options/default.yaml');
410 if (source.exists()) {
411 try {
412 optionMap = optionsProvider.getOptionsFromSource(source);
413 } catch (_) {
414 // Ignore exceptions thrown while trying to load the options file.
415 }
416 }
417 }
418
419 if (optionMap != null) {
420 applyToAnalysisOptions(options, optionMap);
428 if (builderOptions.argResults != null) { 421 if (builderOptions.argResults != null) {
429 applyAnalysisOptionFlags(options, builderOptions.argResults); 422 applyAnalysisOptionFlags(options, builderOptions.argResults);
430 } 423 }
431 } 424 }
432 return options; 425 return options;
433 } 426 }
434 427
435 /** 428 /**
436 * Return the analysis options file that should be used when analyzing code in 429 * Return the analysis options file that should be used when analyzing code in
437 * the directory with the given [path]. 430 * the directory with the given [path].
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 String _readEmbedderYaml(Folder libDir) { 697 String _readEmbedderYaml(Folder libDir) {
705 File file = libDir.getChild(EMBEDDER_FILE_NAME); 698 File file = libDir.getChild(EMBEDDER_FILE_NAME);
706 try { 699 try {
707 return file.readAsStringSync(); 700 return file.readAsStringSync();
708 } on FileSystemException { 701 } on FileSystemException {
709 // File can't be read. 702 // File can't be read.
710 return null; 703 return null;
711 } 704 }
712 } 705 }
713 } 706 }
707
708 /**
709 * Information about a default Dart workspace.
710 */
711 class _BasicWorkspace extends Workspace {
712 /**
713 * The [ResourceProvider] by which paths are converted into [Resource]s.
714 */
715 final ResourceProvider provider;
716
717 /**
718 * The absolute workspace root path (the directory containing the `.jiri_root`
719 * directory).
720 */
721 final String root;
722
723 final ContextBuilder _builder;
724
725 Map<String, List<Folder>> _packageMap;
726
727 Packages _packages;
728
729 _BasicWorkspace._(this.provider, this.root, this._builder);
730
731 @override
732 Map<String, List<Folder>> get packageMap {
733 _packageMap ??= _builder.convertPackagesToMap(packages);
734 return _packageMap;
735 }
736
737 Packages get packages {
738 _packages ??= _builder.createPackageMap(root);
739 return _packages;
740 }
741
742 @override
743 UriResolver get packageUriResolver =>
744 new PackageMapUriResolver(provider, packageMap);
745
746 @override
747 SourceFactory createSourceFactory(DartSdk sdk) {
748 List<UriResolver> resolvers = <UriResolver>[];
749 if (sdk != null) {
750 resolvers.add(new DartUriResolver(sdk));
751 }
752 resolvers.add(packageUriResolver);
753 resolvers.add(new ResourceUriResolver(provider));
754 return new SourceFactory(resolvers, packages, provider);
755 }
756
757 /**
758 * Find the basic workspace that contains the given [path].
759 */
760 static _BasicWorkspace find(
761 ResourceProvider resourceProvider, String path, ContextBuilder builder) {
762 return new _BasicWorkspace._(resourceProvider, path, builder);
763 }
764 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/bazel.dart » ('j') | pkg/analyzer/test/src/context/builder_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698