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 // 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.source.analysis_options_provider; | 5 library analyzer.source.analysis_options_provider; |
6 | 6 |
7 import 'dart:core'; | 7 import 'dart:core'; |
8 | 8 |
9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
11 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
12 import 'package:analyzer/src/source/source_resource.dart'; | 12 import 'package:analyzer/src/source/source_resource.dart'; |
| 13 import 'package:analyzer/src/task/options.dart'; |
13 import 'package:analyzer/src/util/yaml.dart'; | 14 import 'package:analyzer/src/util/yaml.dart'; |
14 import 'package:source_span/source_span.dart'; | 15 import 'package:source_span/source_span.dart'; |
15 import 'package:yaml/yaml.dart'; | 16 import 'package:yaml/yaml.dart'; |
16 | 17 |
17 /// Provide the options found in the analysis options file. | 18 /// Provide the options found in the analysis options file. |
18 class AnalysisOptionsProvider { | 19 class AnalysisOptionsProvider { |
19 /// The source factory used to resolve include declarations | 20 /// The source factory used to resolve include declarations |
20 /// in analysis options files or `null` if include is not supported. | 21 /// in analysis options files or `null` if include is not supported. |
21 SourceFactory sourceFactory; | 22 SourceFactory sourceFactory; |
22 | 23 |
23 AnalysisOptionsProvider([this.sourceFactory]); | 24 AnalysisOptionsProvider([this.sourceFactory]); |
24 | 25 |
25 /// Provide the options found in either | 26 /// Provide the options found in either |
26 /// [root]/[AnalysisEngine.ANALYSIS_OPTIONS_FILE] or | 27 /// [root]/[AnalysisEngine.ANALYSIS_OPTIONS_FILE] or |
27 /// [root]/[AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE]. | 28 /// [root]/[AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE]. |
| 29 /// Recursively merge options referenced by an include directive |
| 30 /// and remove the include directive from the resulting options map. |
28 /// Return an empty options map if the file does not exist. | 31 /// Return an empty options map if the file does not exist. |
29 Map<String, YamlNode> getOptions(Folder root, {bool crawlUp: false}) { | 32 Map<String, YamlNode> getOptions(Folder root, {bool crawlUp: false}) { |
30 Resource resource; | 33 Resource resource; |
31 for (Folder folder = root; folder != null; folder = folder.parent) { | 34 for (Folder folder = root; folder != null; folder = folder.parent) { |
32 resource = folder.getChild(AnalysisEngine.ANALYSIS_OPTIONS_FILE); | 35 resource = folder.getChild(AnalysisEngine.ANALYSIS_OPTIONS_FILE); |
33 if (resource.exists) { | 36 if (resource.exists) { |
34 break; | 37 break; |
35 } | 38 } |
36 resource = folder.getChild(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 39 resource = folder.getChild(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
37 if (resource.exists || !crawlUp) { | 40 if (resource.exists || !crawlUp) { |
38 break; | 41 break; |
39 } | 42 } |
40 } | 43 } |
41 return getOptionsFromFile(resource); | 44 return getOptionsFromFile(resource); |
42 } | 45 } |
43 | 46 |
44 /// Provide the options found in [file]. | 47 /// Provide the options found in [file]. |
| 48 /// Recursively merge options referenced by an include directive |
| 49 /// and remove the include directive from the resulting options map. |
45 /// Return an empty options map if the file does not exist. | 50 /// Return an empty options map if the file does not exist. |
46 Map<String, YamlNode> getOptionsFromFile(File file) { | 51 Map<String, YamlNode> getOptionsFromFile(File file) { |
47 return getOptionsFromSource(new FileSource(file)); | 52 return getOptionsFromSource(new FileSource(file)); |
48 } | 53 } |
49 | 54 |
50 /// Provide the options found in [source]. | 55 /// Provide the options found in [source]. |
| 56 /// Recursively merge options referenced by an include directive |
| 57 /// and remove the include directive from the resulting options map. |
51 /// Return an empty options map if the file does not exist. | 58 /// Return an empty options map if the file does not exist. |
52 Map<String, YamlNode> getOptionsFromSource(Source source) { | 59 Map<String, YamlNode> getOptionsFromSource(Source source) { |
53 Map<String, YamlNode> options = | 60 Map<String, YamlNode> options = |
54 getOptionsFromString(_readAnalysisOptions(source)); | 61 getOptionsFromString(_readAnalysisOptions(source)); |
55 YamlNode node = options.remove('include'); | 62 YamlNode node = options.remove(AnalyzerOptions.include); |
56 if (sourceFactory != null && node is YamlScalar) { | 63 if (sourceFactory != null && node is YamlScalar) { |
57 var path = node.value; | 64 var path = node.value; |
58 if (path is String) { | 65 if (path is String) { |
59 Source parent = sourceFactory.resolveUri(source, path); | 66 Source parent = sourceFactory.resolveUri(source, path); |
60 options = merge(getOptionsFromSource(parent), options); | 67 options = merge(getOptionsFromSource(parent), options); |
61 } | 68 } |
62 } | 69 } |
63 return options; | 70 return options; |
64 } | 71 } |
65 | 72 |
66 /// Provide the options found in [optionsSource]. | 73 /// Provide the options found in [optionsSource]. |
| 74 /// An include directive, if present, will be left as-is, |
| 75 /// and the referenced options will NOT be merged into the result. |
67 /// Return an empty options map if the source is null. | 76 /// Return an empty options map if the source is null. |
68 Map<String, YamlNode> getOptionsFromString(String optionsSource) { | 77 Map<String, YamlNode> getOptionsFromString(String optionsSource) { |
69 Map<String, YamlNode> options = <String, YamlNode>{}; | 78 Map<String, YamlNode> options = <String, YamlNode>{}; |
70 if (optionsSource == null) { | 79 if (optionsSource == null) { |
71 return options; | 80 return options; |
72 } | 81 } |
73 | 82 |
74 YamlNode safelyLoadYamlNode() { | 83 YamlNode safelyLoadYamlNode() { |
75 try { | 84 try { |
76 return loadYamlNode(optionsSource); | 85 return loadYamlNode(optionsSource); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 /// Thrown on options format exceptions. | 156 /// Thrown on options format exceptions. |
148 class OptionsFormatException implements Exception { | 157 class OptionsFormatException implements Exception { |
149 final String message; | 158 final String message; |
150 final SourceSpan span; | 159 final SourceSpan span; |
151 OptionsFormatException(this.message, [this.span]); | 160 OptionsFormatException(this.message, [this.span]); |
152 | 161 |
153 @override | 162 @override |
154 String toString() => | 163 String toString() => |
155 'OptionsFormatException: ${message?.toString()}, ${span?.toString()}'; | 164 'OptionsFormatException: ${message?.toString()}, ${span?.toString()}'; |
156 } | 165 } |
OLD | NEW |