| 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 analysis_server.src.server_options; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 import 'dart:io'; | |
| 9 | |
| 10 import 'package:path/path.dart' as path; | |
| 11 import 'package:yaml/yaml.dart'; | |
| 12 | |
| 13 //TODO: consider renaming (https://github.com/dart-lang/sdk/issues/23927) | |
| 14 const _optionsFileName = '.dart_analysis_server.yaml'; | |
| 15 | |
| 16 /// The shared options instance. | |
| 17 ServerOptions _serverOptions; | |
| 18 | |
| 19 /// Server options. | |
| 20 ServerOptions get serverOptions { | |
| 21 if (_serverOptions == null) { | |
| 22 _serverOptions = _loadOptions(); | |
| 23 } | |
| 24 return _serverOptions; | |
| 25 } | |
| 26 | |
| 27 /// Find the options file relative to the user's home directory. | |
| 28 /// Returns `null` if there is none or the user's homedir cannot | |
| 29 /// be derived from the platform's environment (e.g., `HOME` and | |
| 30 /// `UserProfile` for mac/Linux and Windows respectively). | |
| 31 File _findOptionsFile() { | |
| 32 String home; | |
| 33 Map<String, String> envVars = Platform.environment; | |
| 34 if (Platform.isMacOS || Platform.isLinux) { | |
| 35 home = envVars['HOME']; | |
| 36 } else if (Platform.isWindows) { | |
| 37 home = envVars['UserProfile']; | |
| 38 } | |
| 39 | |
| 40 if (home == null) { | |
| 41 return null; | |
| 42 } | |
| 43 | |
| 44 return new File(path.context.join(home, _optionsFileName)); | |
| 45 } | |
| 46 | |
| 47 ServerOptions _loadOptions() { | |
| 48 File optionsFile = _findOptionsFile(); | |
| 49 try { | |
| 50 if (optionsFile != null && optionsFile.existsSync()) { | |
| 51 return new ServerOptions.fromFile(optionsFile); | |
| 52 } | |
| 53 } catch (e) { | |
| 54 // Fall through. | |
| 55 } | |
| 56 return new ServerOptions._empty(); | |
| 57 } | |
| 58 | |
| 59 /// Describes options captured in an external options file. | |
| 60 /// `ServerOptions` are described in a file `dart_analysis_server.options` | |
| 61 /// located in the user's home directory. Options are defined in YAML and | |
| 62 /// read once and cached. In order for changes to be picked up, server needs | |
| 63 /// to be restarted. | |
| 64 class ServerOptions { | |
| 65 final Map<String, dynamic> _options = new HashMap<String, dynamic>(); | |
| 66 | |
| 67 /// Load options from the given [contents]. | |
| 68 ServerOptions.fromContents(String contents) { | |
| 69 _readOptions(contents); | |
| 70 } | |
| 71 | |
| 72 /// Load options from the given [options] file. | |
| 73 factory ServerOptions.fromFile(File options) => | |
| 74 new ServerOptions.fromContents(options.readAsStringSync()); | |
| 75 | |
| 76 /// Create an empty options object. | |
| 77 ServerOptions._empty(); | |
| 78 | |
| 79 /// Get the value for `key` from the options file. | |
| 80 dynamic operator [](String key) => _options[key]; | |
| 81 | |
| 82 /// Get a String value for this `key` or [defaultValue] if undefined | |
| 83 /// or not a String. | |
| 84 String getStringValue(String key, {String defaultValue: null}) { | |
| 85 var value = _options[key]; | |
| 86 if (value is String) { | |
| 87 return value; | |
| 88 } | |
| 89 return defaultValue; | |
| 90 } | |
| 91 | |
| 92 /// Test whether the given [booleanPropertyKey] is set to the value `true`, | |
| 93 /// falling back to [defaultValue] if undefined. | |
| 94 /// For example: | |
| 95 /// myDebugOption1:true | |
| 96 /// myDebugOption2:TRUE # Also true (case and trailing whitespace are igno
red). | |
| 97 /// myDebugOption3:false | |
| 98 /// myDebugOption4:off # Treated as `false`. | |
| 99 /// myDebugOption5:on # Also read as `false`. | |
| 100 bool isSet(String booleanPropertyKey, {bool defaultValue: false}) { | |
| 101 var value = _options[booleanPropertyKey]; | |
| 102 if (value == null) { | |
| 103 return defaultValue; | |
| 104 } | |
| 105 return value == true; | |
| 106 } | |
| 107 | |
| 108 void _readOptions(String contents) { | |
| 109 var doc = loadYaml(contents); | |
| 110 if (doc is YamlMap) { | |
| 111 doc.forEach((k, v) { | |
| 112 if (k is String) { | |
| 113 _options[k] = v; | |
| 114 } | |
| 115 }); | |
| 116 } | |
| 117 } | |
| 118 } | |
| OLD | NEW |