resolveTestDartCommand function

TestDartCommand resolveTestDartCommand()

Implementation

TestDartCommand resolveTestDartCommand() {
  final executableName = Platform.isWindows ? 'dart.exe' : 'dart';
  final candidates = <String>[];
  final flutterRoot = Platform.environment['FLUTTER_ROOT'];
  if (flutterRoot != null && flutterRoot.isNotEmpty) {
    candidates.add(
      '$flutterRoot${Platform.pathSeparator}bin${Platform.pathSeparator}cache${Platform.pathSeparator}dart-sdk${Platform.pathSeparator}bin${Platform.pathSeparator}$executableName',
    );
  }

  var directory = File(Platform.resolvedExecutable).parent;
  for (var depth = 0; depth < 10; depth++) {
    candidates.add(
      '${directory.path}${Platform.pathSeparator}dart-sdk${Platform.pathSeparator}bin${Platform.pathSeparator}$executableName',
    );
    candidates.add(
      '${directory.path}${Platform.pathSeparator}cache${Platform.pathSeparator}dart-sdk${Platform.pathSeparator}bin${Platform.pathSeparator}$executableName',
    );
    final parent = directory.parent;
    if (parent.path == directory.path) break;
    directory = parent;
  }

  for (final candidate in candidates) {
    if (File(candidate).existsSync()) {
      return TestDartCommand(File(candidate).absolute.path);
    }
  }
  if (Platform.isWindows) {
    final where = Process.runSync('where.exe', ['dart.exe']);
    if (where.exitCode == 0) {
      final path = where.stdout.toString().split(RegExp(r'\r?\n')).first.trim();
      if (path.isNotEmpty) return TestDartCommand(path);
    }
  }
  return const TestDartCommand('dart');
}