|
| 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