config.dart 1.27 KB
Newer Older
1 2 3 4 5 6 7
// 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.

import 'dart:convert';

import 'context.dart';
8
import 'file_system.dart';
9
import 'platform.dart';
10 11 12

class Config {
  Config([File configFile]) {
13
    _configFile = configFile ?? fs.file(fs.path.join(_userHomeDir(), '.flutter_settings'));
14 15 16 17
    if (_configFile.existsSync())
      _values = JSON.decode(_configFile.readAsStringSync());
  }

18
  static Config get instance => context[Config];
19 20

  File _configFile;
21 22
  String get configPath => _configFile.path;

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  Map<String, dynamic> _values = <String, dynamic>{};

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

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

  void setValue(String key, String value) {
    _values[key] = value;
    _flushValues();
  }

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

  void _flushValues() {
    String json = new JsonEncoder.withIndent('  ').convert(_values);
    json = '$json\n';
    _configFile.writeAsStringSync(json);
  }
}

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