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. Uri? 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,
  Uri? google3WorkspaceRoot,
}) async {
  final process = await Process.start(
    dartExecutable ?? Platform.executable,
    <String>[
      'development-service',
      '--${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',
    ],
  );
  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;
}