• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import '../convert.dart';
6import 'context.dart';
7import 'file_system.dart';
8import 'platform.dart';
9
10class Config {
11  Config([File configFile]) {
12    _configFile = configFile ?? fs.file(fs.path.join(_userHomeDir(), '.flutter_settings'));
13    if (_configFile.existsSync())
14      _values = json.decode(_configFile.readAsStringSync());
15  }
16
17  static Config get instance => context.get<Config>();
18
19  File _configFile;
20  String get configPath => _configFile.path;
21
22  Map<String, dynamic> _values = <String, dynamic>{};
23
24  Iterable<String> get keys => _values.keys;
25
26  bool containsKey(String key) => _values.containsKey(key);
27
28  dynamic getValue(String key) => _values[key];
29
30  void setValue(String key, Object value) {
31    _values[key] = value;
32    _flushValues();
33  }
34
35  void removeValue(String key) {
36    _values.remove(key);
37    _flushValues();
38  }
39
40  void _flushValues() {
41    String json = const JsonEncoder.withIndent('  ').convert(_values);
42    json = '$json\n';
43    _configFile.writeAsStringSync(json);
44  }
45}
46
47String _userHomeDir() {
48  final String envKey = platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME';
49  return platform.environment[envKey] ?? '.';
50}
51