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 [root]/[ANALYSIS_OPTIONS_NAME]. |
| 16 /// Return an empty options map if the file does not exist. |
| 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) { |
| 33 var options = <String, YamlNode>{}; |
| 34 if (optionsSource == null) { |
| 35 return options; |
| 36 } |
| 37 var doc = loadYaml(optionsSource); |
| 38 if ((doc != null) && (doc is! YamlMap)) { |
| 39 throw new Exception( |
| 40 'Bad options file format (expected map, got ${doc.runtimeType})\n' |
| 41 'contents of options file:\n' |
| 42 '$optionsSource\n'); |
| 43 } |
| 44 if (doc is YamlMap) { |
| 45 doc.forEach((k, v) { |
| 46 if (k is! String) { |
| 47 throw new Exception( |
| 48 'Bad options file format (expected String scope key, ' |
| 49 'got ${k.runtimeType})'); |
| 50 } |
| 51 options[k] = v; |
| 52 }); |
| 53 } |
| 54 return options; |
| 55 } |
| 56 |
| 57 /// Read the contents of [file] as a string. |
| 58 /// Returns null if file does not exist. |
| 59 String _readAnalysisOptionsFile(File file) { |
| 60 try { |
| 61 return file.readAsStringSync(); |
| 62 } on FileSystemException { |
| 63 // File can't be read. |
| 64 return null; |
| 65 } |
| 66 } |
| 67 } |
OLD | NEW |