Skip to content

Commit 0ac63b6

Browse files
derekxu16Commit Queue
authored andcommitted
[package:frontend_server] Move ResidentCompilerInfo and sendAndReceiveResponse from package:dartdev to package:frontend_server's resident_frontend_server_utils.dart
Change-Id: Ie3c8ac69413d55e510d86c5b20f059d5d5301ec7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395660 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Ben Konyi <[email protected]>
1 parent 11c59c1 commit 0ac63b6

File tree

11 files changed

+128
-131
lines changed

11 files changed

+128
-131
lines changed

pkg/dartdev/lib/src/commands/compilation_server.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'dart:io' show File;
77

88
import 'package:args/args.dart';
99
import 'package:dartdev/src/generate_kernel.dart';
10+
import 'package:frontend_server/resident_frontend_server_utils.dart'
11+
show ResidentCompilerInfo;
1012

1113
import '../core.dart';
1214
import '../resident_frontend_constants.dart';

pkg/dartdev/lib/src/generate_kernel.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'dart:io';
66

77
import 'package:args/args.dart';
8+
import 'package:frontend_server/resident_frontend_server_utils.dart'
9+
show ResidentCompilerInfo, sendAndReceiveResponse;
810
import 'package:kernel/binary/tag.dart' show isValidSdkHash;
911
import 'package:path/path.dart' as p;
1012
import 'package:pub/pub.dart';

pkg/dartdev/lib/src/resident_frontend_utils.dart

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:convert';
6-
import 'dart:io' show File, FileSystemException, InternetAddress, Socket;
6+
import 'dart:io' show File, FileSystemException;
77

88
import 'package:args/args.dart';
9+
import 'package:frontend_server/resident_frontend_server_utils.dart'
10+
show sendAndReceiveResponse;
911
import 'package:path/path.dart' as p;
1012

1113
import 'commands/compilation_server.dart' show CompilationServerCommand;
@@ -51,51 +53,6 @@ File? getResidentCompilerInfoFileConsideringArgs(final ArgResults args) {
5153

5254
final String packageConfigName = p.join('.dart_tool', 'package_config.json');
5355

54-
final class ResidentCompilerInfo {
55-
final String? _sdkHash;
56-
final InternetAddress _address;
57-
final int _port;
58-
59-
/// The SDK hash that kernel files compiled using the Resident Frontend
60-
/// Compiler associated with this object will be stamped with.
61-
String? get sdkHash => _sdkHash;
62-
63-
/// The address that the Resident Frontend Compiler associated with this
64-
/// object is listening from.
65-
InternetAddress get address => _address;
66-
67-
/// The port number that the Resident Frontend Compiler associated with this
68-
/// object is listening on.
69-
int get port => _port;
70-
71-
/// Extracts the value associated with a key from [entries], where [entries]
72-
/// is a [String] with the format '$key1:$value1 $key2:$value2 $key3:$value3 ...'.
73-
static String _extractValueAssociatedWithKey(String entries, String key) =>
74-
RegExp('$key:' r'(\S+)(\s|$)').allMatches(entries).first[1]!;
75-
76-
static ResidentCompilerInfo fromFile(File file) {
77-
final fileContents = file.readAsStringSync();
78-
79-
return ResidentCompilerInfo._(
80-
sdkHash: fileContents.contains('sdkHash:')
81-
? _extractValueAssociatedWithKey(fileContents, 'sdkHash')
82-
: null,
83-
address: InternetAddress(
84-
_extractValueAssociatedWithKey(fileContents, 'address'),
85-
),
86-
port: int.parse(_extractValueAssociatedWithKey(fileContents, 'port')),
87-
);
88-
}
89-
90-
ResidentCompilerInfo._({
91-
required String? sdkHash,
92-
required int port,
93-
required InternetAddress address,
94-
}) : _sdkHash = sdkHash,
95-
_port = port,
96-
_address = address;
97-
}
98-
9956
/// Removes the [serverInfoFile].
10057
void cleanupResidentServerInfo(File serverInfoFile) {
10158
if (serverInfoFile.existsSync()) {
@@ -208,38 +165,6 @@ Future<bool> isFileAotSnapshot(final File file) async {
208165
return false;
209166
}
210167

211-
// TODO: when frontend_server is migrated to null safe Dart, everything
212-
// below this comment can be removed and imported from resident_frontend_server
213-
214-
/// Sends a compilation [request] to the Resident Frontend Compiler associated
215-
/// with [serverInfoFile], and returns the compiler's JSON response.
216-
///
217-
/// Throws a [FileSystemException] if [serverInfoFile] cannot be accessed.
218-
Future<Map<String, dynamic>> sendAndReceiveResponse(
219-
String request,
220-
File serverInfoFile,
221-
) async {
222-
Socket? client;
223-
Map<String, dynamic> jsonResponse;
224-
final residentCompilerInfo = ResidentCompilerInfo.fromFile(serverInfoFile);
225-
try {
226-
client = await Socket.connect(
227-
residentCompilerInfo.address,
228-
residentCompilerInfo.port,
229-
);
230-
client.write(request);
231-
final data = String.fromCharCodes(await client.first);
232-
jsonResponse = jsonDecode(data);
233-
} catch (e) {
234-
jsonResponse = <String, dynamic>{
235-
responseSuccessString: false,
236-
responseErrorString: e.toString(),
237-
};
238-
}
239-
client?.destroy();
240-
return jsonResponse;
241-
}
242-
243168
/// Used to create compile requests for the run CLI command.
244169
///
245170
/// Returns a JSON string that the resident compiler will be able to interpret.

pkg/dartdev/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
dds_service_extensions: any
2222
dtd_impl: any
2323
front_end: any
24+
frontend_server: any
2425
http: any
2526
kernel: any
2627
logging: any

pkg/dartdev/test/commands/run_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'dart:io';
88

99
import 'package:dartdev/src/resident_frontend_constants.dart';
1010
import 'package:dartdev/src/resident_frontend_utils.dart';
11+
import 'package:frontend_server/resident_frontend_server_utils.dart'
12+
show ResidentCompilerInfo, sendAndReceiveResponse;
1113
import 'package:kernel/binary/tag.dart' show sdkHashNull;
1214
import 'package:path/path.dart' as path;
1315
import 'package:pub_semver/pub_semver.dart';

pkg/dartdev/test/resident_frontend_utils_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'dart:io';
66

77
import 'package:dartdev/src/resident_frontend_utils.dart';
8+
import 'package:frontend_server/resident_frontend_server_utils.dart'
9+
show ResidentCompilerInfo;
810
import 'package:path/path.dart' as path;
911
import 'package:test/test.dart';
1012
import 'utils.dart';

pkg/front_end/test/spell_checking_list_common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,6 +2941,7 @@ stable
29412941
stack
29422942
stacked
29432943
stage
2944+
stamped
29442945
stamps
29452946
standalone
29462947
standard

pkg/frontend_server/OWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ file:/tools/OWNERS_CFE
44
file:/OWNERS
55

66
# Add the VM team as owners of ResidentFrontendServer-related files.
7-
per-file lib/src/resident_frontend_server.dart,test/src/resident_frontend_server_test.dart=file:/tools/OWNERS_VM
7+
per-file lib/resident_frontend_server_utils.dart,lib/src/resident_frontend_server.dart,test/src/resident_frontend_server_test.dart=file:/tools/OWNERS_VM
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:convert' show jsonDecode;
6+
import 'dart:io' show File, InternetAddress, Socket;
7+
8+
final class ResidentCompilerInfo {
9+
final String? _sdkHash;
10+
final InternetAddress _address;
11+
final int _port;
12+
13+
/// The SDK hash that kernel files compiled using the Resident Frontend
14+
/// Compiler associated with this object will be stamped with.
15+
String? get sdkHash => _sdkHash;
16+
17+
/// The address that the Resident Frontend Compiler associated with this
18+
/// object is listening from.
19+
InternetAddress get address => _address;
20+
21+
/// The port number that the Resident Frontend Compiler associated with this
22+
/// object is listening on.
23+
int get port => _port;
24+
25+
/// Extracts the value associated with a key from [entries], where [entries]
26+
/// is a [String] with the format '$key1:$value1 $key2:$value2 ...'.
27+
static String _extractValueAssociatedWithKey(String entries, String key) =>
28+
new RegExp('$key:' r'(\S+)(\s|$)').allMatches(entries).first[1]!;
29+
30+
static ResidentCompilerInfo fromFile(File file) {
31+
final String fileContents = file.readAsStringSync();
32+
33+
return new ResidentCompilerInfo._(
34+
sdkHash: fileContents.contains('sdkHash:')
35+
? _extractValueAssociatedWithKey(fileContents, 'sdkHash')
36+
: null,
37+
address: new InternetAddress(
38+
_extractValueAssociatedWithKey(
39+
fileContents,
40+
'address',
41+
),
42+
),
43+
port: int.parse(_extractValueAssociatedWithKey(fileContents, 'port')),
44+
);
45+
}
46+
47+
ResidentCompilerInfo._({
48+
required String? sdkHash,
49+
required int port,
50+
required InternetAddress address,
51+
}) : _sdkHash = sdkHash,
52+
_port = port,
53+
_address = address;
54+
}
55+
56+
/// Sends a compilation [request] to the resident frontend compiler associated
57+
/// with [serverInfoFile], and returns the compiler's JSON response.
58+
///
59+
/// Throws a [FileSystemException] if [serverInfoFile] cannot be accessed.
60+
Future<Map<String, dynamic>> sendAndReceiveResponse(
61+
String request,
62+
File serverInfoFile,
63+
) async {
64+
Socket? client;
65+
Map<String, dynamic> jsonResponse;
66+
final ResidentCompilerInfo residentCompilerInfo =
67+
ResidentCompilerInfo.fromFile(serverInfoFile);
68+
69+
try {
70+
client = await Socket.connect(
71+
residentCompilerInfo.address,
72+
residentCompilerInfo.port,
73+
);
74+
client.write(request);
75+
final String data = new String.fromCharCodes(await client.first);
76+
jsonResponse = jsonDecode(data);
77+
} catch (e) {
78+
jsonResponse = <String, dynamic>{
79+
'success': false,
80+
'errorMessage': e.toString(),
81+
};
82+
}
83+
client?.destroy();
84+
return jsonResponse;
85+
}

pkg/frontend_server/lib/src/resident_frontend_server.dart

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -392,32 +392,6 @@ class ResidentFrontendServer {
392392
}
393393
}
394394

395-
/// Sends the JSON string [request] to the resident frontend server
396-
/// and returns server's response in JSON
397-
///
398-
/// Clients must use this function when wanting to interact with a
399-
/// ResidentFrontendServer instance.
400-
Future<Map<String, dynamic>> sendAndReceiveResponse(
401-
InternetAddress address, int port, String request) async {
402-
Socket? client;
403-
Map<String, dynamic> jsonResponse;
404-
try {
405-
client = await Socket.connect(address, port);
406-
client.write(request);
407-
final String data = new String.fromCharCodes(await client.first);
408-
jsonResponse = jsonDecode(data);
409-
} catch (e) {
410-
jsonResponse = <String, Object>{
411-
'success': false,
412-
'errorMessage': e.toString(),
413-
};
414-
}
415-
if (client != null) {
416-
client.destroy();
417-
}
418-
return jsonResponse;
419-
}
420-
421395
/// Closes the ServerSocket and removes the [serverInfoFile] that is used
422396
/// to access this instance of the Resident Frontend Server.
423397
Future<void> residentServerCleanup(

0 commit comments

Comments
 (0)