start static method

Future<DartDevelopmentServiceLauncher> start({
  1. required Uri remoteVmServiceUri,
  2. Uri? serviceUri,
  3. bool enableAuthCodes = true,
  4. bool serveDevTools = false,
  5. Uri? devToolsServerAddress,
  6. bool enableServicePortFallback = false,
  7. List<String> cachedUserTags = const <String>[],
  8. String? dartExecutable,
  9. String? google3WorkspaceRoot,
})

Implementation

static Future<DartDevelopmentServiceLauncher> start({
  required Uri remoteVmServiceUri,
  Uri? serviceUri,
  bool enableAuthCodes = true,
  bool serveDevTools = false,
  Uri? devToolsServerAddress,
  bool enableServicePortFallback = false,
  List<String> cachedUserTags = const <String>[],
  String? dartExecutable,
  String? google3WorkspaceRoot,
}) async {
  var args = <String>[
    '--${DartDevelopmentServiceOptions.vmServiceUriOption}=$remoteVmServiceUri',
    if (serviceUri != null) ...<String>[
      '--${DartDevelopmentServiceOptions.bindAddressOption}=${serviceUri.host}',       '--${DartDevelopmentServiceOptions.bindPortOption}=${serviceUri.port}',
    ],
    if (!enableAuthCodes)
      '--${DartDevelopmentServiceOptions.disableServiceAuthCodesFlag}',
    if (serveDevTools)
      '--${DartDevelopmentServiceOptions.serveDevToolsFlag}',
    if (devToolsServerAddress != null)
      '--${DartDevelopmentServiceOptions.devToolsServerAddressOption}=$devToolsServerAddress',
    if (enableServicePortFallback)
      '--${DartDevelopmentServiceOptions.enableServicePortFallbackFlag}',
    for (final String tag in cachedUserTags)
      '--${DartDevelopmentServiceOptions.cachedUserTagsOption}=$tag',
    if (google3WorkspaceRoot != null)
      '--${DartDevelopmentServiceOptions.google3WorkspaceRootOption}=$google3WorkspaceRoot',
  ];
  late String executable;
  if (dartExecutable == null) {
    // If a dart executable is not specified and we are able to locate
    // the 'dartaotruntime' executable and the AOT snapshot for dds
    // then invoke it directly as it would avoid the additional hop
    // of going through the dart CLI process to invoke dds.
    executable = Platform.executable;
    var sdkPath = path.absolute(path.dirname(path.dirname(executable)), 'bin');
    var snapshotsDir = path.join(sdkPath, 'snapshots');
    final type = FileSystemEntity.typeSync(snapshotsDir);
    if (type != FileSystemEntityType.directory &&
        type != FileSystemEntityType.link) {
      // This is the less common case where the user is in
      // the checked out Dart SDK, and is executing `dart` via:
      // ./out/ReleaseX64/dart ... or in google3.
      sdkPath = path.absolute(path.dirname(executable));
      snapshotsDir = sdkPath;
    }
    final dartAotRuntime = path.absolute(
        sdkPath, Platform.isWindows ? 'dartaotruntime.exe' : 'dartaotruntime');
    final ddsAotSnapshot =
        path.absolute(snapshotsDir, 'dds_aot.dart.snapshot');
    if (File(dartAotRuntime).existsSync()
        && File(ddsAotSnapshot).existsSync()) {
      executable = dartAotRuntime;
      args = [ddsAotSnapshot, ...args];
    } else {
      args = ['development-service', ...args];
    }
  } else {
    executable = dartExecutable;
    args = ['development-service', ...args];
  }
  final process = await Process.start(executable, args);
  final completer = Completer<DartDevelopmentServiceLauncher>();
  late StreamSubscription<Object?> stderrSub;
  stderrSub = process.stderr
      .transform(utf8.decoder)
      .transform(json.decoder)
      .listen((Object? result) {
    if (result
        case {
          'state': 'started',
          'ddsUri': final String ddsUriStr,
        }) {
      final ddsUri = Uri.parse(ddsUriStr);
      final devToolsUriStr = result['devToolsUri'] as String?;
      final devToolsUri =
          devToolsUriStr == null ? null : Uri.parse(devToolsUriStr);
      final dtdUriStr =
          (result['dtd'] as Map<String, Object?>?)?['uri'] as String?;
      final dtdUri = dtdUriStr == null ? null : Uri.parse(dtdUriStr);

      completer.complete(
        DartDevelopmentServiceLauncher._(
          process: process,
          uri: ddsUri,
          devToolsUri: devToolsUri,
          dtdUri: dtdUri,
        ),
      );
    } else if (result
        case {
          'state': 'error',
          'error': final String error,
        }) {
      final Map<String, Object?>? exceptionDetails =
          result['ddsExceptionDetails'] as Map<String, Object?>?;
      completer.completeError(
        exceptionDetails != null
            ? DartDevelopmentServiceException.fromJson(exceptionDetails)
            : StateError(error),
      );
    } else {
      throw StateError('Unexpected result from DDS: $result');
    }
    stderrSub.cancel();
  });
  return completer.future;
}