| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'package:analyzer/file_system/file_system.dart'; | |
| 6 import 'package:analyzer/source/analysis_options_provider.dart'; | |
| 7 import 'package:analyzer/src/generated/engine.dart'; | |
| 8 import 'package:analyzer/src/task/model.dart'; | |
| 9 import 'package:analyzer/task/model.dart'; | |
| 10 import 'package:package_config/packages.dart'; | |
| 11 import 'package:yaml/src/yaml_node.dart'; | |
| 12 import 'package:yaml/yaml.dart'; | |
| 13 | |
| 14 /// The descriptor used to associate analysis configuration with analysis | |
| 15 /// contexts in configuration data. | |
| 16 final ResultDescriptor<AnalysisConfiguration> ANALYSIS_CONFIGURATION = | |
| 17 new ResultDescriptorImpl('analysis.config', null); | |
| 18 | |
| 19 /// Return configuration associated with this [context], or `null` if there is | |
| 20 /// none. | |
| 21 AnalysisConfiguration getConfiguration(AnalysisContext context) => | |
| 22 context.getConfigurationData(ANALYSIS_CONFIGURATION); | |
| 23 | |
| 24 /// Associate this [config] with the given [context]. | |
| 25 void setConfiguration(AnalysisContext context, AnalysisConfiguration config) { | |
| 26 context.setConfigurationData(ANALYSIS_CONFIGURATION, config); | |
| 27 } | |
| 28 | |
| 29 /// Analysis configuration. | |
| 30 abstract class AnalysisConfiguration { | |
| 31 final AnalysisOptionsProvider optionsProvider = new AnalysisOptionsProvider(); | |
| 32 final Packages packages; | |
| 33 final ResourceProvider resourceProvider; | |
| 34 AnalysisConfiguration(this.resourceProvider, this.packages); | |
| 35 | |
| 36 factory AnalysisConfiguration.fromPubspec( | |
| 37 File pubspec, ResourceProvider resourceProvider, Packages packages) => | |
| 38 new PubspecConfiguration(pubspec, resourceProvider, packages); | |
| 39 | |
| 40 /// Get a map of options defined by this configuration (or `null` if none | |
| 41 /// are specified). | |
| 42 Map get options; | |
| 43 } | |
| 44 | |
| 45 /// Describes an analysis configuration. | |
| 46 class AnalysisConfigurationDescriptor { | |
| 47 /// The name of the package hosting the configuration. | |
| 48 String package; | |
| 49 | |
| 50 /// The name of the configuration "pragma". | |
| 51 String pragma; | |
| 52 | |
| 53 AnalysisConfigurationDescriptor.fromAnalyzerOptions(Map analyzerOptions) { | |
| 54 Object config = analyzerOptions['configuration']; | |
| 55 if (config is String) { | |
| 56 List<String> items = config.split('/'); | |
| 57 if (items.length == 2) { | |
| 58 package = items[0].trim(); | |
| 59 pragma = items[1].trim(); | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 /// Return true if this descriptor is valid. | |
| 65 bool get isValid => package != null && pragma != null; | |
| 66 } | |
| 67 | |
| 68 /// Pubspec-specified analysis configuration. | |
| 69 class PubspecConfiguration extends AnalysisConfiguration { | |
| 70 final File pubspec; | |
| 71 PubspecConfiguration( | |
| 72 this.pubspec, ResourceProvider resourceProvider, Packages packages) | |
| 73 : super(resourceProvider, packages); | |
| 74 | |
| 75 @override | |
| 76 Map get options { | |
| 77 //Safest not to cache (requested infrequently). | |
| 78 if (pubspec.exists) { | |
| 79 try { | |
| 80 String contents = pubspec.readAsStringSync(); | |
| 81 YamlNode map = loadYamlNode(contents); | |
| 82 if (map is YamlMap) { | |
| 83 YamlNode config = map['analyzer']; | |
| 84 if (config is YamlMap) { | |
| 85 AnalysisConfigurationDescriptor descriptor = | |
| 86 new AnalysisConfigurationDescriptor.fromAnalyzerOptions(config); | |
| 87 | |
| 88 if (descriptor.isValid) { | |
| 89 //Create a path, given descriptor and packagemap | |
| 90 Uri uri = packages.asMap()[descriptor.package]; | |
| 91 Uri pragma = new Uri.file('config/${descriptor.pragma}.yaml', | |
| 92 windows: false); | |
| 93 Uri optionsUri = uri.resolveUri(pragma); | |
| 94 String path = resourceProvider.pathContext.fromUri(optionsUri); | |
| 95 File file = resourceProvider.getFile(path); | |
| 96 if (file.exists) { | |
| 97 return optionsProvider.getOptionsFromFile(file); | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 } catch (_) { | |
| 103 // Skip exceptional configurations. | |
| 104 } | |
| 105 } | |
| 106 return null; | |
| 107 } | |
| 108 } | |
| OLD | NEW |