config.dart 1.3 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import '../convert.dart';
6
import 'context.dart';
7
import 'file_system.dart';
8
import 'platform.dart';
9 10 11

class Config {
  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 21
  String get configPath => _configFile.path;

22 23 24 25
  Map<String, dynamic> _values = <String, dynamic>{};

  Iterable<String> get keys => _values.keys;

26 27
  bool containsKey(String key) => _values.containsKey(key);

28 29
  dynamic getValue(String key) => _values[key];

30
  void setValue(String key, Object value) {
31 32 33 34 35 36 37 38 39 40
    _values[key] = value;
    _flushValues();
  }

  void removeValue(String key) {
    _values.remove(key);
    _flushValues();
  }

  void _flushValues() {
41
    String json = const JsonEncoder.withIndent('  ').convert(_values);
42 43 44 45 46 47
    json = '$json\n';
    _configFile.writeAsStringSync(json);
  }
}

String _userHomeDir() {
48
  final String envKey = platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME';
49
  return platform.environment[envKey] ?? '.';
50
}