Skip to content

Commit 0010cc4

Browse files
kenzieschmollCommit Queue
authored andcommitted
Use constants instead of raw strings throughout package:dtd
Change-Id: Ie4b0dfb115a820fb3f0741ce193c70f6b7d560cc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/432725 Reviewed-by: Ben Konyi <[email protected]> Commit-Queue: Kenzie Davisson <[email protected]>
1 parent b1751e6 commit 0010cc4

19 files changed

+382
-242
lines changed

pkg/dds/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
collection: ^1.15.0
1717
dds_service_extensions: ^2.0.0
1818
dap: ^1.4.0
19-
dtd: ^3.0.0
19+
dtd: ^4.0.0
2020
extension_discovery: ^2.0.0
2121
devtools_shared: ^11.0.0
2222
http_multi_server: ^3.0.0

pkg/dtd/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 4.0.0
2+
- **Breaking Change**: rename `EventParameters` constants to `DtdParameters`.
3+
- **Breaking Change**: delete the `kFileSystemServiceName` constant in favor of
4+
`FileSystemServiceConstants.serviceName`.
5+
- **Breaking Change**: delete the `kUnifiedAnalyticsServiceName` constant in
6+
favor of `UnifiedAnalyticsServiceConstants.serviceName`.
7+
- Added `CoreDtdServiceConstants` and `FileSystemServiceConstants` for shared
8+
use among DTD clients.
9+
110
## 3.0.0
211

312
- Added `ConnectedAppService` to store the connections to Dart and Flutter

pkg/dtd/lib/src/constants.dart

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,30 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Service name for the DTD-hosted file system service.
6-
const String kFileSystemServiceName = 'FileSystem';
5+
/// Constants used by the DTD-hosted file system service.
6+
extension FileSystemServiceConstants on Never {
7+
/// Service name for the DTD-hosted file system service.
8+
static const serviceName = 'FileSystem';
9+
10+
/// Service method name for the method that returns the IDE workspace roots.
11+
static const getIDEWorkspaceRoots = 'getIDEWorkspaceRoots';
12+
13+
/// Service method name for the method that returns the project roots
14+
/// contained within the current set of IDE workspace roots.
15+
static const getProjectRoots = 'getProjectRoots';
16+
17+
/// Service method name for the method that lists the contents of a directory.
18+
static const listDirectoryContents = 'listDirectoryContents';
19+
20+
/// Service method name for the method that reads a file as a string.
21+
static const readFileAsString = 'readFileAsString';
22+
23+
/// Service method name for the method that sets the IDE workspace roots.
24+
static const setIDEWorkspaceRoots = 'setIDEWorkspaceRoots';
25+
26+
/// Service method name for the method that writes a file as a string.
27+
static const writeFileAsString = 'writeFileAsString';
28+
}
729

830
/// The default value for the `depth` parameter in the
931
/// `DartToolingDaemon.getProjectRoots` API.
@@ -14,18 +36,62 @@ const String kFileSystemServiceName = 'FileSystem';
1436
/// a user opened their home directory in their IDE.
1537
const int defaultGetProjectRootsDepth = 4;
1638

17-
/// Service name for the DTD-hosted unified analytics service.
18-
const String kUnifiedAnalyticsServiceName = 'UnifiedAnalytics';
39+
/// Constants used by the DTD-hosted unified analytics service.
40+
///
41+
/// This service is intentionally not exposed by package:dtd and should not be
42+
/// used by arbitrary clients.
43+
extension UnifiedAnalyticsServiceConstants on Never {
44+
/// Service name for the DTD-hosted unified analytics service.
45+
static const serviceName = 'UnifiedAnalytics';
46+
47+
/// Service method name for the method that confirms that a unified analytics
48+
/// client showed the required consent message.
49+
static const clientShowedMessage = 'clientShowedMessage';
50+
51+
/// Service method name for the method that returns the unified analytics
52+
/// consent message to prompt users with.
53+
static const getConsentMessage = 'getConsentMessage';
54+
55+
/// Service method name for the method that sends an event to unified
56+
/// analytics.
57+
static const send = 'send';
58+
59+
/// Service method name for the method that sets the enabled status of
60+
/// unified analytics telemetry.
61+
static const setTelemetry = 'setTelemetry';
62+
63+
/// Service method name for the method that determines whether the unified
64+
/// analytics client should display the consent message.
65+
static const shouldShowMessage = 'shouldShowMessage';
66+
67+
/// Service method name for the method that returns whether unified analytics
68+
/// telemetry is enabled.
69+
static const telemetryEnabled = 'telemetryEnabled';
70+
}
1971

2072
/// Constants used as parameter names across various DTD APIs.
21-
extension EventParameters on Never {
73+
extension DtdParameters on Never {
74+
static const capabilities = 'capabilities';
75+
static const contents = 'contents';
76+
static const data = 'data';
77+
static const depth = 'depth';
78+
static const enable = 'enable';
79+
static const encoding = 'encoding';
80+
static const event = 'event';
2281
static const eventData = 'eventData';
2382
static const eventKind = 'eventKind';
2483
static const exposedUri = 'exposedUri';
84+
static const kind = 'kind';
85+
static const method = 'method';
2586
static const name = 'name';
87+
static const roots = 'roots';
2688
static const secret = 'secret';
89+
static const service = 'service';
90+
static const stream = 'stream';
2791
static const streamId = 'streamId';
2892
static const timestamp = 'timestamp';
93+
static const tool = 'tool';
94+
static const type = 'type';
2995
static const uri = 'uri';
3096
}
3197

@@ -58,3 +124,43 @@ extension ConnectedAppServiceConstants on Never {
58124
/// service shuts down.
59125
static const vmServiceUnregistered = 'VmServiceUnregistered';
60126
}
127+
128+
/// Constants used by the core services provided by DTD.
129+
extension CoreDtdServiceConstants on Never {
130+
/// Service method name for the method that returns all the currently
131+
/// registered services available on this DTD instance.
132+
static const getRegisteredServices = 'getRegisteredServices';
133+
134+
/// Service method name for the method that posts an event to a stream.
135+
static const postEvent = 'postEvent';
136+
137+
/// Service method name for the method that a DTD client can call to register
138+
/// a handler for a service method.
139+
static const registerService = 'registerService';
140+
141+
/// The name of the stream for events related to new services/methods being
142+
/// added and removed.
143+
///
144+
/// This stream is not part of the VM service protocol.
145+
static const servicesStreamId = 'Service';
146+
147+
/// The kind of the event sent over the [servicesStreamId] stream when a new
148+
/// service method is registered.
149+
static const serviceRegisteredKind = 'ServiceRegistered';
150+
151+
/// The kind of the event sent over the [servicesStreamId] stream when a
152+
/// service method is unregistered.
153+
static const serviceUnregisteredKind = 'ServiceUnregistered';
154+
155+
/// Service method name for the method that cancels a DTD client's
156+
/// subscription to a stream.
157+
static const streamCancel = 'streamCancel';
158+
159+
/// Service method name for the method that a DTD client can call to subscribe
160+
/// to a stream.
161+
static const streamListen = 'streamListen';
162+
163+
/// Service method name that for the method that notifies any stream
164+
/// subscriptions that an event was posted to a stream.
165+
static const streamNotify = 'streamNotify';
166+
}

pkg/dtd/lib/src/dart_tooling_daemon.dart

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ typedef DTDServiceCallback = Future<Map<String, Object?>> Function(
1515
Parameters params,
1616
);
1717

18-
// TODO(kenz): replace all raw string values with constants from
19-
// `constants.dart` that can be shared with the dtd_impl package.
20-
2118
// TODO(danchevalier): add a serviceMethodIsAvailable experience. it will listen
2219
// to a stream that announces servicemethods getting registered and
2320
// unregistered. The state can then be presented as a listenable so that clients
@@ -33,13 +30,14 @@ class DartToolingDaemon {
3330
/// To over a WebSocket, the [DartToolingDaemon.connect] helper can be used.
3431
DartToolingDaemon.fromStreamChannel(StreamChannel<String> streamChannel)
3532
: _clientPeer = Peer(streamChannel) {
36-
_clientPeer.registerMethod('streamNotify', (Parameters params) {
33+
_clientPeer.registerMethod(CoreDtdServiceConstants.streamNotify,
34+
(Parameters params) {
3735
try {
38-
final streamId = params[EventParameters.streamId].asString;
39-
final eventKind = params[EventParameters.eventKind].asString;
36+
final streamId = params[DtdParameters.streamId].asString;
37+
final eventKind = params[DtdParameters.eventKind].asString;
4038
final eventData =
41-
params[EventParameters.eventData].asMap as Map<String, Object?>;
42-
final timestamp = params[EventParameters.timestamp].asInt;
39+
params[DtdParameters.eventData].asMap as Map<String, Object?>;
40+
final timestamp = params[DtdParameters.timestamp].asInt;
4341

4442
_subscribedStreamControllers[streamId]?.add(
4543
DTDEvent(
@@ -105,10 +103,10 @@ class DartToolingDaemon {
105103
Map<String, Object?>? capabilities,
106104
}) async {
107105
final combinedName = '$service.$method';
108-
await _clientPeer.sendRequest('registerService', {
109-
'service': service,
110-
'method': method,
111-
if (capabilities != null) 'capabilities': capabilities,
106+
await _clientPeer.sendRequest(CoreDtdServiceConstants.registerService, {
107+
DtdParameters.service: service,
108+
DtdParameters.method: method,
109+
if (capabilities != null) DtdParameters.capabilities: capabilities,
112110
});
113111

114112
_clientPeer.registerMethod(
@@ -121,7 +119,7 @@ class DartToolingDaemon {
121119
/// available on this DTD instance.
122120
Future<RegisteredServicesResponse> getRegisteredServices() async {
123121
final json = await _clientPeer.sendRequest(
124-
'getRegisteredServices',
122+
CoreDtdServiceConstants.getRegisteredServices,
125123
) as Map<String, Object?>;
126124

127125
final dtdResponse = _dtdResponseFromJson(json);
@@ -138,9 +136,9 @@ class DartToolingDaemon {
138136
/// [RpcErrorCodes.kStreamAlreadySubscribed] will be thrown.
139137
Future<void> streamListen(String streamId) {
140138
return _clientPeer.sendRequest(
141-
'streamListen',
139+
CoreDtdServiceConstants.streamListen,
142140
{
143-
'streamId': streamId,
141+
DtdParameters.streamId: streamId,
144142
},
145143
);
146144
}
@@ -154,9 +152,9 @@ class DartToolingDaemon {
154152
/// [RpcErrorCodes.kStreamNotSubscribed] will be thrown.
155153
Future<void> streamCancel(String streamId) {
156154
return _clientPeer.sendRequest(
157-
'streamCancel',
155+
CoreDtdServiceConstants.streamCancel,
158156
{
159-
'streamId': streamId,
157+
DtdParameters.streamId: streamId,
160158
},
161159
);
162160
}
@@ -186,11 +184,11 @@ class DartToolingDaemon {
186184
Map<String, Object?> eventData,
187185
) async {
188186
await _clientPeer.sendRequest(
189-
'postEvent',
187+
CoreDtdServiceConstants.postEvent,
190188
{
191-
'streamId': streamId,
192-
'eventKind': eventKind,
193-
'eventData': eventData,
189+
DtdParameters.streamId: streamId,
190+
DtdParameters.eventKind: eventKind,
191+
DtdParameters.eventData: eventData,
194192
},
195193
);
196194
}
@@ -225,7 +223,7 @@ class DartToolingDaemon {
225223
}
226224

227225
DTDResponse _dtdResponseFromJson(Map<String, Object?> json) {
228-
final type = json['type'] as String?;
226+
final type = json[DtdParameters.type] as String?;
229227
if (type == null) {
230228
throw DartToolingDaemonConnectionException.callResponseMissingType(json);
231229
}
@@ -267,10 +265,10 @@ class DTDEvent {
267265
@override
268266
String toString() {
269267
return jsonEncode({
270-
'stream': stream,
271-
'timestamp': timestamp,
272-
'kind': kind,
273-
'data': data,
268+
DtdParameters.stream: stream,
269+
DtdParameters.timestamp: timestamp,
270+
DtdParameters.kind: kind,
271+
DtdParameters.data: data,
274272
});
275273
}
276274
}

pkg/dtd/lib/src/services/connected_app_service.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ extension ConnectedAppsExtension on DartToolingDaemon {
5454
return _callOnConnectedAppService<Success>(
5555
ConnectedAppServiceConstants.registerVmService,
5656
params: {
57-
EventParameters.uri: uri,
58-
EventParameters.secret: secret,
59-
if (exposedUri != null) EventParameters.exposedUri: exposedUri,
60-
if (name != null) EventParameters.name: name,
57+
DtdParameters.uri: uri,
58+
DtdParameters.secret: secret,
59+
if (exposedUri != null) DtdParameters.exposedUri: exposedUri,
60+
if (name != null) DtdParameters.name: name,
6161
},
6262
parseResponse: Success.fromDTDResponse,
6363
);
@@ -83,7 +83,7 @@ extension ConnectedAppsExtension on DartToolingDaemon {
8383
}) {
8484
return _callOnConnectedAppService<Success>(
8585
ConnectedAppServiceConstants.unregisterVmService,
86-
params: {EventParameters.uri: uri, EventParameters.secret: secret},
86+
params: {DtdParameters.uri: uri, DtdParameters.secret: secret},
8787
parseResponse: Success.fromDTDResponse,
8888
);
8989
}

pkg/dtd/lib/src/services/file_system_service.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ extension FileSystemService on DartToolingDaemon {
2929
Encoding encoding = utf8,
3030
}) async {
3131
final result = await call(
32-
kFileSystemServiceName,
33-
'readFileAsString',
32+
FileSystemServiceConstants.serviceName,
33+
FileSystemServiceConstants.readFileAsString,
3434
params: {
35-
'uri': uri.toString(),
36-
'encoding': encoding.name,
35+
DtdParameters.uri: uri.toString(),
36+
DtdParameters.encoding: encoding.name,
3737
},
3838
);
3939
return FileContent.fromDTDResponse(result);
@@ -56,12 +56,12 @@ extension FileSystemService on DartToolingDaemon {
5656
Encoding encoding = utf8,
5757
}) async {
5858
await call(
59-
kFileSystemServiceName,
60-
'writeFileAsString',
59+
FileSystemServiceConstants.serviceName,
60+
FileSystemServiceConstants.writeFileAsString,
6161
params: {
62-
'uri': uri.toString(),
63-
'contents': contents,
64-
'encoding': encoding.name,
62+
DtdParameters.uri: uri.toString(),
63+
DtdParameters.contents: contents,
64+
DtdParameters.encoding: encoding.name,
6565
},
6666
);
6767
}
@@ -81,10 +81,10 @@ extension FileSystemService on DartToolingDaemon {
8181
/// The returned uris will be `file://` Uris.
8282
Future<UriList> listDirectoryContents(Uri uri) async {
8383
final result = await call(
84-
kFileSystemServiceName,
85-
'listDirectoryContents',
84+
FileSystemServiceConstants.serviceName,
85+
FileSystemServiceConstants.listDirectoryContents,
8686
params: {
87-
'uri': uri.toString(),
87+
DtdParameters.uri: uri.toString(),
8888
},
8989
);
9090
return UriList.fromDTDResponse(result);
@@ -106,11 +106,11 @@ extension FileSystemService on DartToolingDaemon {
106106
/// with [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
107107
Future<void> setIDEWorkspaceRoots(String secret, List<Uri> roots) async {
108108
await call(
109-
kFileSystemServiceName,
110-
'setIDEWorkspaceRoots',
109+
FileSystemServiceConstants.serviceName,
110+
FileSystemServiceConstants.setIDEWorkspaceRoots,
111111
params: {
112-
'roots': roots.map<String>((e) => e.toString()).toList(),
113-
'secret': secret,
112+
DtdParameters.roots: roots.map<String>((e) => e.toString()).toList(),
113+
DtdParameters.secret: secret,
114114
},
115115
);
116116
}
@@ -120,8 +120,8 @@ extension FileSystemService on DartToolingDaemon {
120120
/// The returned uris will be `file://` Uris.
121121
Future<IDEWorkspaceRoots> getIDEWorkspaceRoots() async {
122122
final result = await call(
123-
kFileSystemServiceName,
124-
'getIDEWorkspaceRoots',
123+
FileSystemServiceConstants.serviceName,
124+
FileSystemServiceConstants.getIDEWorkspaceRoots,
125125
);
126126
return IDEWorkspaceRoots.fromDTDResponse(result);
127127
}
@@ -141,9 +141,9 @@ extension FileSystemService on DartToolingDaemon {
141141
int depth = defaultGetProjectRootsDepth,
142142
}) async {
143143
final result = await call(
144-
kFileSystemServiceName,
145-
'getProjectRoots',
146-
params: {'depth': depth},
144+
FileSystemServiceConstants.serviceName,
145+
FileSystemServiceConstants.getProjectRoots,
146+
params: {DtdParameters.depth: depth},
147147
);
148148
return UriList.fromDTDResponse(result);
149149
}

0 commit comments

Comments
 (0)