Skip to content

Commit bfc6df1

Browse files
authored
Upgrade to build_runner 0.7 (#90)
- Update dependencies - Add a `build.yaml` in `json_serializable` to expose a builder that is applied automatically to dependents of this package. - Add a `builder.dart` to expose a `BuilderFactory`. - Add a `build.yaml` in `example` to configure the header and keep the same behavior as the manual build script. - Update the README to use the new approach. - Keep a manual build script for `json_serializable` since it does strange stuff. Update to the `applyToRoot` approach and collapse to a single file using `run` and passing through args.
1 parent 89e9642 commit bfc6df1

17 files changed

+194
-267
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ language: dart
33

44
dart:
55
- dev
6-
- stable
76

87
env:
98
- PKG=example TASK=dartanalyzer

example/README.md

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,38 @@ dependencies:
66
json_annotation: ^0.2.2
77

88
dev_dependencies:
9-
build_runner: ^0.6.1
10-
json_serializable: ^0.3.0
9+
build_runner: ^0.7.0
10+
json_serializable: ^0.3.1
1111
```
1212
13-
Next, create the scripts to run code generation. The [`tool`][tool] directory
14-
contains the conventional layout of these scripts.
15-
16-
* [`build_actions.dart`][tool]: A convention when using `package:build` to
17-
have one location to define the actions to be run by [`build.dart`][build] and
18-
[`watch.dart`][watch]. See the comments in the source file for more
19-
information.
20-
21-
* [`build.dart`][build]: Runs one build using the actions defined in
22-
[`build_actions.dart`][build_actions].
23-
24-
* [`watch.dart`][watch]: Starts a watcher that (re)runs the actions defined in
25-
[`build_actions.dart`][build_actions] when files change.
26-
27-
Finally, annotate your code with classes defined in
13+
Annotate your code with classes defined in
2814
[package:json_annotation][json_annotation].
2915
3016
* See [`lib/example.dart`][example] for an example of a file using these
3117
annotations.
3218

3319
* See [`lib/example.g.dart`][example_g] for the generated file.
3420

35-
Run the build script to update the generated code after you modify of of the
36-
source files.
21+
If you would like to use a custom header, or enable the wrapper option, add a
22+
[`build.yaml`][build_config] file and a default target.
23+
24+
Run `pub run build_runner build` to generate files into your source directory.
3725

3826
```console
39-
> dart bin/build.dart
27+
> pub run build_runner build
28+
[INFO] ensureBuildScript: Generating build script completed, took 368ms
4029
[INFO] BuildDefinition: Reading cached asset graph completed, took 54ms
4130
[INFO] BuildDefinition: Checking for updates since last build completed, took 663ms
4231
[INFO] Build: Running build completed, took 10ms
4332
[INFO] Build: Caching finalized dependency graph completed, took 44ms
44-
[INFO] Build: Succeeded after 68ms with 0 outputs
33+
[INFO] Build: Succeeded after 4687ms with 1 outputs
4534
```
4635

47-
*NOTE*: If you're using Flutter, running a Dart script is a bit tricky. You can
48-
find the Dart entry point at `$FLUTTER_ROOT/bin/cache/dart-sdk/bin/dart`.
49-
You can determine `FLUTTER_ROOT` by looking at the first few lines of
50-
`flutter doctor`.
51-
52-
```console
53-
> flutter doctor
54-
[✓] Flutter (on Mac OS X 10.12.6 16G1114, locale en-US, channel master)
55-
• Flutter at YOUR_FLUTTER_ROOT_HERE
56-
• Framework revision 235b64ed2f (13 hours ago), 2017-12-14 20:38:39 -0800
57-
...
58-
```
36+
*NOTE*: If you're using Flutter, running a script from a Dart package is a bit
37+
tricky. Replace `pub run` with `flutter packages pub run`.
5938

60-
[tool]: tool
61-
[build_actions]: tool/build_actions.dart
62-
[build]: tool/build.dart
63-
[watch]: tool/watch.dart
6439
[example]: lib/example.dart
6540
[example_g]: lib/example.g.dart
41+
[build_config]: build.yaml
6642
[json_annotation]: https://pub.dartlang.org/packages/json_annotation
6743
[json_serializable]: https://pub.dartlang.org/packages/json_serializable

example/build.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Read about `build.yaml` at https://pub.dartlang.org/packages/build_config
2+
targets:
3+
example:
4+
builders:
5+
json_serializable:
6+
options:
7+
header: |+
8+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
9+
// for details. All rights reserved. Use of this source code is governed by a
10+
// BSD-style license that can be found in the LICENSE file.
11+
12+
// GENERATED CODE - DO NOT MODIFY BY HAND
13+

example/pubspec.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ dependencies:
88
json_annotation: ^0.2.2
99

1010
dev_dependencies:
11-
build_runner: ^0.6.1
12-
json_serializable: ^0.3.0
11+
build_runner: ^0.7.0
12+
json_serializable: ^0.3.1
1313

1414
# Used by tests. Not required to use `json_serializable`.
1515
path: ^1.5.1
1616
test: ^0.12.29
17+
18+
dependency_overrides:
19+
json_serializable:
20+
path: ../json_serializable

example/test/ensure_build_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void main() {
3030
expect(_changedGeneratedFiles(), isEmpty);
3131

3232
// 2 - run build - should be no output, since nothing should change
33-
var result = _runProc('dart', ['--checked', 'tool/build.dart']);
33+
var result = _runProc('pub',
34+
['run', 'build_runner', 'build', '--delete-conflicting-outputs']);
3435
expect(result,
3536
contains(new RegExp(r'Build: Succeeded after \S+ with \d+ outputs')));
3637

example/test/readme_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ dependencies:
2323
json_annotation: ^0.2.2
2424
2525
dev_dependencies:
26-
build_runner: ^0.6.1
27-
json_serializable: ^0.3.0
26+
build_runner: ^0.7.0
27+
json_serializable: ^0.3.1
2828
''';

example/tool/build.dart

Lines changed: 0 additions & 17 deletions
This file was deleted.

example/tool/build_actions.dart

Lines changed: 0 additions & 36 deletions
This file was deleted.

example/tool/watch.dart

Lines changed: 0 additions & 12 deletions
This file was deleted.

json_serializable/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.3.1-dev
2+
3+
* Add a `build.yaml` so the builder can be consumed by users of `build_runner`
4+
version 0.7.0.
5+
16
## 0.3.0
27

38
* **NEW** top-level library `json_serializable.dart`.

json_serializable/build.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Read about `build.yaml` at https://pub.dartlang.org/packages/build_config
2+
builders:
3+
json_serializable:
4+
target: ":json_serializable"
5+
import: "package:json_serializable/builder.dart"
6+
builder_factories: ["jsonSerializable"]
7+
build_extensions: {".dart": [".g.dart"]}
8+
auto_apply: dependents
9+
build_to: source

json_serializable/lib/builder.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2017, 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+
5+
import 'package:build/build.dart';
6+
7+
import 'json_serializable.dart';
8+
9+
Builder jsonSerializable(BuilderOptions options) => jsonPartBuilder(
10+
header: options.config['header'] as String,
11+
useWrappers: options.config['use_wrappers'] as bool ?? false);

json_serializable/pubspec.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name: json_serializable
2-
version: 0.3.0
2+
version: 0.3.1-dev
33
author: Dart Team <misc@dartlang.org>
44
description: Generates utilities to aid in serializing to/from JSON.
55
homepage: https://github.com/dart-lang/json_serializable
66
environment:
7-
sdk: '>=1.24.0 <2.0.0'
7+
sdk: '>=2.0.0-dev <2.0.0'
88
dependencies:
99
analyzer: '>=0.29.10 <0.31.0'
1010
build: '>=0.9.0 <0.12.0'
11+
build_config: ^0.2.1
1112
cli_util: ^0.1.0
1213

1314
# Use a tight version constraint to ensure that a constraint on
@@ -16,7 +17,7 @@ dependencies:
1617
path: ^1.3.2
1718
source_gen: ^0.7.1
1819
dev_dependencies:
19-
build_runner: ^0.6.0+1
20+
build_runner: ^0.7.0
2021
build_test: ^0.9.0
2122
collection: ^1.14.0
2223
dart_style: ^1.0.0

json_serializable/test/ensure_build_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void main() {
3030
expect(_changedGeneratedFiles(), isEmpty);
3131

3232
// 2 - run build - should be no output, since nothing should change
33-
var result = _runProc('dart', ['--checked', 'tool/build.dart']);
33+
var result = _runProc('pub',
34+
['run', 'build_runner', 'build', '--delete-conflicting-outputs']);
3435
expect(result,
3536
contains(new RegExp(r'Build: Succeeded after \S+ with \d+ outputs')));
3637

0 commit comments

Comments
 (0)