|
| 1 | +[](https://github.com/dart-lang/tools/actions/workflows/api_summary.yaml) |
| 2 | +[](https://pub.dev/packages/api_summary) |
| 3 | +[](https://pub.dev/packages/api_summary/publisher) |
| 4 | + |
| 5 | +A library and command-line tool to create a human-readable text summary of the |
| 6 | +public API of a Dart package. This is highly suitable for tracking the public |
| 7 | +API footprints and ensuring that API modifications are visible during code |
| 8 | +reviews (e.g. using `diff` tests). |
| 9 | + |
| 10 | +> [!NOTE] |
| 11 | +> For robust breaking change tracking, you should use |
| 12 | +> [dart_apitool](https://pub.dev/packages/dart_apitool). |
| 13 | +
|
| 14 | +## Command Line Usage |
| 15 | + |
| 16 | +You can use the command-line tool to generate an API summary for any package |
| 17 | +containing a `pubspec.yaml` file. |
| 18 | + |
| 19 | +### Installing Globally |
| 20 | + |
| 21 | +Activate the package using `dart pub global`: |
| 22 | + |
| 23 | +```bash |
| 24 | +dart pub global activate api_summary |
| 25 | +``` |
| 26 | + |
| 27 | +Then run the tool: |
| 28 | + |
| 29 | +```bash |
| 30 | +api_summary --package-path /path/to/your/package |
| 31 | +``` |
| 32 | + |
| 33 | +Or, to run against the package in the current working directory, just run: |
| 34 | + |
| 35 | +```bash |
| 36 | +api_summary |
| 37 | +``` |
| 38 | + |
| 39 | +### Running via `dart run` |
| 40 | + |
| 41 | +Alternatively, you can run the executable from within a package directory if |
| 42 | +`api_summary` is a dependency: |
| 43 | + |
| 44 | +```bash |
| 45 | +dart run api_summary |
| 46 | +``` |
| 47 | + |
| 48 | +### Options |
| 49 | + |
| 50 | +* `-p, --package-path`: The path to the package directory to summarize |
| 51 | + (defaults to the current working directory). |
| 52 | +* `-h, --help`: Prints usage instructions. |
| 53 | + |
| 54 | +## Programmatic Usage |
| 55 | + |
| 56 | +You can also use this package programmatically inside your Dart projects, such |
| 57 | +as in automated testing or continuous integration scripts. |
| 58 | + |
| 59 | +Add `api_summary` to your `pubspec.yaml`: |
| 60 | + |
| 61 | +```yaml |
| 62 | +dependencies: |
| 63 | + api_summary: ^0.1.0-wip |
| 64 | +``` |
| 65 | +
|
| 66 | +### Basic Example |
| 67 | +
|
| 68 | +Call the `summarizePackage` function to generate a package's public API |
| 69 | +representation: |
| 70 | + |
| 71 | +```dart |
| 72 | +import 'dart:io'; |
| 73 | +import 'package:api_summary/api_summary.dart'; |
| 74 | +
|
| 75 | +void main() async { |
| 76 | + final summary = await summarizePackage('/path/to/package', 'my_package'); |
| 77 | + print(summary); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +An executable programmatic example is also available in the |
| 82 | +[example/example.dart](example/example.dart) file. |
| 83 | + |
| 84 | +### Customizing the Summary |
| 85 | + |
| 86 | +Extend the `ApiSummaryCustomizer` class to customize what is displayed in the |
| 87 | +API summary. For example, to exclude specific public classes or only display |
| 88 | +details of certain elements: |
| 89 | + |
| 90 | +```dart |
| 91 | +import 'package:api_summary/api_summary.dart'; |
| 92 | +import 'package:analyzer/dart/element/element.dart'; |
| 93 | +
|
| 94 | +base class MyCustomizer extends ApiSummaryCustomizer { |
| 95 | + @override |
| 96 | + bool shouldShowDetails(Element element) { |
| 97 | + // Exclude elements named 'InternalHelper' from details printout |
| 98 | + if (element.name == 'InternalHelper') { |
| 99 | + return false; |
| 100 | + } |
| 101 | + return super.shouldShowDetails(element); |
| 102 | + } |
| 103 | +} |
| 104 | +
|
| 105 | +void main() async { |
| 106 | + final summary = await summarizePackage( |
| 107 | + '/path/to/package', |
| 108 | + 'my_package', |
| 109 | + createCustomizer: () => MyCustomizer(), |
| 110 | + ); |
| 111 | + print(summary); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Golden File / Diff Testing |
| 116 | + |
| 117 | +A common best practice with `api_summary` is to verify in a unit test that the |
| 118 | +generated summary matches a checked-in golden file (e.g. `api.txt`). If a |
| 119 | +developer introduces an accidental breaking change or adds a new public API |
| 120 | +element, the test will fail on the `diff`, prompting them to audit and |
| 121 | +intentionally update the golden file. |
| 122 | + |
| 123 | +Below is an example of such a test (available in the |
| 124 | +[test/app_test.dart](test/app_test.dart) file): |
| 125 | + |
| 126 | +```dart |
| 127 | +import 'dart:io'; |
| 128 | +import 'package:path/path.dart' as p; |
| 129 | +import 'package:test/test.dart'; |
| 130 | +import 'package:api_summary/api_summary.dart'; |
| 131 | +
|
| 132 | +void main() { |
| 133 | + test('public API has not changed unexpectedly', () async { |
| 134 | + final packageDir = Directory.current.path; |
| 135 | + final goldenFile = File(p.join(packageDir, 'api.txt')); |
| 136 | +
|
| 137 | + final actualOutput = await summarizePackage(packageDir, 'my_package'); |
| 138 | +
|
| 139 | + if (!goldenFile.existsSync()) { |
| 140 | + // In a new setup or after updates, generate the golden file first |
| 141 | + goldenFile.writeAsStringSync(actualOutput); |
| 142 | + fail('Golden file api.txt did not exist and has been generated. Please review and commit it.'); |
| 143 | + } |
| 144 | +
|
| 145 | + final expectedOutput = goldenFile.readAsStringSync(); |
| 146 | + expect(actualOutput, equals(expectedOutput)); |
| 147 | + }); |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +## Status: experimental |
| 152 | + |
| 153 | +**NOTE**: This package is currently experimental and published under the |
| 154 | +[tools.dart.dev](https://dart.dev/dart-team-packages) pub publisher in order to |
| 155 | +solicit feedback. |
| 156 | + |
| 157 | +These packages have a much higher expected rate of API and breaking changes. |
| 158 | + |
| 159 | +Your feedback is valuable and will help us evolve this package. For general |
| 160 | +feedback, suggestions, and comments, please file an issue in the |
| 161 | +[bug tracker](https://github.com/dart-lang/tools/issues). |
0 commit comments