@@ -6,36 +6,20 @@ import 'dart:convert';
6
6
import 'dart:io' ;
7
7
import 'dart:typed_data' ;
8
8
9
- import 'package:pool/pool.dart' ;
10
-
11
9
/// The filesystem the build is running on.
12
10
///
13
11
/// Methods behave as the `dart:io` methods with the same names, with some
14
12
/// exceptions noted in the docs.
15
13
abstract interface class Filesystem {
16
- /// Whether the file exists.
17
- Future <bool > exists (String path);
18
-
19
14
/// Whether the file exists.
20
15
bool existsSync (String path);
21
16
22
- /// Reads a file as a string.
23
- Future <String > readAsString (String path, {Encoding encoding = utf8});
24
-
25
17
/// Reads a file as a string.
26
18
String readAsStringSync (String path, {Encoding encoding = utf8});
27
19
28
- /// Reads a file as bytes.
29
- Future <Uint8List > readAsBytes (String path);
30
-
31
20
/// Reads a file as bytes.
32
21
Uint8List readAsBytesSync (String path);
33
22
34
- /// Deletes a file.
35
- ///
36
- /// If the file does not exist, does nothing.
37
- Future <void > delete (String path);
38
-
39
23
/// Deletes a file.
40
24
///
41
25
/// If the file does not exist, does nothing.
@@ -44,7 +28,7 @@ abstract interface class Filesystem {
44
28
/// Deletes a directory recursively.
45
29
///
46
30
/// If the directory does not exist, does nothing.
47
- Future < void > deleteDirectory (String path);
31
+ void deleteDirectorySync (String path);
48
32
49
33
/// Writes a file.
50
34
///
@@ -55,48 +39,20 @@ abstract interface class Filesystem {
55
39
Encoding encoding = utf8,
56
40
});
57
41
58
- /// Writes a file.
59
- ///
60
- /// Creates enclosing directories as needed if they don't exist.
61
- Future <void > writeAsString (
62
- String path,
63
- String contents, {
64
- Encoding encoding = utf8,
65
- });
66
-
67
42
/// Writes a file.
68
43
///
69
44
/// Creates enclosing directories as needed if they don't exist.
70
45
void writeAsBytesSync (String path, List <int > contents);
71
-
72
- /// Writes a file.
73
- ///
74
- /// Creates enclosing directories as needed if they don't exist.
75
- Future <void > writeAsBytes (String path, List <int > contents);
76
46
}
77
47
78
48
/// The `dart:io` filesystem.
79
49
class IoFilesystem implements Filesystem {
80
- /// Pool for async file operations.
81
- final _pool = Pool (32 );
82
-
83
- @override
84
- Future <bool > exists (String path) => _pool.withResource (File (path).exists);
85
-
86
50
@override
87
51
bool existsSync (String path) => File (path).existsSync ();
88
52
89
- @override
90
- Future <Uint8List > readAsBytes (String path) =>
91
- _pool.withResource (File (path).readAsBytes);
92
-
93
53
@override
94
54
Uint8List readAsBytesSync (String path) => File (path).readAsBytesSync ();
95
55
96
- @override
97
- Future <String > readAsString (String path, {Encoding encoding = utf8}) =>
98
- _pool.withResource (() => File (path).readAsString (encoding: encoding));
99
-
100
56
@override
101
57
String readAsStringSync (String path, {Encoding encoding = utf8}) =>
102
58
File (path).readAsStringSync (encoding: encoding);
@@ -108,19 +64,9 @@ class IoFilesystem implements Filesystem {
108
64
}
109
65
110
66
@override
111
- Future <void > delete (String path) {
112
- return _pool.withResource (() async {
113
- final file = File (path);
114
- if (await file.exists ()) await file.delete ();
115
- });
116
- }
117
-
118
- @override
119
- Future <void > deleteDirectory (String path) {
120
- return _pool.withResource (() async {
121
- final directory = Directory (path);
122
- if (await directory.exists ()) await directory.delete (recursive: true );
123
- });
67
+ void deleteDirectorySync (String path) {
68
+ final directory = Directory (path);
69
+ if (directory.existsSync ()) directory.deleteSync (recursive: true );
124
70
}
125
71
126
72
@override
@@ -134,15 +80,6 @@ class IoFilesystem implements Filesystem {
134
80
file.writeAsBytesSync (contents);
135
81
}
136
82
137
- @override
138
- Future <void > writeAsBytes (String path, List <int > contents) {
139
- return _pool.withResource (() async {
140
- final file = File (path);
141
- await file.parent.create (recursive: true );
142
- await file.writeAsBytes (contents);
143
- });
144
- }
145
-
146
83
@override
147
84
void writeAsStringSync (
148
85
String path,
@@ -153,19 +90,6 @@ class IoFilesystem implements Filesystem {
153
90
file.parent.createSync (recursive: true );
154
91
file.writeAsStringSync (contents, encoding: encoding);
155
92
}
156
-
157
- @override
158
- Future <void > writeAsString (
159
- String path,
160
- String contents, {
161
- Encoding encoding = utf8,
162
- }) {
163
- return _pool.withResource (() async {
164
- final file = File (path);
165
- await file.parent.create (recursive: true );
166
- await file.writeAsString (contents, encoding: encoding);
167
- });
168
- }
169
93
}
170
94
171
95
/// An in-memory [Filesystem] .
@@ -175,53 +99,30 @@ class InMemoryFilesystem implements Filesystem {
175
99
/// The paths to all files present on the filesystem.
176
100
Iterable <String > get filePaths => _files.keys;
177
101
178
- @override
179
- Future <bool > exists (String path) => Future .value (_files.containsKey (path));
180
-
181
102
@override
182
103
bool existsSync (String path) => _files.containsKey (path);
183
104
184
- @override
185
- Future <Uint8List > readAsBytes (String path) => Future .value (_files[path]! );
186
-
187
105
@override
188
106
Uint8List readAsBytesSync (String path) => _files[path]! ;
189
107
190
- @override
191
- Future <String > readAsString (String path, {Encoding encoding = utf8}) =>
192
- Future .value (encoding.decode (_files[path]! ));
193
-
194
108
@override
195
109
String readAsStringSync (String path, {Encoding encoding = utf8}) =>
196
110
encoding.decode (_files[path]! );
197
111
198
- @override
199
- Future <void > delete (String path) {
200
- _files.remove (path);
201
- return Future .value ();
202
- }
203
-
204
112
@override
205
113
void deleteSync (String path) => _files.remove (path);
206
114
207
115
@override
208
- Future < void > deleteDirectory (String path) {
116
+ void deleteDirectorySync (String path) {
209
117
final prefix = '$path /' ;
210
118
_files.removeWhere ((filePath, _) => filePath.startsWith (prefix));
211
- return Future .value ();
212
119
}
213
120
214
121
@override
215
122
void writeAsBytesSync (String path, List <int > contents) {
216
123
_files[path] = Uint8List .fromList (contents);
217
124
}
218
125
219
- @override
220
- Future <void > writeAsBytes (String path, List <int > contents) {
221
- _files[path] = Uint8List .fromList (contents);
222
- return Future .value ();
223
- }
224
-
225
126
@override
226
127
void writeAsStringSync (
227
128
String path,
@@ -230,14 +131,4 @@ class InMemoryFilesystem implements Filesystem {
230
131
}) {
231
132
_files[path] = Uint8List .fromList (encoding.encode (contents));
232
133
}
233
-
234
- @override
235
- Future <void > writeAsString (
236
- String path,
237
- String contents, {
238
- Encoding encoding = utf8,
239
- }) {
240
- _files[path] = Uint8List .fromList (encoding.encode (contents));
241
- return Future .value ();
242
- }
243
134
}
0 commit comments