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