Unverified Commit cf903d73 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove globals from process, filesystem (#78357)

parent e85fe60d
...@@ -9,7 +9,6 @@ import 'package:meta/meta.dart'; ...@@ -9,7 +9,6 @@ import 'package:meta/meta.dart';
import 'runner.dart' as runner; import 'runner.dart' as runner;
import 'src/artifacts.dart'; import 'src/artifacts.dart';
import 'src/base/context.dart'; import 'src/base/context.dart';
import 'src/base/file_system.dart';
import 'src/base/io.dart'; import 'src/base/io.dart';
import 'src/base/logger.dart'; import 'src/base/logger.dart';
import 'src/base/platform.dart'; import 'src/base/platform.dart';
...@@ -85,7 +84,7 @@ Future<void> main(List<String> args) async { ...@@ -85,7 +84,7 @@ Future<void> main(List<String> args) async {
// instances of the platform or filesystem, so just use those. // instances of the platform or filesystem, so just use those.
Cache.flutterRoot = Cache.defaultFlutterRoot( Cache.flutterRoot = Cache.defaultFlutterRoot(
platform: const LocalPlatform(), platform: const LocalPlatform(),
fileSystem: LocalFileSystem.instance, fileSystem: globals.localFileSystem,
userMessages: UserMessages(), userMessages: UserMessages(),
); );
......
...@@ -250,7 +250,7 @@ Future<int> _exit(int code) async { ...@@ -250,7 +250,7 @@ Future<int> _exit(int code) async {
} }
// Run shutdown hooks before flushing logs // Run shutdown hooks before flushing logs
await shutdownHooks.runShutdownHooks(); await globals.shutdownHooks.runShutdownHooks();
final Completer<void> completer = Completer<void>(); final Completer<void> completer = Completer<void>();
......
...@@ -163,31 +163,19 @@ void copyDirectory( ...@@ -163,31 +163,19 @@ void copyDirectory(
/// directories and files that the tool creates under the system temporary /// directories and files that the tool creates under the system temporary
/// directory when the tool exits either normally or when killed by a signal. /// directory when the tool exits either normally or when killed by a signal.
class LocalFileSystem extends local_fs.LocalFileSystem { class LocalFileSystem extends local_fs.LocalFileSystem {
LocalFileSystem._(Signals signals, List<ProcessSignal> fatalSignals) : LocalFileSystem(this._signals, this._fatalSignals, this._shutdownHooks);
_signals = signals, _fatalSignals = fatalSignals;
@visibleForTesting @visibleForTesting
LocalFileSystem.test({ LocalFileSystem.test({
@required Signals signals, @required Signals signals,
List<ProcessSignal> fatalSignals = Signals.defaultExitSignals, List<ProcessSignal> fatalSignals = Signals.defaultExitSignals,
}) : this._(signals, fatalSignals); }) : this(signals, fatalSignals, null);
// Unless we're in a test of this class's signal handling features, we must
// have only one instance created with the singleton LocalSignals instance
// and the catchable signals it considers to be fatal.
static LocalFileSystem _instance;
static LocalFileSystem get instance => _instance ??= LocalFileSystem._(
LocalSignals.instance,
Signals.defaultExitSignals,
);
Directory _systemTemp; Directory _systemTemp;
final Map<ProcessSignal, Object> _signalTokens = <ProcessSignal, Object>{}; final Map<ProcessSignal, Object> _signalTokens = <ProcessSignal, Object>{};
final ShutdownHooks _shutdownHooks;
@visibleForTesting Future<void> dispose() async {
static Future<void> dispose() => LocalFileSystem.instance?._dispose();
Future<void> _dispose() async {
_tryToDeleteTemp(); _tryToDeleteTemp();
for (final MapEntry<ProcessSignal, Object> signalToken in _signalTokens.entries) { for (final MapEntry<ProcessSignal, Object> signalToken in _signalTokens.entries) {
await _signals.removeHandler(signalToken.key, signalToken.value); await _signals.removeHandler(signalToken.key, signalToken.value);
...@@ -233,7 +221,7 @@ class LocalFileSystem extends local_fs.LocalFileSystem { ...@@ -233,7 +221,7 @@ class LocalFileSystem extends local_fs.LocalFileSystem {
} }
// Make sure that the temporary directory is cleaned up when the tool // Make sure that the temporary directory is cleaned up when the tool
// exits normally. // exits normally.
shutdownHooks?.addShutdownHook( _shutdownHooks?.addShutdownHook(
_tryToDeleteTemp, _tryToDeleteTemp,
); );
} }
......
...@@ -11,7 +11,6 @@ import 'package:process/process.dart'; ...@@ -11,7 +11,6 @@ import 'package:process/process.dart';
import '../convert.dart'; import '../convert.dart';
import 'common.dart'; import 'common.dart';
import 'context.dart';
import 'io.dart'; import 'io.dart';
import 'logger.dart'; import 'logger.dart';
...@@ -26,8 +25,6 @@ typedef ShutdownHook = FutureOr<dynamic> Function(); ...@@ -26,8 +25,6 @@ typedef ShutdownHook = FutureOr<dynamic> Function();
// See [here](https://github.com/flutter/flutter/pull/14535#discussion_r167041161) // See [here](https://github.com/flutter/flutter/pull/14535#discussion_r167041161)
// for more details. // for more details.
ShutdownHooks get shutdownHooks => ShutdownHooks.instance;
abstract class ShutdownHooks { abstract class ShutdownHooks {
factory ShutdownHooks({ factory ShutdownHooks({
@required Logger logger, @required Logger logger,
...@@ -35,8 +32,6 @@ abstract class ShutdownHooks { ...@@ -35,8 +32,6 @@ abstract class ShutdownHooks {
logger: logger, logger: logger,
); );
static ShutdownHooks get instance => context.get<ShutdownHooks>();
/// Registers a [ShutdownHook] to be executed before the VM exits. /// Registers a [ShutdownHook] to be executed before the VM exits.
void addShutdownHook( void addShutdownHook(
ShutdownHook shutdownHook ShutdownHook shutdownHook
......
...@@ -74,7 +74,7 @@ LocalEngineLocator get localEngineLocator => context.get<LocalEngineLocator>(); ...@@ -74,7 +74,7 @@ LocalEngineLocator get localEngineLocator => context.get<LocalEngineLocator>();
/// By default it uses local disk-based implementation. Override this in tests /// By default it uses local disk-based implementation. Override this in tests
/// with [MemoryFileSystem]. /// with [MemoryFileSystem].
FileSystem get fs => ErrorHandlingFileSystem( FileSystem get fs => ErrorHandlingFileSystem(
delegate: context.get<FileSystem>() ?? LocalFileSystem.instance, delegate: context.get<FileSystem>() ?? localFileSystem,
platform: platform, platform: platform,
); );
...@@ -213,3 +213,15 @@ TemplateRenderer get templateRenderer => context.get<TemplateRenderer>(); ...@@ -213,3 +213,15 @@ TemplateRenderer get templateRenderer => context.get<TemplateRenderer>();
/// Gradle utils in the current [AppContext]. /// Gradle utils in the current [AppContext].
GradleUtils get gradleUtils => context.get<GradleUtils>(); GradleUtils get gradleUtils => context.get<GradleUtils>();
ShutdownHooks get shutdownHooks => context.get<ShutdownHooks>();
// Unless we're in a test of this class's signal handling features, we must
// have only one instance created with the singleton LocalSignals instance
// and the catchable signals it considers to be fatal.
LocalFileSystem _instance;
LocalFileSystem get localFileSystem => _instance ??= LocalFileSystem(
LocalSignals.instance,
Signals.defaultExitSignals,
shutdownHooks,
);
...@@ -39,7 +39,7 @@ void main() { ...@@ -39,7 +39,7 @@ void main() {
Logger logger; Logger logger;
setUp(() { setUp(() {
fileSystem = LocalFileSystem.instance; fileSystem = globals.localFileSystem;
platform = const LocalPlatform(); platform = const LocalPlatform();
processManager = const LocalProcessManager(); processManager = const LocalProcessManager();
terminal = AnsiTerminal(platform: platform, stdio: Stdio()); terminal = AnsiTerminal(platform: platform, stdio: Stdio());
......
...@@ -111,7 +111,7 @@ void main() { ...@@ -111,7 +111,7 @@ void main() {
processManager = const LocalProcessManager(); processManager = const LocalProcessManager();
platform = const LocalPlatform(); platform = const LocalPlatform();
terminal = AnsiTerminal(platform: platform, stdio: Stdio()); terminal = AnsiTerminal(platform: platform, stdio: Stdio());
fileSystem = LocalFileSystem.instance; fileSystem = globals.localFileSystem;
logger = BufferLogger.test(); logger = BufferLogger.test();
analyzerSeparator = platform.isWindows ? '-' : '•'; analyzerSeparator = platform.isWindows ? '-' : '•';
artifacts = CachedArtifacts( artifacts = CachedArtifacts(
......
...@@ -79,7 +79,7 @@ String getFlutterRoot() { ...@@ -79,7 +79,7 @@ String getFlutterRoot() {
throw invalidScript(); throw invalidScript();
} }
final List<String> parts = path.split(LocalFileSystem.instance.path.fromUri(scriptUri)); final List<String> parts = path.split(globals.localFileSystem.path.fromUri(scriptUri));
final int toolsIndex = parts.indexOf('flutter_tools'); final int toolsIndex = parts.indexOf('flutter_tools');
if (toolsIndex == -1) { if (toolsIndex == -1) {
throw invalidScript(); throw invalidScript();
...@@ -198,7 +198,7 @@ void test(String description, FutureOr<void> Function() body, { ...@@ -198,7 +198,7 @@ void test(String description, FutureOr<void> Function() body, {
description, description,
() async { () async {
addTearDown(() async { addTearDown(() async {
await LocalFileSystem.dispose(); await globals.localFileSystem.dispose();
}); });
return body(); return body();
}, },
......
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