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 source.analysis_options_provider; | 5 library source.analysis_options_provider; |
6 | 6 |
7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
8 import 'package:analyzer/src/generated/engine.dart'; | 8 import 'package:analyzer/src/generated/engine.dart'; |
| 9 import 'package:source_span/source_span.dart'; |
9 import 'package:yaml/yaml.dart'; | 10 import 'package:yaml/yaml.dart'; |
10 | 11 |
11 /// Provide the options found in the `.analysis_options` file. | 12 /// Provide the options found in the `.analysis_options` file. |
12 class AnalysisOptionsProvider { | 13 class AnalysisOptionsProvider { |
13 /// Provide the options found in [root]/[ANALYSIS_OPTIONS_FILE]. | 14 /// Provide the options found in [root]/[ANALYSIS_OPTIONS_FILE]. |
14 /// Return an empty options map if the file does not exist. | 15 /// Return an empty options map if the file does not exist. |
15 Map<String, YamlNode> getOptions(Folder root) { | 16 Map<String, YamlNode> getOptions(Folder root) { |
16 var optionsSource = _readAnalysisOptionsFile( | 17 var optionsSource = _readAnalysisOptionsFile( |
17 root.getChild(AnalysisEngine.ANALYSIS_OPTIONS_FILE)); | 18 root.getChild(AnalysisEngine.ANALYSIS_OPTIONS_FILE)); |
18 return getOptionsFromString(optionsSource); | 19 return getOptionsFromString(optionsSource); |
19 } | 20 } |
20 | 21 |
21 /// Provide the options found in [file]. | 22 /// Provide the options found in [file]. |
22 /// Return an empty options map if the file does not exist. | 23 /// Return an empty options map if the file does not exist. |
23 Map<String, YamlNode> getOptionsFromFile(File file) { | 24 Map<String, YamlNode> getOptionsFromFile(File file) { |
24 var optionsSource = _readAnalysisOptionsFile(file); | 25 var optionsSource = _readAnalysisOptionsFile(file); |
25 return getOptionsFromString(optionsSource); | 26 return getOptionsFromString(optionsSource); |
26 } | 27 } |
27 | 28 |
28 /// Provide the options found in [optionsSource]. | 29 /// Provide the options found in [optionsSource]. |
29 /// Return an empty options map if the source is null. | 30 /// Return an empty options map if the source is null. |
30 Map<String, YamlNode> getOptionsFromString(String optionsSource) { | 31 Map<String, YamlNode> getOptionsFromString(String optionsSource) { |
31 var options = <String, YamlNode>{}; | 32 var options = <String, YamlNode>{}; |
32 if (optionsSource == null) { | 33 if (optionsSource == null) { |
33 return options; | 34 return options; |
34 } | 35 } |
35 var doc = loadYaml(optionsSource); | 36 var doc = loadYaml(optionsSource); |
36 if ((doc != null) && (doc is! YamlMap)) { | 37 if ((doc != null) && (doc is! YamlMap)) { |
37 throw new Exception( | 38 throw new OptionsFormatException( |
38 'Bad options file format (expected map, got ${doc.runtimeType})\n' | 39 'Bad options file format (expected map, got ${doc.runtimeType})\n' |
39 'contents of options file:\n' | 40 'contents of options file:\n' |
40 '$optionsSource\n'); | 41 '$optionsSource\n', |
| 42 doc.span); |
41 } | 43 } |
42 if (doc is YamlMap) { | 44 if (doc is YamlMap) { |
43 doc.forEach((k, v) { | 45 doc.forEach((k, v) { |
44 if (k is! String) { | 46 if (k is! String) { |
45 throw new Exception( | 47 throw new OptionsFormatException( |
46 'Bad options file format (expected String scope key, ' | 48 'Bad options file format (expected String scope key, ' |
47 'got ${k.runtimeType})'); | 49 'got ${k.runtimeType})', |
| 50 k != null ? k.span : doc.span); |
48 } | 51 } |
49 options[k] = v; | 52 options[k] = v; |
50 }); | 53 }); |
51 } | 54 } |
52 return options; | 55 return options; |
53 } | 56 } |
54 | 57 |
55 /// Read the contents of [file] as a string. | 58 /// Read the contents of [file] as a string. |
56 /// Returns null if file does not exist. | 59 /// Returns null if file does not exist. |
57 String _readAnalysisOptionsFile(File file) { | 60 String _readAnalysisOptionsFile(File file) { |
58 try { | 61 try { |
59 return file.readAsStringSync(); | 62 return file.readAsStringSync(); |
60 } on FileSystemException { | 63 } on FileSystemException { |
61 // File can't be read. | 64 // File can't be read. |
62 return null; | 65 return null; |
63 } | 66 } |
64 } | 67 } |
65 } | 68 } |
| 69 |
| 70 /// Thrown on options format exceptions. |
| 71 class OptionsFormatException implements Exception { |
| 72 final String message; |
| 73 final SourceSpan span; |
| 74 OptionsFormatException(this.message, [this.span]); |
| 75 |
| 76 @override |
| 77 String toString() => |
| 78 'OptionsFormatException: ${message?.toString()}, ${span?.toString()}'; |
| 79 } |
OLD | NEW |