Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added hasInput funtion to Reader/BuildStep #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/asset/reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ import 'id.dart';

abstract class AssetReader {
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8});

Future<bool> hasInput(AssetId id);
}
6 changes: 6 additions & 0 deletions lib/src/builder/build_step.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> hasInput(AssetId id, {bool trackAsDependency: true});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is trackAsDependency something we want to expose to users, or should it be an implementation detail?

I feel like you had a use case for it, but I can't remember what it is right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it mean if the user passes false for this? Presumable, the caller uses the boolean result of this method to do something that affects their build output. If that boolean were to change without the build step being re-run (because it passed trackAsDependency: false), won't you end up with stale or inconsistent output?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some offline discussions I am going to remove this as part of the public api, at least for now, as well as removing the addDependency function. These combined would allow you to write a builder that cheats a bit in terms of marking its dependencies, which specifically when doing resolution would be useful. We may consider adding it back in the future, but for now it doesn't need to be there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do that in a followup cl though


/// Reads an [Asset] by [id] as a [String] using [encoding].
///
/// If [trackAsDependency] is true, then [id] will be marked as a dependency
Expand Down
9 changes: 9 additions & 0 deletions lib/src/builder/build_step_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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
Expand Down
6 changes: 6 additions & 0 deletions lib/src/generate/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ Stream<Asset> _runBuilder(Builder builder, List<AssetId> inputs) async* {
class _SimpleAssetReader implements AssetReader {
const _SimpleAssetReader();

@override
Future<bool> hasInput(AssetId id) async {
assert(id.package == _localPackageName);
return new File(id.path).exists();
}

@override
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) async {
assert(id.package == _localPackageName);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/transformer/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class _TransformAssetReader implements AssetReader {

_TransformAssetReader(this.transform);

@override
Future<bool> hasInput(build.AssetId id) =>
transform.hasInput(_toBarbackAssetId(id));

@override
Future<String> readAsString(build.AssetId id, {Encoding encoding: UTF8}) =>
transform.readInputAsString(_toBarbackAssetId(id), encoding: encoding);
Expand Down
20 changes: 20 additions & 0 deletions test/builder/build_step_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down
6 changes: 5 additions & 1 deletion test/common/in_memory_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class InMemoryAssetReader implements AssetReader {

InMemoryAssetReader(this.assets);

Future<bool> hasInput(AssetId id) {
return new Future.value(assets.containsKey(id));
}

Future<String> 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];
}
}
2 changes: 2 additions & 0 deletions test/common/stub_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'dart:convert';
import 'package:build/build.dart';

class StubAssetReader implements AssetReader {
Future<bool> hasInput(AssetId id) => new Future.value(null);

Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) =>
new Future.value(null);
}