Skip to content

Commit c3573f6

Browse files
committed
Move BuildStep to an interface and add BuildStepImpl with the gory details
1 parent 1dd1014 commit c3573f6

File tree

4 files changed

+91
-55
lines changed

4 files changed

+91
-55
lines changed

lib/src/builder/build_step.dart

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,30 @@
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
import 'dart:async';
5-
import 'dart:collection';
65
import 'dart:convert';
76

87
import '../asset/asset.dart';
98
import '../asset/id.dart';
10-
import '../asset/reader.dart';
11-
import '../asset/writer.dart';
129

1310
/// A single step in the build processes. This represents a single input and
14-
/// its expected and real outputs. It also handles tracking of dependencies.
11+
/// it also handles tracking of dependencies.
1512
class BuildStep {
1613
/// The primary input for this build step.
1714
final Asset input;
1815

19-
/// The list of all outputs which are expected/allowed to be output from this
20-
/// step.
21-
final List<AssetId> expectedOutputs;
22-
23-
/// The actual outputs of this build step.
24-
UnmodifiableListView<Asset> get outputs => new UnmodifiableListView(_outputs);
25-
final List<Asset> _outputs = [];
26-
27-
/// A future that completes once all outputs current are done writing.
28-
///
29-
/// TODO(jakemac): This is not typically needed inside of a Builder, we could
30-
/// consider moving it out to a separate class which wraps a [BuildStep] and
31-
/// this future? Another option would be an `Output` class which wraps an
32-
/// [Asset] and a [Future] that completes once its done writing.
33-
Future get outputsCompleted => _outputsCompleted;
34-
Future _outputsCompleted = new Future(() {});
35-
36-
/// The dependencies read in during this build step.
37-
UnmodifiableListView<AssetId> get dependencies =>
38-
new UnmodifiableListView(_dependencies);
39-
final Set<AssetId> _dependencies = new Set<AssetId>();
40-
41-
/// Used internally for reading files.
42-
final AssetReader _reader;
43-
44-
/// Used internally for writing files.
45-
final AssetWriter _writer;
46-
47-
BuildStep(
48-
this.input, List<AssetId> expectedOutputs, this._reader, this._writer)
49-
: expectedOutputs = new List.unmodifiable(expectedOutputs) {
50-
/// The [input] is always a dependency.
51-
_dependencies.add(input.id);
52-
}
53-
5416
/// Reads an [Asset] by [id] as a [String] using [encoding].
5517
///
5618
/// If [trackAsDependency] is true, then [id] will be marked as a dependency
5719
/// of all [expectedOutputs].
5820
Future<String> readAsString(AssetId id,
59-
{Encoding encoding: UTF8, bool trackAsDependency: true}) {
60-
if (trackAsDependency) _dependencies.add(id);
61-
return _reader.readAsString(id, encoding: encoding);
62-
}
21+
{Encoding encoding: UTF8, bool trackAsDependency: true});
6322

6423
/// Outputs an [Asset] using the current [AssetWriter], and adds [asset] to
6524
/// [outputs].
66-
void writeAsString(Asset asset, {Encoding encoding: UTF8}) {
67-
_outputs.add(asset);
68-
var done = _writer.writeAsString(asset, encoding: encoding);
69-
_outputsCompleted = _outputsCompleted.then((_) => done);
70-
}
25+
void writeAsString(Asset asset, {Encoding encoding: UTF8});
7126

7227
/// Explicitly adds [id] as a dependency of all [expectedOutputs]. This is
7328
/// not generally necessary unless forcing `trackAsDependency: false` when
7429
/// calling [readAsString].
75-
void addDependency(AssetId id) {
76-
_dependencies.add(id);
77-
}
30+
void addDependency(AssetId id);
7831
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
import 'dart:async';
5+
import 'dart:collection';
6+
import 'dart:convert';
7+
8+
import '../asset/asset.dart';
9+
import '../asset/id.dart';
10+
import '../asset/reader.dart';
11+
import '../asset/writer.dart';
12+
import 'build_step.dart';
13+
14+
/// A single step in the build processes. This represents a single input and
15+
/// its expected and real outputs. It also handles tracking of dependencies.
16+
class BuildStepImpl implements BuildStep {
17+
/// The primary input for this build step.
18+
@override
19+
final Asset input;
20+
21+
/// The list of all outputs which are expected/allowed to be output from this
22+
/// step.
23+
final List<AssetId> expectedOutputs;
24+
25+
/// The actual outputs of this build step.
26+
UnmodifiableListView<Asset> get outputs => new UnmodifiableListView(_outputs);
27+
final List<Asset> _outputs = [];
28+
29+
/// A future that completes once all outputs current are done writing.
30+
///
31+
/// TODO(jakemac): This is not typically needed inside of a Builder, we could
32+
/// consider moving it out to a separate class which wraps a [BuildStep] and
33+
/// this future? Another option would be an `Output` class which wraps an
34+
/// [Asset] and a [Future] that completes once its done writing.
35+
Future get outputsCompleted => _outputsCompleted;
36+
Future _outputsCompleted = new Future(() {});
37+
38+
/// The dependencies read in during this build step.
39+
UnmodifiableListView<AssetId> get dependencies =>
40+
new UnmodifiableListView(_dependencies);
41+
final Set<AssetId> _dependencies = new Set<AssetId>();
42+
43+
/// Used internally for reading files.
44+
final AssetReader _reader;
45+
46+
/// Used internally for writing files.
47+
final AssetWriter _writer;
48+
49+
BuildStepImpl(
50+
this.input, List<AssetId> expectedOutputs, this._reader, this._writer)
51+
: expectedOutputs = new List.unmodifiable(expectedOutputs) {
52+
/// The [input] is always a dependency.
53+
_dependencies.add(input.id);
54+
}
55+
56+
/// Reads an [Asset] by [id] as a [String] using [encoding].
57+
///
58+
/// If [trackAsDependency] is true, then [id] will be marked as a dependency
59+
/// of all [expectedOutputs].
60+
@override
61+
Future<String> readAsString(AssetId id,
62+
{Encoding encoding: UTF8, bool trackAsDependency: true}) {
63+
if (trackAsDependency) _dependencies.add(id);
64+
return _reader.readAsString(id, encoding: encoding);
65+
}
66+
67+
/// Outputs an [Asset] using the current [AssetWriter], and adds [asset] to
68+
/// [outputs].
69+
@override
70+
void writeAsString(Asset asset, {Encoding encoding: UTF8}) {
71+
_outputs.add(asset);
72+
var done = _writer.writeAsString(asset, encoding: encoding);
73+
_outputsCompleted = _outputsCompleted.then((_) => done);
74+
}
75+
76+
/// Explicitly adds [id] as a dependency of all [expectedOutputs]. This is
77+
/// not generally necessary unless forcing `trackAsDependency: false` when
78+
/// calling [readAsString].
79+
@override
80+
void addDependency(AssetId id) {
81+
_dependencies.add(id);
82+
}
83+
}

lib/src/generate/build.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import '../asset/id.dart';
1414
import '../asset/reader.dart';
1515
import '../asset/writer.dart';
1616
import '../builder/builder.dart';
17-
import '../builder/build_step.dart';
17+
import '../builder/build_step_impl.dart';
1818
import 'build_result.dart';
1919
import 'input_set.dart';
2020
import 'phase.dart';
@@ -109,7 +109,7 @@ Stream<Asset> _runBuilder(Builder builder, List<AssetId> inputs) async* {
109109
var expectedOutputs = builder.declareOutputs(input);
110110
var inputAsset = new Asset(input, await _reader.readAsString(input));
111111
var buildStep =
112-
new BuildStep(inputAsset, expectedOutputs, _reader, _writer);
112+
new BuildStepImpl(inputAsset, expectedOutputs, _reader, _writer);
113113
await builder.build(buildStep);
114114
await buildStep.outputsCompleted;
115115
for (var output in buildStep.outputs) {

lib/src/transformer/transformer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import '../asset/id.dart' as build;
1212
import '../asset/reader.dart';
1313
import '../asset/writer.dart';
1414
import '../builder/builder.dart';
15-
import '../builder/build_step.dart';
15+
import '../builder/build_step_impl.dart';
1616

1717
abstract class BuilderTransformer implements Transformer, DeclaringTransformer {
1818
List<Builder> get builders;
@@ -35,7 +35,7 @@ abstract class BuilderTransformer implements Transformer, DeclaringTransformer {
3535
var expected = _expectedOutputs(transform.primaryInput.id, [builder]);
3636
if (expected.isEmpty) continue;
3737

38-
var buildStep = new BuildStep(input, expected, reader, writer);
38+
var buildStep = new BuildStepImpl(input, expected, reader, writer);
3939
futures.add(builder.build(buildStep));
4040
}
4141

0 commit comments

Comments
 (0)