persistent_tool_state.dart 4.37 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
// @dart = 2.8

7 8
import 'package:meta/meta.dart';

9 10 11
import 'base/config.dart';
import 'base/context.dart';
import 'base/file_system.dart';
12
import 'base/logger.dart';
13
import 'base/platform.dart';
14
import 'version.dart';
15 16 17 18

/// A class that represents global (non-project-specific) internal state that
/// must persist across tool invocations.
abstract class PersistentToolState {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  factory PersistentToolState({
    @required FileSystem fileSystem,
    @required Logger logger,
    @required Platform platform,
  }) => _DefaultPersistentToolState(
    fileSystem: fileSystem,
    logger: logger,
    platform: platform,
  );

  factory PersistentToolState.test({
    @required Directory directory,
    @required Logger logger,
  }) => _DefaultPersistentToolState.test(
    directory: directory,
    logger: logger,
  );
36 37 38 39 40 41 42

  static PersistentToolState get instance => context.get<PersistentToolState>();

  /// Whether the welcome message should be redisplayed.
  ///
  /// May give null if the value has not been set.
  bool redisplayWelcomeMessage;
43 44 45 46 47 48 49 50

  /// Returns the last active version for a given [channel].
  ///
  /// If there was no active prior version, returns `null` instead.
  String lastActiveVersion(Channel channel);

  /// Update the last active version for a given [channel].
  void updateLastActiveVersion(String fullGitHash, Channel channel);
51

52 53 54
  /// Return the hash of the last active license terms.
  String lastActiveLicenseTerms;

55 56
  /// Whether this client was already determined to be or not be a bot.
  bool isRunningOnBot;
57 58 59

  /// The last time the the DevTools package was activated from pub.
  DateTime lastDevToolsActivationTime;
60 61 62
}

class _DefaultPersistentToolState implements PersistentToolState {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  _DefaultPersistentToolState({
    @required FileSystem fileSystem,
    @required Logger logger,
    @required Platform platform,
  }) : _config = Config(
      _kFileName,
      fileSystem: fileSystem,
      logger: logger,
      platform: platform,
    );

  @visibleForTesting
  _DefaultPersistentToolState.test({
    @required Directory directory,
    @required Logger logger,
  }) : _config = Config.test(
79
      name: _kFileName,
80 81
      directory: directory,
      logger: logger,
82
    );
83

84
  static const String _kFileName = 'tool_state';
85
  static const String _kRedisplayWelcomeMessage = 'redisplay-welcome-message';
86 87 88 89 90 91
  static const Map<Channel, String> _lastActiveVersionKeys = <Channel,String>{
    Channel.master: 'last-active-master-version',
    Channel.dev: 'last-active-dev-version',
    Channel.beta: 'last-active-beta-version',
    Channel.stable: 'last-active-stable-version'
  };
92
  static const String _kBotKey = 'is-bot';
93
  static const String _kLastDevToolsActivationTimeKey = 'last-devtools-activation-time';
94
  static const String _kLicenseHash = 'license-hash';
95 96 97 98

  final Config _config;

  @override
99 100 101
  bool get redisplayWelcomeMessage {
    return _config.getValue(_kRedisplayWelcomeMessage) as bool;
  }
102

103 104 105 106 107 108 109
  @override
  String lastActiveVersion(Channel channel) {
    final String versionKey = _versionKeyFor(channel);
    assert(versionKey != null);
    return _config.getValue(versionKey) as String;
  }

110 111 112 113
  @override
  set redisplayWelcomeMessage(bool value) {
    _config.setValue(_kRedisplayWelcomeMessage, value);
  }
114 115 116 117 118 119 120 121

  @override
  void updateLastActiveVersion(String fullGitHash, Channel channel) {
    final String versionKey = _versionKeyFor(channel);
    assert(versionKey != null);
    _config.setValue(versionKey, fullGitHash);
  }

122 123 124 125 126 127 128 129 130
  @override
  String get lastActiveLicenseTerms => _config.getValue(_kLicenseHash) as String;

  @override
  set lastActiveLicenseTerms(String value) {
    assert(value != null);
    _config.setValue(_kLicenseHash, value);
  }

131 132 133
  String _versionKeyFor(Channel channel) {
    return _lastActiveVersionKeys[channel];
  }
134 135 136 137 138 139

  @override
  bool get isRunningOnBot => _config.getValue(_kBotKey) as bool;

  @override
  set isRunningOnBot(bool value) => _config.setValue(_kBotKey, value);
140 141 142 143 144 145 146 147 148 149

  @override
  DateTime get lastDevToolsActivationTime {
    final String value = _config.getValue(_kLastDevToolsActivationTimeKey) as String;
    return value != null ? DateTime.parse(value) : null;
  }

  @override
  set lastDevToolsActivationTime(DateTime time) =>
      _config.setValue(_kLastDevToolsActivationTimeKey, time.toString());
150
}