1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:process/process.dart';
6import 'package:unified_analytics/unified_analytics.dart';
7
8import 'android/android_sdk.dart';
9import 'android/android_studio.dart';
10import 'android/gradle_utils.dart';
11import 'android/java.dart';
12import 'artifacts.dart';
13import 'base/bot_detector.dart';
14import 'base/config.dart';
15import 'base/context.dart';
16import 'base/error_handling_io.dart';
17import 'base/file_system.dart';
18import 'base/io.dart';
19import 'base/logger.dart';
20import 'base/net.dart';
21import 'base/os.dart';
22import 'base/platform.dart';
23import 'base/process.dart';
24import 'base/signals.dart';
25import 'base/template.dart';
26import 'base/terminal.dart';
27import 'base/time.dart';
28import 'base/user_messages.dart';
29import 'build_system/build_system.dart';
30import 'build_system/build_targets.dart';
31import 'cache.dart';
32import 'custom_devices/custom_devices_config.dart';
33import 'device.dart';
34import 'doctor.dart';
35import 'ios/ios_workflow.dart';
36import 'ios/plist_parser.dart';
37import 'ios/simulators.dart';
38import 'ios/xcodeproj.dart';
39import 'macos/cocoapods.dart';
40import 'macos/cocoapods_validator.dart';
41import 'macos/xcdevice.dart';
42import 'macos/xcode.dart';
43import 'native_assets.dart';
44import 'persistent_tool_state.dart';
45import 'pre_run_validator.dart';
46import 'project.dart';
47import 'reporting/crash_reporting.dart';
48import 'reporting/reporting.dart';
49import 'runner/local_engine.dart';
50import 'version.dart';
51
52// TODO(ianh): We should remove all the global variables and replace them with
53// arguments (to constructors, methods, etc, as appropriate).
54
55Artifacts? get artifacts => context.get<Artifacts>();
56BuildSystem get buildSystem => context.get<BuildSystem>()!;
57BuildTargets get buildTargets => context.get<BuildTargets>()!;
58Cache get cache => context.get<Cache>()!;
59CocoaPodsValidator? get cocoapodsValidator => context.get<CocoaPodsValidator>();
60Config get config => context.get<Config>()!;
61CrashReporter? get crashReporter => context.get<CrashReporter>();
62DeviceManager? get deviceManager => context.get<DeviceManager>();
63Doctor? get doctor => context.get<Doctor>();
64HttpClientFactory? get httpClientFactory => context.get<HttpClientFactory>();
65IOSSimulatorUtils? get iosSimulatorUtils => context.get<IOSSimulatorUtils>();
66Logger get logger => context.get<Logger>()!;
67OperatingSystemUtils get os => context.get<OperatingSystemUtils>()!;
68Signals get signals => context.get<Signals>() ?? LocalSignals.instance;
69AndroidStudio? get androidStudio => context.get<AndroidStudio>();
70AndroidSdk? get androidSdk => context.get<AndroidSdk>();
71FlutterVersion get flutterVersion => context.get<FlutterVersion>()!;
72Usage get flutterUsage => context.get<Usage>()!;
73XcodeProjectInterpreter? get xcodeProjectInterpreter => context.get<XcodeProjectInterpreter>();
74XCDevice? get xcdevice => context.get<XCDevice>();
75Xcode? get xcode => context.get<Xcode>();
76IOSWorkflow? get iosWorkflow => context.get<IOSWorkflow>();
77LocalEngineLocator? get localEngineLocator => context.get<LocalEngineLocator>();
78
79PersistentToolState? get persistentToolState => PersistentToolState.instance;
80
81BotDetector get botDetector => context.get<BotDetector>() ?? _defaultBotDetector;
82final BotDetector _defaultBotDetector = BotDetector(
83 httpClientFactory: context.get<HttpClientFactory>() ?? () => HttpClient(),
84 platform: platform,
85 persistentToolState:
86 persistentToolState ??
87 PersistentToolState(fileSystem: fs, logger: logger, platform: platform),
88);
89Future<bool> get isRunningOnBot => botDetector.isRunningOnBot;
90
91// Analytics instance for package:unified_analytics for analytics
92// reporting for all Flutter and Dart related tooling
93Analytics get analytics => context.get<Analytics>()!;
94
95/// Currently active implementation of the file system.
96///
97/// By default it uses local disk-based implementation. Override this in tests
98/// with [MemoryFileSystem].
99FileSystem get fs => ErrorHandlingFileSystem(
100 delegate: context.get<FileSystem>() ?? localFileSystem,
101 platform: platform,
102);
103
104FileSystemUtils get fsUtils =>
105 context.get<FileSystemUtils>() ?? FileSystemUtils(fileSystem: fs, platform: platform);
106
107const ProcessManager _kLocalProcessManager = LocalProcessManager();
108
109/// The active process manager.
110ProcessManager get processManager => context.get<ProcessManager>() ?? _kLocalProcessManager;
111ProcessUtils get processUtils => context.get<ProcessUtils>()!;
112
113const Platform _kLocalPlatform = LocalPlatform();
114Platform get platform => context.get<Platform>() ?? _kLocalPlatform;
115
116UserMessages get userMessages => context.get<UserMessages>()!;
117
118final OutputPreferences _default = OutputPreferences(
119 wrapText: stdio.hasTerminal,
120 showColor: platform.stdoutSupportsAnsi,
121 stdio: stdio,
122);
123OutputPreferences get outputPreferences => context.get<OutputPreferences>() ?? _default;
124
125/// The current system clock instance.
126SystemClock get systemClock => context.get<SystemClock>() ?? _systemClock;
127SystemClock _systemClock = const SystemClock();
128
129ProcessInfo get processInfo => context.get<ProcessInfo>()!;
130
131/// Display an error level message to the user. Commands should use this if they
132/// fail in some way.
133///
134/// Set [emphasis] to true to make the output bold if it's supported.
135/// Set [color] to a [TerminalColor] to color the output, if the logger
136/// supports it. The [color] defaults to [TerminalColor.red].
137void printError(
138 String message, {
139 StackTrace? stackTrace,
140 bool? emphasis,
141 TerminalColor? color,
142 int? indent,
143 int? hangingIndent,
144 bool? wrap,
145}) {
146 logger.printError(
147 message,
148 stackTrace: stackTrace,
149 emphasis: emphasis ?? false,
150 color: color,
151 indent: indent,
152 hangingIndent: hangingIndent,
153 wrap: wrap,
154 );
155}
156
157/// Display a warning level message to the user. Commands should use this if they
158/// have important warnings to convey that aren't fatal.
159///
160/// Set [emphasis] to true to make the output bold if it's supported.
161/// Set [color] to a [TerminalColor] to color the output, if the logger
162/// supports it. The [color] defaults to [TerminalColor.cyan].
163void printWarning(
164 String message, {
165 bool? emphasis,
166 TerminalColor? color,
167 int? indent,
168 int? hangingIndent,
169 bool? wrap,
170}) {
171 logger.printWarning(
172 message,
173 emphasis: emphasis ?? false,
174 color: color,
175 indent: indent,
176 hangingIndent: hangingIndent,
177 wrap: wrap,
178 );
179}
180
181/// Display normal output of the command. This should be used for things like
182/// progress messages, success messages, or just normal command output.
183///
184/// Set `emphasis` to true to make the output bold if it's supported.
185///
186/// Set `newline` to false to skip the trailing linefeed.
187///
188/// If `indent` is provided, each line of the message will be prepended by the
189/// specified number of whitespaces.
190void printStatus(
191 String message, {
192 bool? emphasis,
193 bool? newline,
194 TerminalColor? color,
195 int? indent,
196 int? hangingIndent,
197 bool? wrap,
198}) {
199 logger.printStatus(
200 message,
201 emphasis: emphasis ?? false,
202 color: color,
203 newline: newline ?? true,
204 indent: indent,
205 hangingIndent: hangingIndent,
206 wrap: wrap,
207 );
208}
209
210/// Display the [message] inside a box.
211///
212/// For example, this is the generated output:
213///
214/// ┌─ [title] ─┐
215/// │ [message] │
216/// └───────────┘
217///
218/// If a terminal is attached, the lines in [message] are automatically wrapped based on
219/// the available columns.
220void printBox(String message, {String? title}) {
221 logger.printBox(message, title: title);
222}
223
224/// Use this for verbose tracing output. Users can turn this output on in order
225/// to help diagnose issues with the toolchain or with their setup.
226void printTrace(String message) => logger.printTrace(message);
227
228AnsiTerminal get terminal {
229 return context.get<AnsiTerminal>() ?? _defaultAnsiTerminal;
230}
231
232final AnsiTerminal _defaultAnsiTerminal = AnsiTerminal(
233 stdio: stdio,
234 platform: platform,
235 now: DateTime.now(),
236 shutdownHooks: shutdownHooks,
237);
238
239/// The global Stdio wrapper.
240Stdio get stdio => context.get<Stdio>() ?? (_stdioInstance ??= Stdio());
241Stdio? _stdioInstance;
242
243PlistParser get plistParser =>
244 context.get<PlistParser>() ??
245 (_plistInstance ??= PlistParser(
246 fileSystem: fs,
247 processManager: processManager,
248 logger: logger,
249 ));
250PlistParser? _plistInstance;
251
252/// The global template renderer.
253TemplateRenderer get templateRenderer => context.get<TemplateRenderer>()!;
254
255/// Global [ShutdownHooks] that should be run before the tool process exits.
256///
257/// This is depended on by [localFileSystem] which is called before any
258/// [Context] is set up, and thus this cannot be a Context getter.
259final ShutdownHooks shutdownHooks = ShutdownHooks();
260
261// Unless we're in a test of this class's signal handling features, we must
262// have only one instance created with the singleton LocalSignals instance
263// and the catchable signals it considers to be fatal.
264LocalFileSystem? _instance;
265LocalFileSystem get localFileSystem =>
266 _instance ??= LocalFileSystem(LocalSignals.instance, Signals.defaultExitSignals, shutdownHooks);
267
268/// Gradle utils in the current [AppContext].
269GradleUtils? get gradleUtils => context.get<GradleUtils>();
270
271CocoaPods? get cocoaPods => context.get<CocoaPods>();
272
273FlutterProjectFactory get projectFactory {
274 return context.get<FlutterProjectFactory>() ??
275 FlutterProjectFactory(logger: logger, fileSystem: fs);
276}
277
278CustomDevicesConfig get customDevicesConfig => context.get<CustomDevicesConfig>()!;
279
280PreRunValidator get preRunValidator =>
281 context.get<PreRunValidator>() ?? const NoOpPreRunValidator();
282
283// Used to build RegExp instances which can detect the VM service message.
284final RegExp kVMServiceMessageRegExp = RegExp(
285 r'The Dart VM service is listening on ((http|//)[a-zA-Z0-9:/=_\-\.\[\]]+)',
286);
287
288/// Contains information about the JRE/JDK to use for Java-dependent operations.
289///
290/// A value of [null] indicates that no installation of java could be found on
291/// the host machine.
292Java? get java => context.get<Java>();
293
294TestCompilerNativeAssetsBuilder? get nativeAssetsBuilder =>
295 context.get<TestCompilerNativeAssetsBuilder>();
296

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com