Unverified Commit 4a730509 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate persistent_tool_state to null safety (#80085)

parent 4cf0465b
......@@ -42,10 +42,10 @@ class BotDetector {
// When set, GA logs to a local file (normally for tests) so we don't need to filter.
|| _platform.environment.containsKey('FLUTTER_ANALYTICS_LOG_FILE')
) {
return _persistentToolState.isRunningOnBot = false;
return _persistentToolState.runningOnBot = false;
}
return _persistentToolState.isRunningOnBot = _platform.environment['BOT'] == 'true'
return _persistentToolState.runningOnBot = _platform.environment['BOT'] == 'true'
// https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
|| _platform.environment['TRAVIS'] == 'true'
......
......@@ -178,7 +178,7 @@ class DevtoolsServerLauncher extends DevtoolsLauncher {
'devtools`:\n${_devToolsActivateProcess.stderr}');
return false;
}
_persistentToolState.lastDevToolsActivationTime = DateTime.now();
_persistentToolState.lastDevToolsActivation = DateTime.now();
return true;
} on Exception catch (e, _) {
_logger.printError('Error running `pub global activate devtools`: $e');
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:meta/meta.dart';
import 'base/config.dart';
......@@ -17,9 +15,9 @@ import 'version.dart';
/// must persist across tool invocations.
abstract class PersistentToolState {
factory PersistentToolState({
@required FileSystem fileSystem,
@required Logger logger,
@required Platform platform,
required FileSystem fileSystem,
required Logger logger,
required Platform platform,
}) => _DefaultPersistentToolState(
fileSystem: fileSystem,
logger: logger,
......@@ -27,43 +25,47 @@ abstract class PersistentToolState {
);
factory PersistentToolState.test({
@required Directory directory,
@required Logger logger,
required Directory directory,
required Logger logger,
}) => _DefaultPersistentToolState.test(
directory: directory,
logger: logger,
);
static PersistentToolState get instance => context.get<PersistentToolState>();
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;
bool? get shouldRedisplayWelcomeMessage;
set redisplayWelcomeMessage(bool value); // Enforced nonnull setter.
/// Returns the last active version for a given [channel].
///
/// If there was no active prior version, returns `null` instead.
String lastActiveVersion(Channel channel);
String? lastActiveVersion(Channel channel);
/// Update the last active version for a given [channel].
void updateLastActiveVersion(String fullGitHash, Channel channel);
/// Return the hash of the last active license terms.
String lastActiveLicenseTerms;
String? get lastActiveLicenseTermsHash;
set lastActiveLicenseTerms(String value); // Enforced nonnull setter.
/// Whether this client was already determined to be or not be a bot.
bool isRunningOnBot;
bool? get isRunningOnBot;
set runningOnBot(bool value); // Enforced nonnull setter.
/// The last time the DevTools package was activated from pub.
DateTime lastDevToolsActivationTime;
DateTime? get lastDevToolsActivationTime;
set lastDevToolsActivation(DateTime value); // Enforced nonnull setter.
}
class _DefaultPersistentToolState implements PersistentToolState {
_DefaultPersistentToolState({
@required FileSystem fileSystem,
@required Logger logger,
@required Platform platform,
required FileSystem fileSystem,
required Logger logger,
required Platform platform,
}) : _config = Config(
_kFileName,
fileSystem: fileSystem,
......@@ -73,8 +75,8 @@ class _DefaultPersistentToolState implements PersistentToolState {
@visibleForTesting
_DefaultPersistentToolState.test({
@required Directory directory,
@required Logger logger,
required Directory directory,
required Logger logger,
}) : _config = Config.test(
name: _kFileName,
directory: directory,
......@@ -96,55 +98,56 @@ class _DefaultPersistentToolState implements PersistentToolState {
final Config _config;
@override
bool get redisplayWelcomeMessage {
return _config.getValue(_kRedisplayWelcomeMessage) as bool;
bool? get shouldRedisplayWelcomeMessage {
return _config.getValue(_kRedisplayWelcomeMessage) as bool?;
}
@override
String lastActiveVersion(Channel channel) {
final String versionKey = _versionKeyFor(channel);
assert(versionKey != null);
return _config.getValue(versionKey) as String;
set redisplayWelcomeMessage(bool value) {
_config.setValue(_kRedisplayWelcomeMessage, value);
}
@override
set redisplayWelcomeMessage(bool value) {
_config.setValue(_kRedisplayWelcomeMessage, value);
String? lastActiveVersion(Channel channel) {
final String? versionKey = _versionKeyFor(channel);
assert(versionKey != null);
return _config.getValue(versionKey!) as String?;
}
@override
void updateLastActiveVersion(String fullGitHash, Channel channel) {
final String versionKey = _versionKeyFor(channel);
final String? versionKey = _versionKeyFor(channel);
assert(versionKey != null);
_config.setValue(versionKey, fullGitHash);
_config.setValue(versionKey!, fullGitHash);
}
@override
String get lastActiveLicenseTerms => _config.getValue(_kLicenseHash) as String;
String? get lastActiveLicenseTermsHash => _config.getValue(_kLicenseHash) as String?;
@override
set lastActiveLicenseTerms(String value) {
assert(value != null);
_config.setValue(_kLicenseHash, value);
}
String _versionKeyFor(Channel channel) {
String? _versionKeyFor(Channel channel) {
return _lastActiveVersionKeys[channel];
}
@override
bool get isRunningOnBot => _config.getValue(_kBotKey) as bool;
bool? get isRunningOnBot => _config.getValue(_kBotKey) as bool?;
@override
set isRunningOnBot(bool value) => _config.setValue(_kBotKey, value);
set runningOnBot(bool value) {
_config.setValue(_kBotKey, value);
}
@override
DateTime get lastDevToolsActivationTime {
final String value = _config.getValue(_kLastDevToolsActivationTimeKey) as String;
DateTime? get lastDevToolsActivationTime {
final String? value = _config.getValue(_kLastDevToolsActivationTimeKey) as String?;
return value != null ? DateTime.parse(value) : null;
}
@override
set lastDevToolsActivationTime(DateTime time) =>
set lastDevToolsActivation(DateTime time) =>
_config.setValue(_kLastDevToolsActivationTimeKey, time.toString());
}
......@@ -61,10 +61,10 @@ class FirstRunMessenger {
/// that the license terms are not printed during a `flutter upgrade`, until the
/// user manually runs the tool.
bool shouldDisplayLicenseTerms() {
if (_persistentToolState.redisplayWelcomeMessage == false) {
if (_persistentToolState.shouldRedisplayWelcomeMessage == false) {
return false;
}
final String oldHash = _persistentToolState.lastActiveLicenseTerms;
final String oldHash = _persistentToolState.lastActiveLicenseTermsHash;
return oldHash != _currentHash;
}
......
......@@ -235,7 +235,7 @@ void main() {
});
testWithoutContext('DevtoolsLauncher does not activate DevTools if it was recently activated', () async {
persistentToolState.lastDevToolsActivationTime = DateTime.now();
persistentToolState.lastDevToolsActivation = DateTime.now();
final DevtoolsLauncher launcher = DevtoolsServerLauncher(
pubExecutable: 'pub',
logger: logger,
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
......@@ -22,18 +20,18 @@ void main() {
directory: directory,
logger: BufferLogger.test(),
);
expect(state1.redisplayWelcomeMessage, null);
expect(state1.shouldRedisplayWelcomeMessage, null);
state1.redisplayWelcomeMessage = true;
expect(stateFile.existsSync(), true);
expect(state1.redisplayWelcomeMessage, true);
expect(state1.shouldRedisplayWelcomeMessage, true);
state1.redisplayWelcomeMessage = false;
expect(state1.redisplayWelcomeMessage, false);
expect(state1.shouldRedisplayWelcomeMessage, false);
final PersistentToolState state2 = PersistentToolState.test(
directory: directory,
logger: BufferLogger.test(),
);
expect(state2.redisplayWelcomeMessage, false);
expect(state2.shouldRedisplayWelcomeMessage, false);
});
testWithoutContext('channel versions can be cached and stored', () {
......@@ -69,7 +67,7 @@ void main() {
);
final DateTime time = DateTime.now();
state1.lastDevToolsActivationTime = time;
state1.lastDevToolsActivation = time;
final PersistentToolState state2 = PersistentToolState.test(
directory: directory,
......
......@@ -50,8 +50,10 @@ void main() {
FirstRunMessenger setUpFirstRunMessenger({bool redisplayWelcomeMessage, bool test = false }) {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final PersistentToolState state = PersistentToolState.test(directory: fileSystem.currentDirectory, logger: BufferLogger.test())
..redisplayWelcomeMessage = redisplayWelcomeMessage;
final PersistentToolState state = PersistentToolState.test(directory: fileSystem.currentDirectory, logger: BufferLogger.test());
if (redisplayWelcomeMessage != null) {
state.redisplayWelcomeMessage = redisplayWelcomeMessage;
}
if (test) {
return TestFirstRunMessenger(state);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment