create static method

Future<AnalysisServer> create({
  1. String? sdkPath,
  2. String? scriptPath,
  3. void onRead(
    1. String str
    )?,
  4. void onWrite(
    1. String str
    )?,
  5. List<String>? vmArgs,
  6. List<String>? serverArgs,
  7. String? clientId,
  8. String? clientVersion,
  9. Map<String, String>? processEnvironment,
})

Create and connect to a new analysis server instance.

  • sdkPath override the default sdk path
  • scriptPath override the default entry-point script to use for the analysis server
  • onRead called every time data is read from the server
  • onWrite called every time data is written to the server

Implementation

static Future<AnalysisServer> create(
    {String? sdkPath,
    String? scriptPath,
    void Function(String str)? onRead,
    void Function(String str)? onWrite,
    List<String>? vmArgs,
    List<String>? serverArgs,
    String? clientId,
    String? clientVersion,
    Map<String, String>? processEnvironment}) async {
  Completer<int> processCompleter = new Completer();

  String vmPath;
  if (sdkPath != null) {
    vmPath =
        path.join(sdkPath, 'bin', Platform.isWindows ? 'dart.exe' : 'dart');
  } else {
    sdkPath = path.dirname(path.dirname(Platform.resolvedExecutable));
    vmPath = Platform.resolvedExecutable;
  }
  scriptPath ??= '$sdkPath/bin/snapshots/analysis_server.dart.snapshot';
  if (!File(scriptPath).existsSync()) {
    throw "The analysis_server snapshot doesn't exist at '$scriptPath', "
        "consider passing `sdkPath` to `AnalysisServer.create`.";
  }

  List<String> args = [scriptPath, '--sdk', sdkPath];
  if (vmArgs != null) args.insertAll(0, vmArgs);
  if (serverArgs != null) args.addAll(serverArgs);
  if (clientId != null) args.add('--client-id=$clientId');
  if (clientVersion != null) args.add('--client-version=$clientVersion');

  Process process =
      await Process.start(vmPath, args, environment: processEnvironment);
  unawaited(process.exitCode.then((code) => processCompleter.complete(code)));

  Stream<String> inStream = process.stdout
      .transform(utf8.decoder)
      .transform(const LineSplitter())
      .map((String message) {
    if (onRead != null) onRead(message);
    return message;
  });

  AnalysisServer server = new AnalysisServer(inStream, (String message) {
    if (onWrite != null) onWrite(message);
    process.stdin.writeln(message);
  }, processCompleter, process.kill);

  return server;
}