Skip to content

Commit 981924f

Browse files
kenzieschmollCommit Queue
authored andcommitted
[dtd] Refactor FileSystemService code.
No functional changes. Just moves the file system code to be in its own mixin like other internal services. Change-Id: Ib4d3c2cf2389ab983ed9dd69a347407f8ab64990 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429162 Commit-Queue: Kenzie Davisson <[email protected]> Reviewed-by: Ben Konyi <[email protected]>
1 parent a067d75 commit 981924f

File tree

3 files changed

+151
-135
lines changed

3 files changed

+151
-135
lines changed

pkg/dtd/lib/dtd.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ library;
88
export 'src/connected_app_service.dart';
99
export 'src/constants.dart';
1010
export 'src/dart_tooling_daemon.dart';
11+
export 'src/file_system/file_system_service.dart';
1112
export 'src/file_system/types.dart';
1213
export 'src/response_types.dart';
1314
export 'src/rpc_error_codes.dart';

pkg/dtd/lib/src/dart_tooling_daemon.dart

Lines changed: 0 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -213,141 +213,6 @@ class DartToolingDaemon {
213213
// TODO(danchevalier): Find out how to get access to the id.
214214
return DTDResponse('-1', type, json);
215215
}
216-
217-
/// Reads the file at [uri] from disk in the environment where the Dart
218-
/// Tooling Daemon is running.
219-
///
220-
/// If [uri] is not contained in the IDE workspace roots, then an
221-
/// [RpcException] with [RpcErrorCodes.kPermissionDenied] is thrown.
222-
///
223-
/// If [uri] does not exist, then an [RpcException] exception with error
224-
/// code [RpcErrorCodes.kFileDoesNotExist] is thrown.
225-
///
226-
/// If [uri] does not have a file scheme, then an [RpcException] with
227-
/// [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
228-
Future<FileContent> readFileAsString(
229-
Uri uri, {
230-
Encoding encoding = utf8,
231-
}) async {
232-
final result = await call(
233-
kFileSystemServiceName,
234-
'readFileAsString',
235-
params: {
236-
'uri': uri.toString(),
237-
'encoding': encoding.name,
238-
},
239-
);
240-
return FileContent.fromDTDResponse(result);
241-
}
242-
243-
/// Writes [contents] to the file at [uri] in the environment where the Dart
244-
/// Tooling Daemon is running.
245-
///
246-
/// The file will be created if it does not exist, and it will be overwritten
247-
/// if it already exist.
248-
///
249-
/// If [uri] is not contained in the IDE workspace roots, then an
250-
/// [RpcException] with [RpcErrorCodes.kPermissionDenied] is thrown.
251-
///
252-
/// If [uri] does not have a file scheme, then an [RpcException] with
253-
/// [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
254-
Future<void> writeFileAsString(
255-
Uri uri,
256-
String contents, {
257-
Encoding encoding = utf8,
258-
}) async {
259-
await call(
260-
kFileSystemServiceName,
261-
'writeFileAsString',
262-
params: {
263-
'uri': uri.toString(),
264-
'contents': contents,
265-
'encoding': encoding.name,
266-
},
267-
);
268-
}
269-
270-
/// Lists the directories and files under the directory at [uri] in the
271-
/// environment where the Dart Tooling Daemon is running.
272-
///
273-
/// If [uri] is not a directory, throws an [RpcException] exception with error
274-
/// code [RpcErrorCodes.kDirectoryDoesNotExist].
275-
///
276-
/// If [uri] is not contained in the IDE workspace roots, then an
277-
/// [RpcException] with [RpcErrorCodes.kPermissionDenied] is thrown.
278-
///
279-
/// If [uri] does not have a file scheme, then an [RpcException] with
280-
/// [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
281-
///
282-
/// The returned uris will be `file://` Uris.
283-
Future<UriList> listDirectoryContents(Uri uri) async {
284-
final result = await call(
285-
kFileSystemServiceName,
286-
'listDirectoryContents',
287-
params: {
288-
'uri': uri.toString(),
289-
},
290-
);
291-
return UriList.fromDTDResponse(result);
292-
}
293-
294-
/// Sets the IDE workspace roots for the FileSystem service.
295-
///
296-
/// This is a privileged RPC that require's a [secret], which is provided by
297-
/// the Dart Tooling Daemon, to be called successfully. This secret is
298-
/// generated by the daemon and provided to its spawner to ensure only trusted
299-
/// clients can set workspace roots. If [secret] is invalid, an [RpcException]
300-
/// with error code [RpcErrorCodes.kPermissionDenied] is thrown.
301-
///
302-
/// If [secret] does not match the secret created when Dart Tooling Daemon was
303-
/// created, then an [RpcException] with [RpcErrorCodes.kPermissionDenied] is
304-
/// thrown.
305-
///
306-
/// If one of the [roots] is missing a "file" scheme then an [RpcException]
307-
/// with [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
308-
Future<void> setIDEWorkspaceRoots(String secret, List<Uri> roots) async {
309-
await call(
310-
kFileSystemServiceName,
311-
'setIDEWorkspaceRoots',
312-
params: {
313-
'roots': roots.map<String>((e) => e.toString()).toList(),
314-
'secret': secret,
315-
},
316-
);
317-
}
318-
319-
/// Gets the IDE workspace roots for the FileSystem service.
320-
///
321-
/// The returned uris will be `file://` Uris.
322-
Future<IDEWorkspaceRoots> getIDEWorkspaceRoots() async {
323-
final result = await call(
324-
kFileSystemServiceName,
325-
'getIDEWorkspaceRoots',
326-
);
327-
return IDEWorkspaceRoots.fromDTDResponse(result);
328-
}
329-
330-
/// Gets the project roots contained within the current set of IDE workspace
331-
/// roots.
332-
///
333-
/// A project root is any directory that contains a 'pubspec.yaml' file. If
334-
/// IDE workspace roots are not set, or if there are no project roots within
335-
/// the IDE workspace roots, this method will return an empty [UriList].
336-
///
337-
/// [depth] is the maximum depth that each IDE workspace root directory tree
338-
/// will be searched for project roots.
339-
///
340-
/// The returned uris will be `file://` Uris.
341-
Future<UriList> getProjectRoots({
342-
int depth = defaultGetProjectRootsDepth,
343-
}) async {
344-
final result = await call(
345-
kFileSystemServiceName,
346-
'getProjectRoots',
347-
params: {'depth': depth},
348-
);
349-
return UriList.fromDTDResponse(result);
350-
}
351216
}
352217

353218
/// Represents the response of an RPC call to the Dart Tooling Daemon.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright (c) 2025, 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';
6+
7+
import 'package:json_rpc_2/json_rpc_2.dart' show RpcException;
8+
9+
import '../constants.dart';
10+
import '../dart_tooling_daemon.dart';
11+
12+
import '../rpc_error_codes.dart' show RpcErrorCodes;
13+
import 'types.dart';
14+
15+
extension FileSystemService on DartToolingDaemon {
16+
/// Reads the file at [uri] from disk in the environment where the Dart
17+
/// Tooling Daemon is running.
18+
///
19+
/// If [uri] is not contained in the IDE workspace roots, then an
20+
/// [RpcException] with [RpcErrorCodes.kPermissionDenied] is thrown.
21+
///
22+
/// If [uri] does not exist, then an [RpcException] exception with error
23+
/// code [RpcErrorCodes.kFileDoesNotExist] is thrown.
24+
///
25+
/// If [uri] does not have a file scheme, then an [RpcException] with
26+
/// [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
27+
Future<FileContent> readFileAsString(
28+
Uri uri, {
29+
Encoding encoding = utf8,
30+
}) async {
31+
final result = await call(
32+
kFileSystemServiceName,
33+
'readFileAsString',
34+
params: {
35+
'uri': uri.toString(),
36+
'encoding': encoding.name,
37+
},
38+
);
39+
return FileContent.fromDTDResponse(result);
40+
}
41+
42+
/// Writes [contents] to the file at [uri] in the environment where the Dart
43+
/// Tooling Daemon is running.
44+
///
45+
/// The file will be created if it does not exist, and it will be overwritten
46+
/// if it already exist.
47+
///
48+
/// If [uri] is not contained in the IDE workspace roots, then an
49+
/// [RpcException] with [RpcErrorCodes.kPermissionDenied] is thrown.
50+
///
51+
/// If [uri] does not have a file scheme, then an [RpcException] with
52+
/// [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
53+
Future<void> writeFileAsString(
54+
Uri uri,
55+
String contents, {
56+
Encoding encoding = utf8,
57+
}) async {
58+
await call(
59+
kFileSystemServiceName,
60+
'writeFileAsString',
61+
params: {
62+
'uri': uri.toString(),
63+
'contents': contents,
64+
'encoding': encoding.name,
65+
},
66+
);
67+
}
68+
69+
/// Lists the directories and files under the directory at [uri] in the
70+
/// environment where the Dart Tooling Daemon is running.
71+
///
72+
/// If [uri] is not a directory, throws an [RpcException] exception with error
73+
/// code [RpcErrorCodes.kDirectoryDoesNotExist].
74+
///
75+
/// If [uri] is not contained in the IDE workspace roots, then an
76+
/// [RpcException] with [RpcErrorCodes.kPermissionDenied] is thrown.
77+
///
78+
/// If [uri] does not have a file scheme, then an [RpcException] with
79+
/// [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
80+
///
81+
/// The returned uris will be `file://` Uris.
82+
Future<UriList> listDirectoryContents(Uri uri) async {
83+
final result = await call(
84+
kFileSystemServiceName,
85+
'listDirectoryContents',
86+
params: {
87+
'uri': uri.toString(),
88+
},
89+
);
90+
return UriList.fromDTDResponse(result);
91+
}
92+
93+
/// Sets the IDE workspace roots for the FileSystem service.
94+
///
95+
/// This is a privileged RPC that require's a [secret], which is provided by
96+
/// the Dart Tooling Daemon, to be called successfully. This secret is
97+
/// generated by the daemon and provided to its spawner to ensure only trusted
98+
/// clients can set workspace roots. If [secret] is invalid, an [RpcException]
99+
/// with error code [RpcErrorCodes.kPermissionDenied] is thrown.
100+
///
101+
/// If [secret] does not match the secret created when Dart Tooling Daemon was
102+
/// created, then an [RpcException] with [RpcErrorCodes.kPermissionDenied] is
103+
/// thrown.
104+
///
105+
/// If one of the [roots] is missing a "file" scheme then an [RpcException]
106+
/// with [RpcErrorCodes.kExpectsUriParamWithFileScheme] is thrown.
107+
Future<void> setIDEWorkspaceRoots(String secret, List<Uri> roots) async {
108+
await call(
109+
kFileSystemServiceName,
110+
'setIDEWorkspaceRoots',
111+
params: {
112+
'roots': roots.map<String>((e) => e.toString()).toList(),
113+
'secret': secret,
114+
},
115+
);
116+
}
117+
118+
/// Gets the IDE workspace roots for the FileSystem service.
119+
///
120+
/// The returned uris will be `file://` Uris.
121+
Future<IDEWorkspaceRoots> getIDEWorkspaceRoots() async {
122+
final result = await call(
123+
kFileSystemServiceName,
124+
'getIDEWorkspaceRoots',
125+
);
126+
return IDEWorkspaceRoots.fromDTDResponse(result);
127+
}
128+
129+
/// Gets the project roots contained within the current set of IDE workspace
130+
/// roots.
131+
///
132+
/// A project root is any directory that contains a 'pubspec.yaml' file. If
133+
/// IDE workspace roots are not set, or if there are no project roots within
134+
/// the IDE workspace roots, this method will return an empty [UriList].
135+
///
136+
/// [depth] is the maximum depth that each IDE workspace root directory tree
137+
/// will be searched for project roots.
138+
///
139+
/// The returned uris will be `file://` Uris.
140+
Future<UriList> getProjectRoots({
141+
int depth = defaultGetProjectRootsDepth,
142+
}) async {
143+
final result = await call(
144+
kFileSystemServiceName,
145+
'getProjectRoots',
146+
params: {'depth': depth},
147+
);
148+
return UriList.fromDTDResponse(result);
149+
}
150+
}

0 commit comments

Comments
 (0)