persistent_tool_state.dart 4.09 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 8 9
import 'base/config.dart';
import 'base/context.dart';
import 'base/file_system.dart';
10
import 'base/logger.dart';
11
import 'base/platform.dart';
12
import 'version.dart';
13 14 15 16

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

  factory PersistentToolState.test({
28 29
    required Directory directory,
    required Logger logger,
30 31 32 33
  }) => _DefaultPersistentToolState.test(
    directory: directory,
    logger: logger,
  );
34

35
  static PersistentToolState? get instance => context.get<PersistentToolState>();
36 37 38 39

  /// Whether the welcome message should be redisplayed.
  ///
  /// May give null if the value has not been set.
40
  bool? get shouldRedisplayWelcomeMessage;
41
  void setShouldRedisplayWelcomeMessage(bool value); // Enforced nonnull setter.
42 43 44 45

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

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

51
  /// Return the hash of the last active license terms.
52
  String? get lastActiveLicenseTermsHash;
53
  void setLastActiveLicenseTermsHash(String value); // Enforced nonnull setter.
54

55
  /// Whether this client was already determined to be or not be a bot.
56
  bool? get isRunningOnBot;
57
  void setIsRunningOnBot(bool value); // Enforced nonnull setter.
58 59 60
}

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

  @visibleForTesting
  _DefaultPersistentToolState.test({
74 75
    required Directory directory,
    required Logger logger,
76
  }) : _config = Config.test(
77
      name: _kFileName,
78 79
      directory: directory,
      logger: logger,
80
    );
81

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

  final Config _config;

  @override
96 97
  bool? get shouldRedisplayWelcomeMessage {
    return _config.getValue(_kRedisplayWelcomeMessage) as bool?;
98
  }
99

100
  @override
101
  void setShouldRedisplayWelcomeMessage(bool value) {
102
    _config.setValue(_kRedisplayWelcomeMessage, value);
103 104
  }

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

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

119
  @override
120
  String? get lastActiveLicenseTermsHash => _config.getValue(_kLicenseHash) as String?;
121 122

  @override
123
  void setLastActiveLicenseTermsHash(String value) {
124 125 126
    _config.setValue(_kLicenseHash, value);
  }

127
  String? _versionKeyFor(Channel channel) {
128 129
    return _lastActiveVersionKeys[channel];
  }
130 131

  @override
132
  bool? get isRunningOnBot => _config.getValue(_kBotKey) as bool?;
133 134

  @override
135
  void setIsRunningOnBot(bool value) {
136 137
    _config.setValue(_kBotKey, value);
  }
138
}