@@ -18,46 +18,54 @@ import 'writer.dart';
18
18
/// Basic [AssetReader] which uses a [PackageGraph] to look up where to read
19
19
/// files from disk.
20
20
class FileBasedAssetReader extends AssetReader implements AssetReaderState {
21
+ @override
22
+ final AssetPathProvider assetPathProvider;
21
23
@override
22
24
final Filesystem filesystem;
23
25
@override
24
26
late final AssetFinder assetFinder = FunctionAssetFinder (_findAssets);
25
27
28
+ final FilesystemCache cache;
26
29
final PackageGraph packageGraph;
27
30
28
- FileBasedAssetReader (this .packageGraph, {Filesystem ? filesystem})
29
- : filesystem = filesystem ?? IoFilesystem (assetPathProvider: packageGraph);
31
+ FileBasedAssetReader (
32
+ this .packageGraph, {
33
+ Filesystem ? filesystem,
34
+ FilesystemCache ? cache,
35
+ }) : filesystem = filesystem ?? IoFilesystem (),
36
+ cache = cache ?? const PassthroughFilesystemCache (),
37
+ assetPathProvider = packageGraph;
30
38
31
39
@override
32
40
FileBasedAssetReader copyWith ({FilesystemCache ? cache}) =>
33
- FileBasedAssetReader (
34
- packageGraph,
35
- filesystem: filesystem.copyWith (cache: cache),
36
- );
37
-
38
- @override
39
- AssetPathProvider ? get assetPathProvider => packageGraph;
41
+ FileBasedAssetReader (packageGraph, filesystem: filesystem, cache: cache);
40
42
41
43
@override
42
44
InputTracker ? get inputTracker => null ;
43
45
44
46
@override
45
- Future <bool > canRead (AssetId id) => filesystem.exists (id);
47
+ Future <bool > canRead (AssetId id) {
48
+ final path = assetPathProvider.pathFor (id);
49
+ return filesystem.exists (path);
50
+ }
46
51
47
52
@override
48
53
Future <List <int >> readAsBytes (AssetId id) async {
49
- if (! await filesystem.exists (id)) {
54
+ final path = assetPathProvider.pathFor (id);
55
+ if (! await filesystem.exists (path)) {
56
+ // TODO(davidmorgan): report the path as well?
50
57
throw AssetNotFoundException (id);
51
58
}
52
- return filesystem.readAsBytes (id );
59
+ return filesystem.readAsBytes (path );
53
60
}
54
61
55
62
@override
56
63
Future <String > readAsString (AssetId id, {Encoding encoding = utf8}) async {
57
- if (! await filesystem.exists (id)) {
64
+ final path = assetPathProvider.pathFor (id);
65
+ if (! await filesystem.exists (path)) {
58
66
throw AssetNotFoundException (id);
59
67
}
60
- return filesystem.readAsString (id , encoding: encoding);
68
+ return filesystem.readAsString (path , encoding: encoding);
61
69
}
62
70
63
71
// This is only for generators, so only `BuildStep` needs to implement it.
@@ -97,19 +105,23 @@ class FileBasedAssetWriter implements RunnerAssetWriter {
97
105
final PackageGraph packageGraph;
98
106
final Filesystem filesystem;
99
107
100
- FileBasedAssetWriter (this .packageGraph)
101
- : filesystem = IoFilesystem (assetPathProvider: packageGraph);
108
+ FileBasedAssetWriter (this .packageGraph) : filesystem = IoFilesystem ();
102
109
103
110
@override
104
- Future writeAsBytes (AssetId id, List <int > bytes) =>
105
- filesystem.writeAsBytes (id, bytes);
111
+ Future <void > writeAsBytes (AssetId id, List <int > bytes) async {
112
+ final path = packageGraph.pathFor (id);
113
+ await filesystem.writeAsBytes (path, bytes);
114
+ }
106
115
107
116
@override
108
- Future writeAsString (
117
+ Future < void > writeAsString (
109
118
AssetId id,
110
119
String contents, {
111
120
Encoding encoding = utf8,
112
- }) => filesystem.writeAsString (id, contents, encoding: encoding);
121
+ }) async {
122
+ final path = packageGraph.pathFor (id);
123
+ await filesystem.writeAsString (path, contents, encoding: encoding);
124
+ }
113
125
114
126
@override
115
127
Future delete (AssetId id) async {
@@ -119,7 +131,8 @@ class FileBasedAssetWriter implements RunnerAssetWriter {
119
131
'Should not delete assets outside of ${packageGraph .root .name }' ,
120
132
);
121
133
}
122
- await filesystem.delete (id);
134
+ final path = packageGraph.pathFor (id);
135
+ await filesystem.delete (path);
123
136
}
124
137
125
138
@override
0 commit comments