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