| 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:yaml/yaml.dart'; | 8 import 'package:yaml/yaml.dart'; |
| 9 | 9 |
| 10 /// Provide the options found in the `.analysis_options` file. | 10 /// Provide the options found in the `.analysis_options` file. |
| 11 class AnalysisOptionsProvider { | 11 class AnalysisOptionsProvider { |
| 12 /// The name of the analysis options source file. | 12 /// The name of the analysis options source file. |
| 13 static const String ANALYSIS_OPTIONS_NAME = '.analysis_options'; | 13 static const String ANALYSIS_OPTIONS_NAME = '.analysis_options'; |
| 14 | 14 |
| 15 /// Provide the options found in the [ANALYSIS_OPTIONS_NAME] file located in | 15 /// Provide the options found in [root]/[ANALYSIS_OPTIONS_NAME]. |
| 16 /// [folder]. Return an empty options map if the file does not exist. | 16 /// Return an empty options map if the file does not exist. |
| 17 Map<String, YamlNode> getOptions(Folder root) { | 17 Map<String, YamlNode> getOptions(Folder root) { |
| 18 var optionsSource = |
| 19 _readAnalysisOptionsFile(root.getChild(ANALYSIS_OPTIONS_NAME)); |
| 20 return getOptionsFromString(optionsSource); |
| 21 } |
| 22 |
| 23 /// Provide the options found in [file]. |
| 24 /// Return an empty options map if the file does not exist. |
| 25 Map<String, YamlNode> getOptionsFromFile(File file) { |
| 26 var optionsSource = _readAnalysisOptionsFile(file); |
| 27 return getOptionsFromString(optionsSource); |
| 28 } |
| 29 |
| 30 /// Provide the options found in [optionsSource]. |
| 31 /// Return an empty options map if the source is null. |
| 32 Map<String, YamlNode> getOptionsFromString(String optionsSource) { |
| 18 var options = <String, YamlNode>{}; | 33 var options = <String, YamlNode>{}; |
| 19 var optionsSource = _readAnalysisOptionsFile(root); | |
| 20 if (optionsSource == null) { | 34 if (optionsSource == null) { |
| 21 return options; | 35 return options; |
| 22 } | 36 } |
| 23 var doc = loadYaml(optionsSource); | 37 var doc = loadYaml(optionsSource); |
| 24 if (doc is! YamlMap) { | 38 if (doc is! YamlMap) { |
| 25 throw new Exception( | 39 throw new Exception( |
| 26 'Bad options file format (expected map, got ${doc.runtimeType})'); | 40 'Bad options file format (expected map, got ${doc.runtimeType})'); |
| 27 } | 41 } |
| 28 if (doc is YamlMap) { | 42 if (doc is YamlMap) { |
| 29 doc.forEach((k, v) { | 43 doc.forEach((k, v) { |
| 30 if (k is! String) { | 44 if (k is! String) { |
| 31 throw new Exception( | 45 throw new Exception( |
| 32 'Bad options file format (expected String scope key, ' | 46 'Bad options file format (expected String scope key, ' |
| 33 'got ${k.runtimeType})'); | 47 'got ${k.runtimeType})'); |
| 34 } | 48 } |
| 35 options[k] = v; | 49 options[k] = v; |
| 36 }); | 50 }); |
| 37 } | 51 } |
| 38 return options; | 52 return options; |
| 39 } | 53 } |
| 40 | 54 |
| 41 /// Read the contents of [root]/[ANALYSIS_OPTIONS_NAME] as a string. | 55 /// Read the contents of [file] as a string. |
| 42 /// Returns null if file does not exist. | 56 /// Returns null if file does not exist. |
| 43 String _readAnalysisOptionsFile(Folder root) { | 57 String _readAnalysisOptionsFile(File file) { |
| 44 var file = root.getChild(ANALYSIS_OPTIONS_NAME); | |
| 45 try { | 58 try { |
| 46 return file.readAsStringSync(); | 59 return file.readAsStringSync(); |
| 47 } on FileSystemException { | 60 } on FileSystemException { |
| 48 // File can't be read. | 61 // File can't be read. |
| 49 return null; | 62 return null; |
| 50 } | 63 } |
| 51 } | 64 } |
| 52 } | 65 } |
| OLD | NEW |