io.dart 6.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// This file serves as the single point of entry into the `dart:io` APIs
/// within Flutter tools.
///
/// In order to make Flutter tools more testable, we use the `FileSystem` APIs
/// in `package:file` rather than using the `dart:io` file APIs directly (see
/// `file_system.dart`). Doing so allows us to swap out local file system
/// access with mockable (or in-memory) file systems, making our tests hermetic
/// vis-a-vis file system access.
///
14 15 16
/// We also use `package:platform` to provide an abstraction away from the
/// static methods in the `dart:io` `Platform` class (see `platform.dart`). As
/// such, do not export Platform from this file!
17
///
18 19 20 21 22 23 24 25 26 27
/// To ensure that all file system and platform API access within Flutter tools
/// goes through the proper APIs, we forbid direct imports of `dart:io` (via a
/// test), forcing all callers to instead import this file, which exports the
/// blessed subset of `dart:io` that is legal to use in Flutter tools.
///
/// Because of the nature of this file, it is important that **platform and file
/// APIs not be exported from `dart:io` in this file**! Moreover, be careful
/// about any additional exports that you add to this file, as doing so will
/// increase the API surface that we have to test in Flutter tools, and the APIs
/// in `dart:io` can sometimes be hard to use in tests.
28
import 'dart:async';
29
import 'dart:io' as io show exit, IOSink, ProcessSignal, stderr, stdin, Stdout, stdout;
30 31 32

import 'package:meta/meta.dart';

33
import 'context.dart';
34
import 'platform.dart';
35 36
import 'process.dart';

37 38 39
export 'dart:io'
    show
        BytesBuilder,
40
        CompressionOptions,
41
        // Directory         NO! Use `file_system.dart`
42
        exitCode,
43 44
        // File              NO! Use `file_system.dart`
        // FileSystemEntity  NO! Use `file_system.dart`
45
        gzip,
46
        HandshakeException,
47 48 49
        HttpClient,
        HttpClientRequest,
        HttpClientResponse,
50 51
        // TODO(tvolkert): Uncomment (flutter/flutter#33791)
        //HttpClientResponseCompressionState,
52 53 54 55
        HttpHeaders,
        HttpRequest,
        HttpServer,
        HttpStatus,
56 57 58 59
        InternetAddress,
        InternetAddressType,
        IOException,
        IOSink,
60
        // Link              NO! Use `file_system.dart`
61
        pid,
62
        // Platform          NO! use `platform.dart`
63 64 65
        Process,
        ProcessException,
        ProcessResult,
66
        // ProcessSignal     NO! Use [ProcessSignal] below.
67
        ProcessStartMode,
68
        // RandomAccessFile  NO! Use `file_system.dart`
69
        ServerSocket,
70 71 72
        // stderr,           NO! Use `io.dart`
        // stdin,            NO! Use `io.dart`
        Stdin,
73
        StdinException,
74
        // stdout,           NO! Use `io.dart`
75
        Stdout,
76 77
        Socket,
        SocketException,
78
        systemEncoding,
79
        WebSocket,
80
        WebSocketException,
81 82 83
        WebSocketTransformer;

/// Exits the process with the given [exitCode].
84
typedef ExitFunction = void Function(int exitCode);
85

86
const ExitFunction _defaultExitFunction = io.exit;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

ExitFunction _exitFunction = _defaultExitFunction;

/// Exits the process.
///
/// This is analogous to the `exit` function in `dart:io`, except that this
/// function may be set to a testing-friendly value by calling
/// [setExitFunctionForTests] (and then restored to its default implementation
/// with [restoreExitFunction]). The default implementation delegates to
/// `dart:io`.
ExitFunction get exit => _exitFunction;

/// Sets the [exit] function to a function that throws an exception rather
/// than exiting the process; this is intended for testing purposes.
@visibleForTesting
102
void setExitFunctionForTests([ ExitFunction exitFunction ]) {
103
  _exitFunction = exitFunction ?? (int exitCode) {
104
    throw ProcessExit(exitCode, immediate: true);
105 106 107 108 109 110 111 112
  };
}

/// Restores the [exit] function to the `dart:io` implementation.
@visibleForTesting
void restoreExitFunction() {
  _exitFunction = _defaultExitFunction;
}
113 114 115 116 117 118 119 120 121 122

/// A portable version of [io.ProcessSignal].
///
/// Listening on signals that don't exist on the current platform is just a
/// no-op. This is in contrast to [io.ProcessSignal], where listening to
/// non-existent signals throws an exception.
class ProcessSignal implements io.ProcessSignal {
  @visibleForTesting
  const ProcessSignal(this._delegate);

123 124 125 126 127 128
  static const ProcessSignal SIGWINCH = _PosixProcessSignal._(io.ProcessSignal.sigwinch);
  static const ProcessSignal SIGTERM = _PosixProcessSignal._(io.ProcessSignal.sigterm);
  static const ProcessSignal SIGUSR1 = _PosixProcessSignal._(io.ProcessSignal.sigusr1);
  static const ProcessSignal SIGUSR2 = _PosixProcessSignal._(io.ProcessSignal.sigusr2);
  static const ProcessSignal SIGINT =  ProcessSignal(io.ProcessSignal.sigint);
  static const ProcessSignal SIGKILL =  ProcessSignal(io.ProcessSignal.sigkill);
129 130 131 132 133

  final io.ProcessSignal _delegate;

  @override
  Stream<ProcessSignal> watch() {
134
    return _delegate.watch().map<ProcessSignal>((io.ProcessSignal signal) => this);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  }

  @override
  String toString() => _delegate.toString();
}

/// A [ProcessSignal] that is only available on Posix platforms.
///
/// Listening to a [_PosixProcessSignal] is a no-op on Windows.
class _PosixProcessSignal extends ProcessSignal {

  const _PosixProcessSignal._(io.ProcessSignal wrappedSignal) : super(wrappedSignal);

  @override
  Stream<ProcessSignal> watch() {
    if (platform.isWindows)
151
      return const Stream<ProcessSignal>.empty();
152 153 154
    return super.watch();
  }
}
155 156 157 158 159

class Stdio {
  const Stdio();

  Stream<List<int>> get stdin => io.stdin;
160
  io.Stdout get stdout => io.stdout;
161
  io.IOSink get stderr => io.stderr;
162 163 164 165 166

  bool get hasTerminal => io.stdout.hasTerminal;
  int get terminalColumns => hasTerminal ? io.stdout.terminalColumns : null;
  int get terminalLines => hasTerminal ? io.stdout.terminalLines : null;
  bool get supportsAnsiEscapes => hasTerminal ? io.stdout.supportsAnsiEscapes : false;
167 168
}

169 170
Stdio get stdio => context.get<Stdio>() ?? const Stdio();
io.Stdout get stdout => stdio.stdout;
171 172
Stream<List<int>> get stdin => stdio.stdin;
io.IOSink get stderr => stdio.stderr;