@@ -6,36 +6,20 @@ import 'dart:convert';
66import 'dart:io' ;
77import 'dart:typed_data' ;
88
9- import 'package:pool/pool.dart' ;
10-
119/// The filesystem the build is running on.
1210///
1311/// Methods behave as the `dart:io` methods with the same names, with some
1412/// exceptions noted in the docs.
1513abstract interface class Filesystem {
16- /// Whether the file exists.
17- Future <bool > exists (String path);
18-
1914 /// Whether the file exists.
2015 bool existsSync (String path);
2116
22- /// Reads a file as a string.
23- Future <String > readAsString (String path, {Encoding encoding = utf8});
24-
2517 /// Reads a file as a string.
2618 String readAsStringSync (String path, {Encoding encoding = utf8});
2719
28- /// Reads a file as bytes.
29- Future <Uint8List > readAsBytes (String path);
30-
3120 /// Reads a file as bytes.
3221 Uint8List readAsBytesSync (String path);
3322
34- /// Deletes a file.
35- ///
36- /// If the file does not exist, does nothing.
37- Future <void > delete (String path);
38-
3923 /// Deletes a file.
4024 ///
4125 /// If the file does not exist, does nothing.
@@ -44,7 +28,7 @@ abstract interface class Filesystem {
4428 /// Deletes a directory recursively.
4529 ///
4630 /// If the directory does not exist, does nothing.
47- Future < void > deleteDirectory (String path);
31+ void deleteDirectorySync (String path);
4832
4933 /// Writes a file.
5034 ///
@@ -55,48 +39,20 @@ abstract interface class Filesystem {
5539 Encoding encoding = utf8,
5640 });
5741
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-
6742 /// Writes a file.
6843 ///
6944 /// Creates enclosing directories as needed if they don't exist.
7045 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);
7646}
7747
7848/// The `dart:io` filesystem.
7949class 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-
8650 @override
8751 bool existsSync (String path) => File (path).existsSync ();
8852
89- @override
90- Future <Uint8List > readAsBytes (String path) =>
91- _pool.withResource (File (path).readAsBytes);
92-
9353 @override
9454 Uint8List readAsBytesSync (String path) => File (path).readAsBytesSync ();
9555
96- @override
97- Future <String > readAsString (String path, {Encoding encoding = utf8}) =>
98- _pool.withResource (() => File (path).readAsString (encoding: encoding));
99-
10056 @override
10157 String readAsStringSync (String path, {Encoding encoding = utf8}) =>
10258 File (path).readAsStringSync (encoding: encoding);
@@ -108,19 +64,9 @@ class IoFilesystem implements Filesystem {
10864 }
10965
11066 @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 );
12470 }
12571
12672 @override
@@ -134,15 +80,6 @@ class IoFilesystem implements Filesystem {
13480 file.writeAsBytesSync (contents);
13581 }
13682
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-
14683 @override
14784 void writeAsStringSync (
14885 String path,
@@ -153,19 +90,6 @@ class IoFilesystem implements Filesystem {
15390 file.parent.createSync (recursive: true );
15491 file.writeAsStringSync (contents, encoding: encoding);
15592 }
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- }
16993}
17094
17195/// An in-memory [Filesystem] .
@@ -175,53 +99,30 @@ class InMemoryFilesystem implements Filesystem {
17599 /// The paths to all files present on the filesystem.
176100 Iterable <String > get filePaths => _files.keys;
177101
178- @override
179- Future <bool > exists (String path) => Future .value (_files.containsKey (path));
180-
181102 @override
182103 bool existsSync (String path) => _files.containsKey (path);
183104
184- @override
185- Future <Uint8List > readAsBytes (String path) => Future .value (_files[path]! );
186-
187105 @override
188106 Uint8List readAsBytesSync (String path) => _files[path]! ;
189107
190- @override
191- Future <String > readAsString (String path, {Encoding encoding = utf8}) =>
192- Future .value (encoding.decode (_files[path]! ));
193-
194108 @override
195109 String readAsStringSync (String path, {Encoding encoding = utf8}) =>
196110 encoding.decode (_files[path]! );
197111
198- @override
199- Future <void > delete (String path) {
200- _files.remove (path);
201- return Future .value ();
202- }
203-
204112 @override
205113 void deleteSync (String path) => _files.remove (path);
206114
207115 @override
208- Future < void > deleteDirectory (String path) {
116+ void deleteDirectorySync (String path) {
209117 final prefix = '$path /' ;
210118 _files.removeWhere ((filePath, _) => filePath.startsWith (prefix));
211- return Future .value ();
212119 }
213120
214121 @override
215122 void writeAsBytesSync (String path, List <int > contents) {
216123 _files[path] = Uint8List .fromList (contents);
217124 }
218125
219- @override
220- Future <void > writeAsBytes (String path, List <int > contents) {
221- _files[path] = Uint8List .fromList (contents);
222- return Future .value ();
223- }
224-
225126 @override
226127 void writeAsStringSync (
227128 String path,
@@ -230,14 +131,4 @@ class InMemoryFilesystem implements Filesystem {
230131 }) {
231132 _files[path] = Uint8List .fromList (encoding.encode (contents));
232133 }
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- }
243134}
0 commit comments