prepareToolInvocation function

ToolInvocation prepareToolInvocation(
  1. String executable,
  2. List<String> arguments,
  3. Map<String, String> environment, {
  4. FlutterToolchainResolver? flutterResolver,
  5. ToolRunnerMode runnerMode = ToolRunnerMode.auto,
})

Applies SDK-safe, non-persistent environment defaults to a tool launch.

Both Dart and Flutter accept the global --suppress-analytics option only before the subcommand. Flutter also honours this environment value in subprocesses that do not inherit the command-line option.

On Windows, when the logical tool is flutter, the generic preparation is replaced by FlutterToolchainResolver which bypasses the batch launcher and invokes the Flutter tool snapshot through the cached Dart SDK directly.

Implementation

ToolInvocation prepareToolInvocation(
  String executable,
  List<String> arguments,
  Map<String, String> environment, {
  FlutterToolchainResolver? flutterResolver,
  ToolRunnerMode runnerMode = ToolRunnerMode.auto,
}) {
  final tool = _toolName(executable);
  if (runnerMode == ToolRunnerMode.directSnapshot && tool != 'flutter') {
    throw const ToolInvocationException(
      diagnosticCode: 'ZUKE-FLUTTER-RUNNER-MODE-INVALID',
      message: 'directSnapshot is only supported for Flutter runners.',
    );
  }
  final preparedArguments = List<String>.from(arguments);
  final preparedEnvironment = Map<String, String>.from(environment);

  final useSnapshot =
      tool == 'flutter' &&
      (runnerMode == ToolRunnerMode.directSnapshot ||
          (runnerMode == ToolRunnerMode.auto && Platform.isWindows));
  if (useSnapshot) {
    final resolver = flutterResolver ?? const FlutterToolchainResolver();
    return resolver.resolve(executable, preparedArguments, preparedEnvironment);
  }

  if ((tool == 'dart' || tool == 'flutter') &&
      !preparedArguments.contains('--suppress-analytics')) {
    preparedArguments.insert(0, '--suppress-analytics');
  }
  if (tool == 'dart' &&
      preparedArguments.length > 1 &&
      preparedArguments[1].toLowerCase().endsWith('.dart')) {
    preparedArguments.insert(1, 'run');
  }
  if (tool == 'flutter') {
    preparedEnvironment['FLUTTER_SUPPRESS_ANALYTICS'] = 'true';
  }

  return ToolInvocation(
    executable: executable,
    arguments: List<String>.unmodifiable(preparedArguments),
    environment: Map<String, String>.unmodifiable(preparedEnvironment),
  );
}