Skip to content

Commit 680afb2

Browse files
committed
added hasInput funtion to Reader/BuildStep
1 parent fd76acc commit 680afb2

File tree

8 files changed

+54
-1
lines changed

8 files changed

+54
-1
lines changed

lib/src/asset/reader.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ import 'id.dart';
88

99
abstract class AssetReader {
1010
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8});
11+
12+
Future<bool> hasInput(AssetId id);
1113
}

lib/src/builder/build_step.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ abstract class BuildStep {
1313
/// The primary input for this build step.
1414
Asset get input;
1515

16+
/// Checks if an [Asset] by [id] exists as an input for this [BuildStep].
17+
///
18+
/// If [trackAsDependency] is true, then [id] will be marked as a dependency
19+
/// of all [expectedOutputs].
20+
Future<bool> hasInput(AssetId id, {bool trackAsDependency: true});
21+
1622
/// Reads an [Asset] by [id] as a [String] using [encoding].
1723
///
1824
/// If [trackAsDependency] is true, then [id] will be marked as a dependency

lib/src/builder/build_step_impl.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ class BuildStepImpl implements BuildStep {
4949
_dependencies.add(input.id);
5050
}
5151

52+
/// Checks if an [Asset] by [id] exists as an input for this [BuildStep].
53+
///
54+
/// If [trackAsDependency] is true, then [id] will be marked as a dependency
55+
/// of all [expectedOutputs].
56+
Future<bool> hasInput(AssetId id, {bool trackAsDependency: true}) {
57+
if (trackAsDependency) _dependencies.add(id);
58+
return _reader.hasInput(id);
59+
}
60+
5261
/// Reads an [Asset] by [id] as a [String] using [encoding].
5362
///
5463
/// If [trackAsDependency] is true, then [id] will be marked as a dependency

lib/src/generate/build.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ Stream<Asset> _runBuilder(Builder builder, List<AssetId> inputs) async* {
122122
class _SimpleAssetReader implements AssetReader {
123123
const _SimpleAssetReader();
124124

125+
@override
126+
Future<bool> hasInput(AssetId id) async {
127+
assert(id.package == _localPackageName);
128+
return new File(id.path).exists();
129+
}
130+
125131
@override
126132
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) async {
127133
assert(id.package == _localPackageName);

lib/src/transformer/transformer.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ class _TransformAssetReader implements AssetReader {
8989

9090
_TransformAssetReader(this.transform);
9191

92+
@override
93+
Future<bool> hasInput(build.AssetId id) =>
94+
transform.hasInput(_toBarbackAssetId(id));
95+
9296
@override
9397
Future<String> readAsString(build.AssetId id, {Encoding encoding: UTF8}) =>
9498
transform.readInputAsString(_toBarbackAssetId(id), encoding: encoding);

test/builder/build_step_impl_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ main() {
3535
var a2 = makeAssetId();
3636
await buildStep.readAsString(a2);
3737
expect(buildStep.dependencies, [primary.id, a1, a2]);
38+
39+
var a3 = makeAssetId();
40+
await buildStep.readAsString(a3, trackAsDependency: false);
41+
expect(buildStep.dependencies.contains(a3), isFalse);
42+
});
43+
44+
test('tracks dependencies on hasInput', () async {
45+
expect(buildStep.dependencies, [primary.id]);
46+
47+
var a1 = makeAssetId();
48+
await buildStep.hasInput(a1);
49+
expect(buildStep.dependencies, [primary.id, a1]);
50+
51+
var a2 = makeAssetId();
52+
await buildStep.hasInput(a2);
53+
expect(buildStep.dependencies, [primary.id, a1, a2]);
54+
55+
var a3 = makeAssetId();
56+
await buildStep.hasInput(a3, trackAsDependency: false);
57+
expect(buildStep.dependencies.contains(a3), isFalse);
3858
});
3959

4060
test('addDependency adds dependencies', () {

test/common/in_memory_reader.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ class InMemoryAssetReader implements AssetReader {
1111

1212
InMemoryAssetReader(this.assets);
1313

14+
Future<bool> hasInput(AssetId id) {
15+
return new Future.value(assets.containsKey(id));
16+
}
17+
1418
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) async {
15-
if (!assets.containsKey(id)) throw new AssetNotFoundException(id);
19+
if (!await hasInput(id)) throw new AssetNotFoundException(id);
1620
return assets[id];
1721
}
1822
}

test/common/stub_reader.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'dart:convert';
77
import 'package:build/build.dart';
88

99
class StubAssetReader implements AssetReader {
10+
Future<bool> hasInput(AssetId id) => new Future.value(null);
11+
1012
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) =>
1113
new Future.value(null);
1214
}

0 commit comments

Comments
 (0)