Skip to content

Upgrade to build_runner 0.7 #90

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

Merged
merged 9 commits into from
Jan 2, 2018
Merged
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ language: dart

dart:
- dev
- stable

env:
- PKG=example TASK=dartanalyzer
Expand Down
50 changes: 13 additions & 37 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,38 @@ dependencies:
json_annotation: ^0.2.2

dev_dependencies:
build_runner: ^0.6.1
json_serializable: ^0.3.0
build_runner: ^0.7.0
json_serializable: ^0.3.1
```

Next, create the scripts to run code generation. The [`tool`][tool] directory
contains the conventional layout of these scripts.

* [`build_actions.dart`][tool]: A convention when using `package:build` to
have one location to define the actions to be run by [`build.dart`][build] and
[`watch.dart`][watch]. See the comments in the source file for more
information.

* [`build.dart`][build]: Runs one build using the actions defined in
[`build_actions.dart`][build_actions].

* [`watch.dart`][watch]: Starts a watcher that (re)runs the actions defined in
[`build_actions.dart`][build_actions] when files change.

Finally, annotate your code with classes defined in
Annotate your code with classes defined in
[package:json_annotation][json_annotation].

* See [`lib/example.dart`][example] for an example of a file using these
annotations.

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

Run the build script to update the generated code after you modify of of the
source files.
If you would like to use a custom header, or enable the wrapper option, add a
[`build.yaml`][build_config] file and a default target.

Run `pub run build_runner build` to generate files into your source directory.

```console
> dart bin/build.dart
> pub run build_runner build
[INFO] ensureBuildScript: Generating build script completed, took 368ms
[INFO] BuildDefinition: Reading cached asset graph completed, took 54ms
[INFO] BuildDefinition: Checking for updates since last build completed, took 663ms
[INFO] Build: Running build completed, took 10ms
[INFO] Build: Caching finalized dependency graph completed, took 44ms
[INFO] Build: Succeeded after 68ms with 0 outputs
[INFO] Build: Succeeded after 4687ms with 1 outputs
```

*NOTE*: If you're using Flutter, running a Dart script is a bit tricky. You can
find the Dart entry point at `$FLUTTER_ROOT/bin/cache/dart-sdk/bin/dart`.
You can determine `FLUTTER_ROOT` by looking at the first few lines of
`flutter doctor`.

```console
> flutter doctor
[✓] Flutter (on Mac OS X 10.12.6 16G1114, locale en-US, channel master)
• Flutter at YOUR_FLUTTER_ROOT_HERE
• Framework revision 235b64ed2f (13 hours ago), 2017-12-14 20:38:39 -0800
...
```
*NOTE*: If you're using Flutter, running a script from a Dart package is a bit
tricky. Replace `pub run` with `flutter packages pub run`.

[tool]: tool
[build_actions]: tool/build_actions.dart
[build]: tool/build.dart
[watch]: tool/watch.dart
[example]: lib/example.dart
[example_g]: lib/example.g.dart
[build_config]: build.yaml
[json_annotation]: https://pub.dartlang.org/packages/json_annotation
[json_serializable]: https://pub.dartlang.org/packages/json_serializable
13 changes: 13 additions & 0 deletions example/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about `build.yaml` at https://pub.dartlang.org/packages/build_config
targets:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto – add a # FYI ready about build.yaml here... line at the top...

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

example:
builders:
json_serializable:
options:
header: |+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// GENERATED CODE - DO NOT MODIFY BY HAND

8 changes: 6 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ dependencies:
json_annotation: ^0.2.2

dev_dependencies:
build_runner: ^0.6.1
json_serializable: ^0.3.0
build_runner: ^0.7.0
json_serializable: ^0.3.1

# Used by tests. Not required to use `json_serializable`.
path: ^1.5.1
test: ^0.12.29

dependency_overrides:
json_serializable:
path: ../json_serializable
3 changes: 2 additions & 1 deletion example/test/ensure_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void main() {
expect(_changedGeneratedFiles(), isEmpty);

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

Expand Down
4 changes: 2 additions & 2 deletions example/test/readme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ dependencies:
json_annotation: ^0.2.2

dev_dependencies:
build_runner: ^0.6.1
json_serializable: ^0.3.0
build_runner: ^0.7.0
json_serializable: ^0.3.1
''';
17 changes: 0 additions & 17 deletions example/tool/build.dart

This file was deleted.

36 changes: 0 additions & 36 deletions example/tool/build_actions.dart

This file was deleted.

12 changes: 0 additions & 12 deletions example/tool/watch.dart

This file was deleted.

5 changes: 5 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.1-dev

* Add a `build.yaml` so the builder can be consumed by users of `build_runner`
version 0.7.0.

## 0.3.0

* **NEW** top-level library `json_serializable.dart`.
Expand Down
9 changes: 9 additions & 0 deletions json_serializable/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about `build.yaml` at https://pub.dartlang.org/packages/build_config
builders:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should add a # some link here comment to point source diggers to the right spot to understand how this is suppose to work?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

json_serializable:
target: ":json_serializable"
import: "package:json_serializable/builder.dart"
builder_factories: ["jsonSerializable"]
build_extensions: {".dart": [".g.dart"]}
auto_apply: dependents
build_to: source
11 changes: 11 additions & 0 deletions json_serializable/lib/builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:build/build.dart';

import 'json_serializable.dart';

Builder jsonSerializable(BuilderOptions options) => jsonPartBuilder(
header: options.config['header'] as String,
useWrappers: options.config['use_wrappers'] as bool ?? false);
7 changes: 4 additions & 3 deletions json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
name: json_serializable
version: 0.3.0
version: 0.3.1-dev
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
environment:
sdk: '>=1.24.0 <2.0.0'
sdk: '>=2.0.0-dev <2.0.0'
dependencies:
Copy link
Collaborator

Choose a reason for hiding this comment

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

@natebosch – should update the SDK dependency then, too? Right?

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO it shouldn't be a requirement. It's pub's job to take dependency SDK constraints into account when version solving.

If I'm only using stuff from package:foo version 1.0.0 but my dependency tightened the constraint to 1.1.0 I don't go hunting for that information and bump tighten my constraint too - that's what the version solve is for. From my perspective the SDK constraint is just like a package constraint in this way.

In practice I think it's fine to do it since we're not testing on older SDKs so we're not confident that we haven't suddenly stopped working for reasons around this package.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

analyzer: '>=0.29.10 <0.31.0'
build: '>=0.9.0 <0.12.0'
build_config: ^0.2.1
cli_util: ^0.1.0

# Use a tight version constraint to ensure that a constraint on
Expand All @@ -16,7 +17,7 @@ dependencies:
path: ^1.3.2
source_gen: ^0.7.1
dev_dependencies:
build_runner: ^0.6.0+1
build_runner: ^0.7.0
build_test: ^0.9.0
collection: ^1.14.0
dart_style: ^1.0.0
Expand Down
3 changes: 2 additions & 1 deletion json_serializable/test/ensure_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void main() {
expect(_changedGeneratedFiles(), isEmpty);

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

Expand Down
Loading