globals.dart 5.55 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
import 'package:platform/platform.dart';
import 'package:process/process.dart';

8 9
import 'android/android_sdk.dart';
import 'android/android_studio.dart';
10
import 'artifacts.dart';
11
import 'base/bot_detector.dart';
12
import 'base/config.dart';
13
import 'base/context.dart';
14 15
import 'base/error_handling_file_system.dart';
import 'base/file_system.dart';
16
import 'base/io.dart';
17
import 'base/logger.dart';
18
import 'base/net.dart';
19
import 'base/os.dart';
20
import 'base/template.dart';
21
import 'base/terminal.dart';
22
import 'base/user_messages.dart';
23
import 'cache.dart';
24
import 'fuchsia/fuchsia_sdk.dart';
25
import 'ios/ios_deploy.dart';
26
import 'ios/ios_workflow.dart';
27
import 'ios/mac.dart';
28
import 'ios/plist_parser.dart';
29
import 'ios/simulators.dart';
30
import 'macos/xcode.dart';
31
import 'persistent_tool_state.dart';
32
import 'reporting/reporting.dart';
33
import 'version.dart';
34
import 'web/chrome.dart';
35

36
Artifacts get artifacts => context.get<Artifacts>();
37 38
Cache get cache => context.get<Cache>();
Config get config => context.get<Config>();
39 40
Logger get logger => context.get<Logger>();
OperatingSystemUtils get os => context.get<OperatingSystemUtils>();
41
PersistentToolState get persistentToolState => PersistentToolState.instance;
42
Usage get flutterUsage => context.get<Usage>();
43 44 45 46 47 48 49 50 51 52 53

const FileSystem _kLocalFs = LocalFileSystem();

/// Currently active implementation of the file system.
///
/// By default it uses local disk-based implementation. Override this in tests
/// with [MemoryFileSystem].
FileSystem get fs => ErrorHandlingFileSystem(
  context.get<FileSystem>() ?? _kLocalFs,
);

54 55 56 57 58 59
final FileSystemUtils _defaultFileSystemUtils = FileSystemUtils(
  fileSystem: fs,
  platform: platform,
);

FileSystemUtils get fsUtils => context.get<FileSystemUtils>() ?? _defaultFileSystemUtils;
60 61 62 63 64 65 66 67 68 69

const ProcessManager _kLocalProcessManager = LocalProcessManager();

/// The active process manager.
ProcessManager get processManager => context.get<ProcessManager>() ?? _kLocalProcessManager;

const Platform _kLocalPlatform = LocalPlatform();

Platform get platform => context.get<Platform>() ?? _kLocalPlatform;

70 71
AndroidStudio get androidStudio => context.get<AndroidStudio>();
AndroidSdk get androidSdk => context.get<AndroidSdk>();
72
FlutterVersion get flutterVersion => context.get<FlutterVersion>();
73
FuchsiaArtifacts get fuchsiaArtifacts => context.get<FuchsiaArtifacts>();
74
IMobileDevice get iMobileDevice => context.get<IMobileDevice>();
75
IOSDeploy get iosDeploy => context.get<IOSDeploy>();
76
IOSSimulatorUtils get iosSimulatorUtils => context.get<IOSSimulatorUtils>();
77
IOSWorkflow get iosWorkflow => context.get<IOSWorkflow>();
78
SimControl get simControl => context.get<SimControl>();
79 80
UserMessages get userMessages => context.get<UserMessages>();
Xcode get xcode => context.get<Xcode>();
81

82 83
XCDevice get xcdevice => context.get<XCDevice>();

84 85 86
final BotDetector _defaultBotDetector = BotDetector(
  httpClientFactory: context.get<HttpClientFactory>() ?? () => HttpClient(),
  platform: platform,
87
  persistentToolState: persistentToolState,
88 89 90 91 92 93
);

BotDetector get botDetector => context.get<BotDetector>() ?? _defaultBotDetector;

Future<bool> get isRunningOnBot => botDetector.isRunningOnBot;

94 95
/// Display an error level message to the user. Commands should use this if they
/// fail in some way.
96
///
97 98 99 100 101 102 103 104
/// Set [emphasis] to true to make the output bold if it's supported.
/// Set [color] to a [TerminalColor] to color the output, if the logger
/// supports it. The [color] defaults to [TerminalColor.red].
void printError(
  String message, {
  StackTrace stackTrace,
  bool emphasis,
  TerminalColor color,
105 106
  int indent,
  int hangingIndent,
107
  bool wrap,
108 109 110 111 112 113
}) {
  logger.printError(
    message,
    stackTrace: stackTrace,
    emphasis: emphasis ?? false,
    color: color,
114 115
    indent: indent,
    hangingIndent: hangingIndent,
116
    wrap: wrap,
117
  );
118
}
119 120 121

/// Display normal output of the command. This should be used for things like
/// progress messages, success messages, or just normal command output.
122 123 124 125 126
///
/// Set `emphasis` to true to make the output bold if it's supported.
///
/// Set `newline` to false to skip the trailing linefeed.
///
127 128
/// If `indent` is provided, each line of the message will be prepended by the
/// specified number of whitespaces.
129
void printStatus(
130 131 132 133 134
  String message, {
  bool emphasis,
  bool newline,
  TerminalColor color,
  int indent,
135
  int hangingIndent,
136
  bool wrap,
137
}) {
138 139
  logger.printStatus(
    message,
140 141 142 143
    emphasis: emphasis ?? false,
    color: color,
    newline: newline ?? true,
    indent: indent,
144
    hangingIndent: hangingIndent,
145
    wrap: wrap,
146
  );
147
}
148 149 150 151

/// Use this for verbose tracing output. Users can turn this output on in order
/// to help diagnose issues with the toolchain or with their setup.
void printTrace(String message) => logger.printTrace(message);
152 153 154 155 156

AnsiTerminal get terminal {
  return context?.get<AnsiTerminal>() ?? _defaultAnsiTerminal;
}

157 158 159 160 161 162
final AnsiTerminal _defaultAnsiTerminal = AnsiTerminal(
  stdio: stdio,
  platform: platform,
);

/// The global Stdio wrapper.
163 164 165 166 167 168 169 170
Stdio get stdio => context.get<Stdio>() ?? (_stdioInstance ??= Stdio());
Stdio _stdioInstance;

PlistParser get plistParser => context.get<PlistParser>() ?? (
  _plistInstance ??= PlistParser(
    fileSystem: fs,
    processManager: processManager,
    logger: logger,
171
));
172
PlistParser _plistInstance;
173

174 175
/// The [ChromeLauncher] instance.
ChromeLauncher get chromeLauncher => context.get<ChromeLauncher>();
176 177 178

/// The global template renderer
TemplateRenderer get templateRenderer => context.get<TemplateRenderer>();