config.dart 2.72 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7
import '../convert.dart';
8
import 'file_system.dart';
9
import 'logger.dart';
10
import 'platform.dart';
11
import 'utils.dart';
12

13
/// A class to abstract configuration files.
14
class Config {
15 16 17 18 19 20
  /// Constructs a new [Config] object from a file called [name] in the
  /// current user's home directory as determined by the [Platform] and
  /// [FileSystem].
  factory Config(
    String name, {
    @required FileSystem fileSystem,
21
    @required Logger logger,
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    @required Platform platform,
  }) {
    final File file = fileSystem.file(fileSystem.path.join(
      _userHomePath(platform),
      name,
    ));
    return Config._(file, logger);
  }

  /// Constructs a new [Config] object from a file called [name] in
  /// the given [Directory].
  factory Config.test(
    String name, {
    @required Directory directory,
    @required Logger logger,
  }) => Config._(directory.childFile(name), logger);

  Config._(File file, Logger logger) : _file = file, _logger = logger {
40 41 42 43 44 45 46 47 48 49 50 51 52
    if (!_file.existsSync()) {
      return;
    }
    try {
      _values = castStringKeyedMap(json.decode(_file.readAsStringSync()));
    } on FormatException {
      _logger
        ..printError('Failed to decode preferences in ${_file.path}.')
        ..printError(
            'You may need to reapply any previously saved configuration '
            'with the "flutter config" command.',
        );
      _file.deleteSync();
53
    }
54 55
  }

56
  /// The default name for the Flutter config file.
57 58 59 60
  static const String kFlutterSettings = '.flutter_settings';

  final Logger _logger;

61 62
  File _file;

63
  String get configPath => _file.path;
64

65 66 67 68
  Map<String, dynamic> _values = <String, dynamic>{};

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

69 70
  bool containsKey(String key) => _values.containsKey(key);

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

73
  void setValue(String key, Object value) {
74 75 76 77 78 79 80 81 82 83
    _values[key] = value;
    _flushValues();
  }

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

  void _flushValues() {
84
    String json = const JsonEncoder.withIndent('  ').convert(_values);
85
    json = '$json\n';
86
    _file.writeAsStringSync(json);
87
  }
88 89 90 91 92 93 94 95 96 97 98 99

  // Reads the process environment to find the current user's home directory.
  //
  // If the searched environment variables are not set, '.' is returned instead.
  //
  // Note that this is different from FileSystemUtils.homeDirPath.
  static String _userHomePath(Platform platform) {
    final String envKey = platform.operatingSystem == 'windows'
      ? 'APPDATA'
      : 'HOME';
    return platform.environment[envKey] ?? '.';
  }
100
}