@@ -8,58 +8,44 @@ import 'dart:typed_data';
8
8
9
9
import 'package:pool/pool.dart' ;
10
10
11
- import '../asset/id.dart' ;
12
- import 'asset_path_provider.dart' ;
13
- import 'filesystem_cache.dart' ;
14
-
15
11
/// The filesystem the build is running on.
16
12
///
17
13
/// Methods behave as the `dart:io` methods with the same names, with some
18
14
/// exceptions noted in the docs.
19
- ///
20
- /// Some methods cache, all uses of the cache are noted in the docs.
21
- ///
22
- /// The cache might be a [PassthroughFilesystemCache] in which case it has no
23
- /// effect.
24
- ///
25
- /// TODO(davidmorgan): extend caching to sync methods, deletes, writes.
26
15
abstract interface class Filesystem {
27
- FilesystemCache get cache;
28
-
29
- /// Returns a new instance with optionally updated [cache] .
30
- Filesystem copyWith ({FilesystemCache ? cache});
16
+ /// Whether the file exists.
17
+ Future <bool > exists (String path);
31
18
32
19
/// Whether the file exists.
33
- ///
34
- /// Uses [cache] .
35
- Future <bool > exists (AssetId id);
20
+ bool existsSync (String path);
36
21
37
22
/// Reads a file as a string.
38
- ///
39
- /// Uses [cache] . For `utf8` , the `String` is cached; for any other encoding
40
- /// the bytes are cached but the conversion runs on every read .
41
- Future < String > readAsString ( AssetId id , {Encoding encoding = utf8});
23
+ Future < String > readAsString ( String path, { Encoding encoding = utf8});
24
+
25
+ /// Reads a file as a string .
26
+ String readAsStringSync ( String path , {Encoding encoding = utf8});
42
27
43
28
/// Reads a file as bytes.
44
- ///
45
- /// Uses [cache] .
46
- Future <Uint8List > readAsBytes (AssetId id);
29
+ Future <Uint8List > readAsBytes (String path);
30
+
31
+ /// Reads a file as bytes.
32
+ Uint8List readAsBytesSync (String path);
47
33
48
34
/// Deletes a file.
49
35
///
50
36
/// If the file does not exist, does nothing.
51
- Future <void > delete (AssetId id );
37
+ Future <void > delete (String path );
52
38
53
39
/// Deletes a file.
54
40
///
55
41
/// If the file does not exist, does nothing.
56
- void deleteSync (AssetId id );
42
+ void deleteSync (String path );
57
43
58
44
/// Writes a file.
59
45
///
60
46
/// Creates enclosing directories as needed if they don't exist.
61
47
void writeAsStringSync (
62
- AssetId id ,
48
+ String path ,
63
49
String contents, {
64
50
Encoding encoding = utf8,
65
51
});
@@ -68,205 +54,165 @@ abstract interface class Filesystem {
68
54
///
69
55
/// Creates enclosing directories as needed if they don't exist.
70
56
Future <void > writeAsString (
71
- AssetId id ,
57
+ String path ,
72
58
String contents, {
73
59
Encoding encoding = utf8,
74
60
});
75
61
76
62
/// Writes a file.
77
63
///
78
64
/// Creates enclosing directories as needed if they don't exist.
79
- void writeAsBytesSync (AssetId id , List <int > contents);
65
+ void writeAsBytesSync (String path , List <int > contents);
80
66
81
67
/// Writes a file.
82
68
///
83
69
/// Creates enclosing directories as needed if they don't exist.
84
- Future <void > writeAsBytes (AssetId id , List <int > contents);
70
+ Future <void > writeAsBytes (String path , List <int > contents);
85
71
}
86
72
87
- /// A filesystem using [assetPathProvider] to map to the `dart:io` filesystem.
73
+ /// The `dart:io` filesystem.
88
74
class IoFilesystem implements Filesystem {
89
- @override
90
- final FilesystemCache cache;
91
-
92
- final AssetPathProvider assetPathProvider;
93
-
94
75
/// Pool for async file operations.
95
76
final _pool = Pool (32 );
96
77
97
- IoFilesystem ({
98
- required this .assetPathProvider,
99
- this .cache = const PassthroughFilesystemCache (),
100
- });
78
+ @override
79
+ Future <bool > exists (String path) => _pool.withResource (File (path).exists);
101
80
102
81
@override
103
- IoFilesystem copyWith ({FilesystemCache ? cache}) => IoFilesystem (
104
- assetPathProvider: assetPathProvider,
105
- cache: cache ?? this .cache,
106
- );
82
+ bool existsSync (String path) => File (path).existsSync ();
107
83
108
84
@override
109
- Future <bool > exists ( AssetId id ) =>
110
- cache. exists (id, ifAbsent : () => _pool.withResource (_fileFor (id).exists) );
85
+ Future <Uint8List > readAsBytes ( String path ) =>
86
+ _pool.withResource (File (path).readAsBytes );
111
87
112
88
@override
113
- Future <Uint8List > readAsBytes (AssetId id) => cache.readAsBytes (
114
- id,
115
- ifAbsent: () => _pool.withResource (_fileFor (id).readAsBytes),
116
- );
89
+ Uint8List readAsBytesSync (String path) => File (path).readAsBytesSync ();
117
90
118
91
@override
119
- Future <String > readAsString (AssetId id, {Encoding encoding = utf8}) async {
120
- // The cache only directly supports utf8, for other encodings get the
121
- // bytes via the cache then convert.
122
- if (encoding == utf8) {
123
- return cache.readAsString (
124
- id,
125
- ifAbsent: () => _pool.withResource (_fileFor (id).readAsString),
126
- );
127
- } else {
128
- return encoding.decode (await readAsBytes (id));
129
- }
130
- }
92
+ Future <String > readAsString (String path, {Encoding encoding = utf8}) =>
93
+ _pool.withResource (() => File (path).readAsString (encoding: encoding));
131
94
132
95
@override
133
- void deleteSync (AssetId id) {
134
- final file = _fileFor (id);
96
+ String readAsStringSync (String path, {Encoding encoding = utf8}) =>
97
+ File (path).readAsStringSync (encoding: encoding);
98
+
99
+ @override
100
+ void deleteSync (String path) {
101
+ final file = File (path);
135
102
if (file.existsSync ()) file.deleteSync ();
136
103
}
137
104
138
105
@override
139
- Future <void > delete (AssetId id ) {
106
+ Future <void > delete (String path ) {
140
107
return _pool.withResource (() async {
141
- final file = _fileFor (id );
108
+ final file = File (path );
142
109
if (await file.exists ()) await file.delete ();
143
110
});
144
111
}
145
112
146
113
@override
147
114
void writeAsBytesSync (
148
- AssetId id ,
115
+ String path ,
149
116
List <int > contents, {
150
117
Encoding encoding = utf8,
151
118
}) {
152
- final file = _fileFor (id );
119
+ final file = File (path );
153
120
file.parent.createSync (recursive: true );
154
121
file.writeAsBytesSync (contents);
155
122
}
156
123
157
124
@override
158
- Future <void > writeAsBytes (AssetId id , List <int > contents) {
125
+ Future <void > writeAsBytes (String path , List <int > contents) {
159
126
return _pool.withResource (() async {
160
- final file = _fileFor (id );
127
+ final file = File (path );
161
128
await file.parent.create (recursive: true );
162
129
await file.writeAsBytes (contents);
163
130
});
164
131
}
165
132
166
133
@override
167
134
void writeAsStringSync (
168
- AssetId id ,
135
+ String path ,
169
136
String contents, {
170
137
Encoding encoding = utf8,
171
138
}) {
172
- final file = _fileFor (id );
139
+ final file = File (path );
173
140
file.parent.createSync (recursive: true );
174
141
file.writeAsStringSync (contents, encoding: encoding);
175
142
}
176
143
177
144
@override
178
145
Future <void > writeAsString (
179
- AssetId id ,
146
+ String path ,
180
147
String contents, {
181
148
Encoding encoding = utf8,
182
149
}) {
183
150
return _pool.withResource (() async {
184
- final file = _fileFor (id );
151
+ final file = File (path );
185
152
await file.parent.create (recursive: true );
186
153
await file.writeAsString (contents, encoding: encoding);
187
154
});
188
155
}
189
-
190
- /// Returns a [File] for [id] for the current [assetPathProvider] .
191
- File _fileFor (AssetId id) {
192
- return File (assetPathProvider.pathFor (id));
193
- }
194
156
}
195
157
196
158
/// An in-memory [Filesystem] .
197
159
class InMemoryFilesystem implements Filesystem {
198
- @override
199
- FilesystemCache cache;
160
+ final Map <String , Uint8List > _files = {};
200
161
201
- final Map <AssetId , List <int >> assets;
162
+ /// The paths to all files present on the filesystem.
163
+ Iterable <String > get filePaths => _files.keys;
202
164
203
- InMemoryFilesystem ({FilesystemCache ? cache})
204
- : cache = cache ?? const PassthroughFilesystemCache (),
205
- assets = {};
165
+ @override
166
+ Future <bool > exists (String path) async => _files.containsKey (path);
206
167
207
- InMemoryFilesystem ._({required this .cache, required this .assets});
168
+ @override
169
+ bool existsSync (String path) => _files.containsKey (path);
208
170
209
171
@override
210
- InMemoryFilesystem copyWith ({FilesystemCache ? cache}) =>
211
- InMemoryFilesystem ._(assets: assets, cache: cache ?? this .cache);
172
+ Future <Uint8List > readAsBytes (String path) async => _files[path]! ;
212
173
213
174
@override
214
- Future <bool > exists (AssetId id) async =>
215
- cache.exists (id, ifAbsent: () async => assets.containsKey (id));
175
+ Uint8List readAsBytesSync (String path) => _files[path]! ;
216
176
217
177
@override
218
- Future <Uint8List > readAsBytes ( AssetId id ) async =>
219
- cache. readAsBytes (id, ifAbsent : () async => assets[id] as Uint8List );
178
+ Future <String > readAsString ( String path, { Encoding encoding = utf8} ) async =>
179
+ encoding. decode (_files[path] ! );
220
180
221
181
@override
222
- Future <String > readAsString (AssetId id, {Encoding encoding = utf8}) async {
223
- // The cache only directly supports utf8, for other encodings get the
224
- // bytes via the cache then convert.
225
- if (encoding == utf8) {
226
- return cache.readAsString (
227
- id,
228
- ifAbsent: () async => encoding.decode (assets[id]! ),
229
- );
230
- } else {
231
- return encoding.decode (await readAsBytes (id));
232
- }
233
- }
182
+ String readAsStringSync (String path, {Encoding encoding = utf8}) =>
183
+ encoding.decode (_files[path]! );
234
184
235
185
@override
236
- Future <void > delete (AssetId id) async {
237
- assets.remove (id);
238
- }
186
+ Future <void > delete (String path) async => _files.remove (path);
239
187
240
188
@override
241
- void deleteSync (AssetId id) async {
242
- assets.remove (id);
243
- }
189
+ void deleteSync (String path) async => _files.remove (path);
244
190
245
191
@override
246
- void writeAsBytesSync (AssetId id , List <int > contents) {
247
- assets[id ] = contents;
192
+ void writeAsBytesSync (String path , List <int > contents) {
193
+ _files[path ] = Uint8List . fromList ( contents) ;
248
194
}
249
195
250
196
@override
251
- Future <void > writeAsBytes (AssetId id , List <int > contents) async {
252
- assets[id ] = contents;
197
+ Future <void > writeAsBytes (String path , List <int > contents) async {
198
+ _files[path ] = Uint8List . fromList ( contents) ;
253
199
}
254
200
255
201
@override
256
202
void writeAsStringSync (
257
- AssetId id ,
203
+ String path ,
258
204
String contents, {
259
205
Encoding encoding = utf8,
260
206
}) {
261
- assets[id ] = encoding.encode (contents);
207
+ _files[path ] = Uint8List . fromList ( encoding.encode (contents) );
262
208
}
263
209
264
210
@override
265
211
Future <void > writeAsString (
266
- AssetId id ,
212
+ String path ,
267
213
String contents, {
268
214
Encoding encoding = utf8,
269
215
}) async {
270
- assets[id ] = encoding.encode (contents);
216
+ _files[path ] = Uint8List . fromList ( encoding.encode (contents) );
271
217
}
272
218
}
0 commit comments