diff --git a/lib/src/asset/reader.dart b/lib/src/asset/reader.dart index 011acfe7c..7a827e317 100644 --- a/lib/src/asset/reader.dart +++ b/lib/src/asset/reader.dart @@ -8,4 +8,6 @@ import 'id.dart'; abstract class AssetReader { Future readAsString(AssetId id, {Encoding encoding: UTF8}); + + Future hasInput(AssetId id); } diff --git a/lib/src/builder/build_step.dart b/lib/src/builder/build_step.dart index ae84acdd6..9dabdae5c 100644 --- a/lib/src/builder/build_step.dart +++ b/lib/src/builder/build_step.dart @@ -13,6 +13,12 @@ abstract class BuildStep { /// The primary input for this build step. Asset get input; + /// Checks if an [Asset] by [id] exists as an input for this [BuildStep]. + /// + /// If [trackAsDependency] is true, then [id] will be marked as a dependency + /// of all [expectedOutputs]. + Future hasInput(AssetId id, {bool trackAsDependency: true}); + /// Reads an [Asset] by [id] as a [String] using [encoding]. /// /// If [trackAsDependency] is true, then [id] will be marked as a dependency diff --git a/lib/src/builder/build_step_impl.dart b/lib/src/builder/build_step_impl.dart index 6ccbcd21d..18f686d6c 100644 --- a/lib/src/builder/build_step_impl.dart +++ b/lib/src/builder/build_step_impl.dart @@ -49,6 +49,15 @@ class BuildStepImpl implements BuildStep { _dependencies.add(input.id); } + /// Checks if an [Asset] by [id] exists as an input for this [BuildStep]. + /// + /// If [trackAsDependency] is true, then [id] will be marked as a dependency + /// of all [expectedOutputs]. + Future hasInput(AssetId id, {bool trackAsDependency: true}) { + if (trackAsDependency) _dependencies.add(id); + return _reader.hasInput(id); + } + /// Reads an [Asset] by [id] as a [String] using [encoding]. /// /// If [trackAsDependency] is true, then [id] will be marked as a dependency diff --git a/lib/src/generate/build.dart b/lib/src/generate/build.dart index 60b1d8900..ebe78e916 100644 --- a/lib/src/generate/build.dart +++ b/lib/src/generate/build.dart @@ -122,6 +122,12 @@ Stream _runBuilder(Builder builder, List inputs) async* { class _SimpleAssetReader implements AssetReader { const _SimpleAssetReader(); + @override + Future hasInput(AssetId id) async { + assert(id.package == _localPackageName); + return new File(id.path).exists(); + } + @override Future readAsString(AssetId id, {Encoding encoding: UTF8}) async { assert(id.package == _localPackageName); diff --git a/lib/src/transformer/transformer.dart b/lib/src/transformer/transformer.dart index fe17855ff..abc22d14b 100644 --- a/lib/src/transformer/transformer.dart +++ b/lib/src/transformer/transformer.dart @@ -89,6 +89,10 @@ class _TransformAssetReader implements AssetReader { _TransformAssetReader(this.transform); + @override + Future hasInput(build.AssetId id) => + transform.hasInput(_toBarbackAssetId(id)); + @override Future readAsString(build.AssetId id, {Encoding encoding: UTF8}) => transform.readInputAsString(_toBarbackAssetId(id), encoding: encoding); diff --git a/test/builder/build_step_impl_test.dart b/test/builder/build_step_impl_test.dart index 0cff6eaf5..9d0ae92f5 100644 --- a/test/builder/build_step_impl_test.dart +++ b/test/builder/build_step_impl_test.dart @@ -35,6 +35,26 @@ main() { var a2 = makeAssetId(); await buildStep.readAsString(a2); expect(buildStep.dependencies, [primary.id, a1, a2]); + + var a3 = makeAssetId(); + await buildStep.readAsString(a3, trackAsDependency: false); + expect(buildStep.dependencies.contains(a3), isFalse); + }); + + test('tracks dependencies on hasInput', () async { + expect(buildStep.dependencies, [primary.id]); + + var a1 = makeAssetId(); + await buildStep.hasInput(a1); + expect(buildStep.dependencies, [primary.id, a1]); + + var a2 = makeAssetId(); + await buildStep.hasInput(a2); + expect(buildStep.dependencies, [primary.id, a1, a2]); + + var a3 = makeAssetId(); + await buildStep.hasInput(a3, trackAsDependency: false); + expect(buildStep.dependencies.contains(a3), isFalse); }); test('addDependency adds dependencies', () { diff --git a/test/common/in_memory_reader.dart b/test/common/in_memory_reader.dart index df6d6a919..b2c23a2b6 100644 --- a/test/common/in_memory_reader.dart +++ b/test/common/in_memory_reader.dart @@ -11,8 +11,12 @@ class InMemoryAssetReader implements AssetReader { InMemoryAssetReader(this.assets); + Future hasInput(AssetId id) { + return new Future.value(assets.containsKey(id)); + } + Future readAsString(AssetId id, {Encoding encoding: UTF8}) async { - if (!assets.containsKey(id)) throw new AssetNotFoundException(id); + if (!await hasInput(id)) throw new AssetNotFoundException(id); return assets[id]; } } diff --git a/test/common/stub_reader.dart b/test/common/stub_reader.dart index 660ac8990..ea208e120 100644 --- a/test/common/stub_reader.dart +++ b/test/common/stub_reader.dart @@ -7,6 +7,8 @@ import 'dart:convert'; import 'package:build/build.dart'; class StubAssetReader implements AssetReader { + Future hasInput(AssetId id) => new Future.value(null); + Future readAsString(AssetId id, {Encoding encoding: UTF8}) => new Future.value(null); }