Skip to content

Commit 5a316dd

Browse files
committed
example: improve readme documentation...
...and ensure pubspec/readme consistency with a test
1 parent 254f8c4 commit 5a316dd

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

example/README.md

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,68 @@
1-
An simple example of a Dart package using [json_serializable].
1+
To use [package:json_serializable][json_serializable] in your package, add these
2+
dependencies to your `pubspec.yaml`.
23

3-
* `lib/example.dart`: A library configured with annotations –
4-
`JsonSerializable` and `JsonLiteral` – to enable code generation.
4+
```yaml
5+
dependencies:
6+
json_annotation: ^0.2.1
57

6-
* Note: the annotations are defined in `package:json_annotation`.
7-
This is the only package required in the `dependencies` section of your
8-
`pubspec.yaml`.
8+
dev_dependencies:
9+
build_runner: ^0.6.1
10+
json_serializable: ^0.2.5
11+
source_gen: ^0.7.2+1
12+
```
913
10-
* `tool/`: Contains the code run during development to create and update
11-
generated code.
14+
Next, create the scripts to run code generation. The [`tool`][tool] directory
15+
contains the conventional layout of these scripts.
1216

13-
* `build_actions.dart`: A convention when using `package:build` to
14-
have one location to define the actions to be run by `build.dart` and
15-
`watch.dart`. See the comments in the source file for more information.
17+
* [`build_actions.dart`][tool]: A convention when using `package:build` to
18+
have one location to define the actions to be run by [`build.dart`][build] and
19+
[`watch.dart`][watch]. See the comments in the source file for more
20+
information.
1621

17-
* `build.dart`: Runs one build using the actions defined in
18-
`build_actions.dart`.
22+
* [`build.dart`][build]: Runs one build using the actions defined in
23+
[`build_actions.dart`][build_actions].
1924

20-
* `watch.dart`: Starts a watcher that (re)runs the actions defined in
21-
`build_actions.dart` when files change.
25+
* [`watch.dart`][watch]: Starts a watcher that (re)runs the actions defined in
26+
[`build_actions.dart`][build_actions] when files change.
2227

28+
Finally, annotate your code with classes defined in
29+
[package:json_annotation][json_annotation].
30+
31+
* See [`lib/example.dart`][example] for an example of a file using these
32+
annotations.
33+
34+
* See [`lib/example.g.dart`][example_g] for the generated file.
35+
36+
Run the build script to update the generated code after you modify of of the
37+
source files.
38+
39+
```console
40+
> dart bin/build.dart
41+
[INFO] BuildDefinition: Reading cached asset graph completed, took 54ms
42+
[INFO] BuildDefinition: Checking for updates since last build completed, took 663ms
43+
[INFO] Build: Running build completed, took 10ms
44+
[INFO] Build: Caching finalized dependency graph completed, took 44ms
45+
[INFO] Build: Succeeded after 68ms with 0 outputs
46+
```
47+
48+
*NOTE*: If you're using Flutter, running a Dart script is a bit tricky. You can
49+
find the Dart entry point at `$FLUTTER_ROOT/bin/cache/dart-sdk/bin/dart`.
50+
You can determine `FLUTTER_ROOT` by looking at the first few lines of
51+
`flutter doctor`.
52+
53+
```console
54+
> flutter doctor
55+
[✓] Flutter (on Mac OS X 10.12.6 16G1114, locale en-US, channel master)
56+
• Flutter at YOUR_FLUTTER_ROOT_HERE
57+
• Framework revision 235b64ed2f (13 hours ago), 2017-12-14 20:38:39 -0800
58+
...
59+
```
60+
61+
[tool]: tool
62+
[build_actions]: tool/build_actions.dart
63+
[build]: tool/build.dart
64+
[watch]: tool/watch.dart
65+
[example]: lib/example.dart
66+
[example_g]: lib/example.g.dart
67+
[json_annotation]: https://pub.dartlang.org/packages/json_annotation
2368
[json_serializable]: https://pub.dartlang.org/packages/json_serializable

example/test/readme_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 'dart:io';
6+
7+
import 'package:test/test.dart';
8+
9+
void main() {
10+
_expect('README.md');
11+
_expect('pubspec.yaml');
12+
}
13+
14+
void _expect(String fileName) {
15+
test(fileName, () {
16+
final file = new File(fileName);
17+
expect(file.readAsStringSync(), contains(_pubspecContent));
18+
});
19+
}
20+
21+
final _pubspecContent = r'''
22+
dependencies:
23+
json_annotation: ^0.2.1
24+
25+
dev_dependencies:
26+
build_runner: ^0.6.1
27+
json_serializable: ^0.2.5
28+
source_gen: ^0.7.2+1
29+
''';

0 commit comments

Comments
 (0)