create static method
Create and connect to a new analysis server instance.
sdkPath
override the default sdk pathscriptPath
override the default entry-point script to use for the analysis serveronRead
called every time data is read from the serveronWrite
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;
}