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