Skip to content

Commit 13f0132

Browse files
committed
File based.
1 parent b9f445c commit 13f0132

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

build_resolvers/test/resolver_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:convert';
65
import 'dart:io' show Platform;
76
import 'dart:isolate';
87

build_runner_core/lib/src/asset/file_based.dart

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,54 @@ import 'writer.dart';
1818
/// Basic [AssetReader] which uses a [PackageGraph] to look up where to read
1919
/// files from disk.
2020
class FileBasedAssetReader extends AssetReader implements AssetReaderState {
21+
@override
22+
final AssetPathProvider assetPathProvider;
2123
@override
2224
final Filesystem filesystem;
2325
@override
2426
late final AssetFinder assetFinder = FunctionAssetFinder(_findAssets);
2527

28+
final FilesystemCache cache;
2629
final PackageGraph packageGraph;
2730

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

3139
@override
3240
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);
4042

4143
@override
4244
InputTracker? get inputTracker => null;
4345

4446
@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+
}
4651

4752
@override
4853
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?
5057
throw AssetNotFoundException(id);
5158
}
52-
return filesystem.readAsBytes(id);
59+
return filesystem.readAsBytes(path);
5360
}
5461

5562
@override
5663
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)) {
5866
throw AssetNotFoundException(id);
5967
}
60-
return filesystem.readAsString(id, encoding: encoding);
68+
return filesystem.readAsString(path, encoding: encoding);
6169
}
6270

6371
// This is only for generators, so only `BuildStep` needs to implement it.
@@ -97,19 +105,23 @@ class FileBasedAssetWriter implements RunnerAssetWriter {
97105
final PackageGraph packageGraph;
98106
final Filesystem filesystem;
99107

100-
FileBasedAssetWriter(this.packageGraph)
101-
: filesystem = IoFilesystem(assetPathProvider: packageGraph);
108+
FileBasedAssetWriter(this.packageGraph) : filesystem = IoFilesystem();
102109

103110
@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+
}
106115

107116
@override
108-
Future writeAsString(
117+
Future<void> writeAsString(
109118
AssetId id,
110119
String contents, {
111120
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+
}
113125

114126
@override
115127
Future delete(AssetId id) async {
@@ -119,7 +131,8 @@ class FileBasedAssetWriter implements RunnerAssetWriter {
119131
'Should not delete assets outside of ${packageGraph.root.name}',
120132
);
121133
}
122-
await filesystem.delete(id);
134+
final path = packageGraph.pathFor(id);
135+
await filesystem.delete(path);
123136
}
124137

125138
@override

0 commit comments

Comments
 (0)