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

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: 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 // 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.
6 // significant change. Please see the README file for more information.
7
8 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.
9
10 import 'dart:io';
11
12 import 'package:path/path.dart' as path;
13 import 'package:yaml/yaml.dart';
14
15 const _optionsFileName = 'dart_analysis_server.options';
16
17 /// The shared options instance.
18 ServerOptions _serverOptions;
19
20 /// Server options.
21 ServerOptions get serverOptions {
22 if (_serverOptions == null) {
23 _serverOptions = _loadOptions();
24 }
25 return _serverOptions;
26 }
27
28 File _findOptionsFile() {
29 String home;
30 Map<String, String> envVars = Platform.environment;
31 if (Platform.isMacOS || Platform.isLinux) {
32 home = envVars['HOME'];
33 } else if (Platform.isWindows) {
34 home = envVars['UserProfile'];
35 }
36
37 // 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.
38 if (home == null) {
39 return null;
40 }
41
42 return new File(path.context.join(home, _optionsFileName));
43 }
44
45 ServerOptions _loadOptions() {
46 File optionsFile = _findOptionsFile();
47 try {
48 if (optionsFile.existsSync()) {
Brian Wilkerson 2015/07/30 18:18:59 Add "optionsFile != null &&"
pquitslund 2015/07/30 19:55:47 Done.
49 return new ServerOptions.fromFile(optionsFile);
50 }
51 } catch (e) {
52 return new ServerOptions._empty();
53 }
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!
54 }
55
56 /// Describes options captured in an external options file.
57 /// `ServerOptions` are described in a file `dart_analysis_server.options`
58 /// located in the user's home directory. Options are defined in YAML and
59 /// read once and cached. In order for changes to be picked up, server needs
60 /// to be restarted.
61 class ServerOptions {
62 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.
63
64 /// Load options from the given [contents].
65 ServerOptions.fromContents(String contents) {
66 _readOptions(contents);
67 }
68
69 /// Load options from the given [options] file.
70 factory ServerOptions.fromFile(File options) =>
71 new ServerOptions.fromContents(options.readAsStringSync());
72
73 /// Create an empty options object.
74 ServerOptions._empty();
75
76 /// Get the value for `key` from the options file.
77 dynamic operator [](String key) => _options[key];
78
79 /// Get a String value for this `key` or [defaultValue] if undefined
80 /// or not a String.
81 String getStringValue(String key, {String defaultValue: null}) {
82 var value = _options[key];
83 if (value is String) {
84 return value;
85 }
86 return defaultValue;
87 }
88
89 /// Test whether the given [booleanPropertyKey] is set to the value `true`.
90 /// For example:
91 /// myDebugOption:true
92 /// myDebugOption:TRUE # Also true (case and trailing whitespace are igno red).
93 /// myDebugOption2:false
94 /// myDebugOption2:off # Treated as `false`.
95 /// 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.
96 bool isSet(String booleanPropertyKey) => _options[booleanPropertyKey] == true;
97
98 void _readOptions(String contents) {
99 var doc = loadYaml(contents);
100 if (doc is YamlMap) {
101 doc.forEach((k, v) {
102 if (k is String) {
103 _options[k] = v;
104 }
105 });
106 }
107 }
108 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698