Chromium Code Reviews| Index: pkg/analysis_server/lib/src/server_options.dart |
| diff --git a/pkg/analysis_server/lib/src/server_options.dart b/pkg/analysis_server/lib/src/server_options.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..497c9a296e1ad840df110a14181265042fb1d29b |
| --- /dev/null |
| +++ b/pkg/analysis_server/lib/src/server_options.dart |
| @@ -0,0 +1,108 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +// This code was auto-generated, is not intended to be edited, and is subject to |
|
Brian Wilkerson
2015/07/30 18:18:59
?
pquitslund
2015/07/30 19:55:48
Done.
|
| +// significant change. Please see the README file for more information. |
| + |
| +library analysis_server.server_options; |
|
Brian Wilkerson
2015/07/30 18:18:59
Should be "analysis_server.src.server_options".
pquitslund
2015/07/30 19:55:47
Done.
|
| + |
| +import 'dart:io'; |
| + |
| +import 'package:path/path.dart' as path; |
| +import 'package:yaml/yaml.dart'; |
| + |
| +const _optionsFileName = 'dart_analysis_server.options'; |
| + |
| +/// The shared options instance. |
| +ServerOptions _serverOptions; |
| + |
| +/// Server options. |
| +ServerOptions get serverOptions { |
| + if (_serverOptions == null) { |
| + _serverOptions = _loadOptions(); |
| + } |
| + return _serverOptions; |
| +} |
| + |
| +File _findOptionsFile() { |
| + String home; |
| + Map<String, String> envVars = Platform.environment; |
| + if (Platform.isMacOS || Platform.isLinux) { |
| + home = envVars['HOME']; |
| + } else if (Platform.isWindows) { |
| + home = envVars['UserProfile']; |
| + } |
| + |
| + // Shouldn't happen but to be safe. |
|
Brian Wilkerson
2015/07/30 18:18:59
This can happen if either (a) the platform is not
pquitslund
2015/07/30 19:55:48
Done.
|
| + if (home == null) { |
| + return null; |
| + } |
| + |
| + return new File(path.context.join(home, _optionsFileName)); |
| +} |
| + |
| +ServerOptions _loadOptions() { |
| + File optionsFile = _findOptionsFile(); |
| + try { |
| + if (optionsFile.existsSync()) { |
|
Brian Wilkerson
2015/07/30 18:18:59
Add "optionsFile != null &&"
pquitslund
2015/07/30 19:55:47
Done.
|
| + return new ServerOptions.fromFile(optionsFile); |
| + } |
| + } catch (e) { |
| + return new ServerOptions._empty(); |
| + } |
|
scheglov
2015/07/30 18:04:10
If the options file does not exist, we will fall-t
pquitslund
2015/07/30 19:55:47
Ah, yes. Good catch!
|
| +} |
| + |
| +/// Describes options captured in an external options file. |
| +/// `ServerOptions` are described in a file `dart_analysis_server.options` |
| +/// located in the user's home directory. Options are defined in YAML and |
| +/// read once and cached. In order for changes to be picked up, server needs |
| +/// to be restarted. |
| +class ServerOptions { |
| + final Map<String, dynamic> _options = <String, dynamic>{}; |
|
Brian Wilkerson
2015/07/30 18:18:59
It probably doesn't matter much here, but we've ad
pquitslund
2015/07/30 19:55:47
NP. Updated.
|
| + |
| + /// Load options from the given [contents]. |
| + ServerOptions.fromContents(String contents) { |
| + _readOptions(contents); |
| + } |
| + |
| + /// Load options from the given [options] file. |
| + factory ServerOptions.fromFile(File options) => |
| + new ServerOptions.fromContents(options.readAsStringSync()); |
| + |
| + /// Create an empty options object. |
| + ServerOptions._empty(); |
| + |
| + /// Get the value for `key` from the options file. |
| + dynamic operator [](String key) => _options[key]; |
| + |
| + /// Get a String value for this `key` or [defaultValue] if undefined |
| + /// or not a String. |
| + String getStringValue(String key, {String defaultValue: null}) { |
| + var value = _options[key]; |
| + if (value is String) { |
| + return value; |
| + } |
| + return defaultValue; |
| + } |
| + |
| + /// Test whether the given [booleanPropertyKey] is set to the value `true`. |
| + /// For example: |
| + /// myDebugOption:true |
| + /// myDebugOption:TRUE # Also true (case and trailing whitespace are ignored). |
| + /// myDebugOption2:false |
| + /// myDebugOption2:off # Treated as `false`. |
| + /// myDebugOption2:on # Also read as `false`. |
|
Brian Wilkerson
2015/07/30 18:18:59
nit: it would be more consistent if either all the
pquitslund
2015/07/30 19:55:47
Done.
|
| + bool isSet(String booleanPropertyKey) => _options[booleanPropertyKey] == true; |
| + |
| + void _readOptions(String contents) { |
| + var doc = loadYaml(contents); |
| + if (doc is YamlMap) { |
| + doc.forEach((k, v) { |
| + if (k is String) { |
| + _options[k] = v; |
| + } |
| + }); |
| + } |
| + } |
| +} |