Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: pkg/analysis_server/lib/src/server_options.dart

Issue 1270593002: Server options handling. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Review fixes. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 const _optionsFileName = 'dart_analysis_server.options';
14
15 /// The shared options instance.
16 ServerOptions _serverOptions;
17
18 /// Server options.
19 ServerOptions get serverOptions {
20 if (_serverOptions == null) {
21 _serverOptions = _loadOptions();
22 }
23 return _serverOptions;
24 }
25
26 /// Find the options file relative to the user's home directory.
27 /// Returns `null` if there is none or the user's homedir cannot
28 /// be derived from the platform's environment (e.g., `HOME` and
29 /// `UserProfile` for mac/Linux and Windows respectively).
30 File _findOptionsFile() {
31 String home;
32 Map<String, String> envVars = Platform.environment;
33 if (Platform.isMacOS || Platform.isLinux) {
34 home = envVars['HOME'];
35 } else if (Platform.isWindows) {
36 home = envVars['UserProfile'];
37 }
38
39 if (home == null) {
40 return null;
41 }
42
43 return new File(path.context.join(home, _optionsFileName));
44 }
45
46 ServerOptions _loadOptions() {
47 File optionsFile = _findOptionsFile();
48 try {
49 if (optionsFile != null && optionsFile.existsSync()) {
50 return new ServerOptions.fromFile(optionsFile);
51 }
52 } catch (e) {
53 // Fall through.
54 }
55 return new ServerOptions._empty();
56 }
57
58 /// Describes options captured in an external options file.
59 /// `ServerOptions` are described in a file `dart_analysis_server.options`
60 /// located in the user's home directory. Options are defined in YAML and
61 /// read once and cached. In order for changes to be picked up, server needs
62 /// to be restarted.
63 class ServerOptions {
64 final Map<String, dynamic> _options = new HashMap<String, dynamic>();
65
66 /// Load options from the given [contents].
67 ServerOptions.fromContents(String contents) {
68 _readOptions(contents);
69 }
70
71 /// Load options from the given [options] file.
72 factory ServerOptions.fromFile(File options) =>
73 new ServerOptions.fromContents(options.readAsStringSync());
74
75 /// Create an empty options object.
76 ServerOptions._empty();
77
78 /// Get the value for `key` from the options file.
79 dynamic operator [](String key) => _options[key];
80
81 /// Get a String value for this `key` or [defaultValue] if undefined
82 /// or not a String.
83 String getStringValue(String key, {String defaultValue: null}) {
84 var value = _options[key];
85 if (value is String) {
86 return value;
87 }
88 return defaultValue;
89 }
90
91 /// Test whether the given [booleanPropertyKey] is set to the value `true`.
92 /// For example:
93 /// myDebugOption1:true
94 /// myDebugOption2:TRUE # Also true (case and trailing whitespace are igno red).
95 /// myDebugOption3:false
96 /// myDebugOption4:off # Treated as `false`.
97 /// myDebugOption5:on # Also read as `false`.
98 bool isSet(String booleanPropertyKey) => _options[booleanPropertyKey] == true;
99
100 void _readOptions(String contents) {
101 var doc = loadYaml(contents);
102 if (doc is YamlMap) {
103 doc.forEach((k, v) {
104 if (k is String) {
105 _options[k] = v;
106 }
107 });
108 }
109 }
110 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/context_manager.dart ('k') | pkg/analysis_server/test/server_options_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698