Updating snippets documentation README.md (#50483)
Updates the README file for snippets.
This commit is contained in:
parent
8ef5e2f046
commit
c6e45ffa30
@ -1,7 +1,7 @@
|
|||||||
# Dartdoc Generation
|
# Dartdoc Generation
|
||||||
|
|
||||||
The Flutter API documentation contains code blocks that help provide
|
The Flutter API documentation contains code blocks that help provide context or
|
||||||
context or a good starting point when learning to use any of Flutter's APIs.
|
a good starting point when learning to use any of Flutter's APIs.
|
||||||
|
|
||||||
To generate these code blocks, Flutter uses dartdoc tools to turn documentation
|
To generate these code blocks, Flutter uses dartdoc tools to turn documentation
|
||||||
in the source code into API documentation, as seen on https://api.flutter.dev/.
|
in the source code into API documentation, as seen on https://api.flutter.dev/.
|
||||||
@ -16,13 +16,23 @@ in the source code into API documentation, as seen on https://api.flutter.dev/.
|
|||||||
|
|
||||||
## Types of code blocks
|
## Types of code blocks
|
||||||
|
|
||||||
There's two kinds of code blocks.
|
There are three kinds of code blocks.
|
||||||
|
|
||||||
* snippets, which are more or less context-free code snippets that we
|
* A `snippet`, which is a more or less context-free code snippet that we
|
||||||
magically determine how to analyze, and
|
magically determine how to analyze, and
|
||||||
|
|
||||||
* samples, which get placed into a full-fledged application, and can
|
* A `dartpad` sample, which gets placed into a full-fledged application, and can
|
||||||
be actually executed inline in the documentation using DartPad.
|
be actually executed inline in the documentation on the web page using
|
||||||
|
DartPad.
|
||||||
|
|
||||||
|
* A `sample`, which gets placed into a full-fledged application, but isn't
|
||||||
|
placed into DartPad in the documentation because it doesn't make sense to do
|
||||||
|
so.
|
||||||
|
|
||||||
|
Ideally every sample is a DartPad sample, but some samples don't have any visual
|
||||||
|
representation, and some just don't make sense that way (for example, sample
|
||||||
|
code for setting the system UI's notification area color on Android won't do
|
||||||
|
anything on the web).
|
||||||
|
|
||||||
### Snippet Tool
|
### Snippet Tool
|
||||||
|
|
||||||
@ -45,41 +55,39 @@ code. Here is an example of the code `snippet` tool in use:
|
|||||||
/// {@end-tool}
|
/// {@end-tool}
|
||||||
```
|
```
|
||||||
|
|
||||||
This will generate sample code that can be copied to the clipboard and added
|
This will generate sample code that can be copied to the clipboard and added to
|
||||||
to existing applications.
|
existing applications.
|
||||||
|
|
||||||
This uses the skeleton for [snippet](config/skeletons/snippet.html)
|
This uses the skeleton for [snippet](config/skeletons/snippet.html) snippets
|
||||||
snippets when generating the HTML to put into the Dart docs.
|
when generating the HTML to put into the Dart docs.
|
||||||
|
|
||||||
#### Analysis
|
#### Analysis
|
||||||
|
|
||||||
The `../bots/analyze-sample-code.dart` script finds code inside the
|
The `../bots/analyze-sample-code.dart` script finds code inside the `@tool
|
||||||
`@tool snippet` sections and uses the Dart analyzer to check them.
|
snippet` sections and uses the Dart analyzer to check them.
|
||||||
|
|
||||||
There are several kinds of sample code you can specify:
|
There are several kinds of sample code you can specify:
|
||||||
|
|
||||||
* Constructor calls, typically showing what might exist in a build
|
* Constructor calls, typically showing what might exist in a build method. These
|
||||||
method. These will be inserted into an assignment expression
|
will be inserted into an assignment expression assigning to a variable of type
|
||||||
assigning to a variable of type "dynamic" and followed by a
|
"dynamic" and followed by a semicolon, for the purposes of analysis.
|
||||||
semicolon, for the purposes of analysis.
|
|
||||||
|
|
||||||
* Class definitions. These start with "class", and are analyzed
|
* Class definitions. These start with "class", and are analyzed verbatim.
|
||||||
verbatim.
|
|
||||||
|
|
||||||
* Other code. It gets included verbatim, though any line that says
|
* Other code. It gets included verbatim, though any line that says `// ...` is
|
||||||
`// ...` is considered to separate the block into multiple blocks
|
considered to separate the block into multiple blocks to be processed
|
||||||
to be processed individually.
|
individually.
|
||||||
|
|
||||||
The above means that it's tricky to include verbatim imperative code
|
The above means that it's tricky to include verbatim imperative code (e.g. a
|
||||||
(e.g. a call to a method), since it won't be valid to have such code
|
call to a method), since it won't be valid to have such code at the top level.
|
||||||
at the top level. Instead, wrap it in a function or even a whole
|
Instead, wrap it in a function or even a whole class, or make it a valid
|
||||||
class, or make it a valid variable declaration.
|
variable declaration.
|
||||||
|
|
||||||
You can declare code that should be included in the analysis but not
|
You can declare code that should be included in the analysis but not shown in
|
||||||
shown in the API docs by adding a comment "// Examples can assume:" to
|
the API docs by adding a comment "// Examples can assume:" to the file (usually
|
||||||
the file (usually at the top of the file, after the imports),
|
at the top of the file, after the imports), following by one or more
|
||||||
following by one or more commented-out lines of code. That code is
|
commented-out lines of code. That code is included verbatim in the analysis. For
|
||||||
included verbatim in the analysis. For example:
|
example:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
// Examples can assume:
|
// Examples can assume:
|
||||||
@ -95,9 +103,12 @@ You can assume that the entire Flutter framework and most common
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
The code `sample` tool can expand sample code into full Flutter applications.
|
The code `sample` and `dartpad` tools can expand sample code into full Flutter
|
||||||
These sample applications can be directly copied and used to demonstrate the
|
applications. These sample applications can be directly copied and used to
|
||||||
API's functionality in a sample application:
|
demonstrate the API's functionality in a sample application, or used with the
|
||||||
|
`flutter create` command to create a local project with the sample code. The
|
||||||
|
`dartpad` samples are embedded into the API docs web page and are live
|
||||||
|
applications in the API documentation.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
/// {@tool sample --template=stateless_widget_material}
|
/// {@tool sample --template=stateless_widget_material}
|
||||||
@ -129,31 +140,34 @@ API's functionality in a sample application:
|
|||||||
This uses the skeleton for [application](config/skeletons/sample.html)
|
This uses the skeleton for [application](config/skeletons/sample.html)
|
||||||
snippets.
|
snippets.
|
||||||
|
|
||||||
Code `sample` also allow for quick Flutter app generation using the following command:
|
The `sample` and `dartpad` tools also allow for quick Flutter app generation
|
||||||
|
using the following command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
flutter create --sample=[directory.File.sampleNumber] [name_of_project_directory]
|
flutter create --sample=[directory.File.sampleNumber] [name_of_project_directory]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This command is displayed as part of the sample in the API docs.
|
||||||
|
|
||||||
#### Templates
|
#### Templates
|
||||||
|
|
||||||
In order to support showing an entire app when you click on the right tab of
|
In order to support showing an entire app when you click on the right tab of the
|
||||||
the code sample UI, we have to be able to insert the `sample` block into the
|
code sample UI, we have to be able to insert the `sample` or `dartpad` block
|
||||||
template and instantiate the right parts.
|
into the template and instantiate the right parts.
|
||||||
|
|
||||||
To do this, there is a [config/templates](config/templates) directory that
|
To do this, there is a [config/templates](config/templates) directory that
|
||||||
contains a list of templates. These templates represent an entire app that the
|
contains a list of templates. These templates represent an entire app that the
|
||||||
`sample` can be placed into, basically a replacement for `lib/main.dart` in a
|
`sample` or `dartpad` can be placed into, basically a replacement for
|
||||||
flutter app package.
|
`lib/main.dart` in a flutter app package.
|
||||||
|
|
||||||
For more information about how to create, use, or update templates, see
|
For more information about how to create, use, or update templates, see
|
||||||
[config/templates/README.md](config/templates/README.md).
|
[config/templates/README.md](config/templates/README.md).
|
||||||
|
|
||||||
#### Analysis
|
#### Analysis
|
||||||
|
|
||||||
The `../bots/analyze-sample-code.dart` script finds code inside the
|
The `../bots/analyze-sample-code.dart` script finds code inside the `@tool
|
||||||
`@tool sample` sections and uses the Dart analyzer to check them
|
sample` sections and uses the Dart analyzer to check them after applying the
|
||||||
after applying the specified template.
|
specified template.
|
||||||
|
|
||||||
## Skeletons
|
## Skeletons
|
||||||
|
|
||||||
@ -161,7 +175,8 @@ A skeleton (in relation to this tool) is an HTML template into which the Dart
|
|||||||
code blocks and descriptions are interpolated.
|
code blocks and descriptions are interpolated.
|
||||||
|
|
||||||
There is currently one skeleton for
|
There is currently one skeleton for
|
||||||
[application](config/skeletons/sample.html) samples and one for
|
[application](config/skeletons/sample.html) samples, one for
|
||||||
|
[dartpad](config/skeletons/dartpad-sample.html) and one for
|
||||||
[snippet](config/skeletons/snippet.html) code samples, but there could be more.
|
[snippet](config/skeletons/snippet.html) code samples, but there could be more.
|
||||||
|
|
||||||
Skeletons use mustache notation (e.g. `{{code}}`) to mark where components will
|
Skeletons use mustache notation (e.g. `{{code}}`) to mark where components will
|
||||||
@ -184,8 +199,30 @@ that your code blocks are showing up correctly:
|
|||||||
|
|
||||||
1. Make an update to a code block or create a new code block.
|
1. Make an update to a code block or create a new code block.
|
||||||
2. From the root directory, run `./dev/bots/docs.sh`. This should start
|
2. From the root directory, run `./dev/bots/docs.sh`. This should start
|
||||||
generating a local copy of the API documentation.
|
generating a local copy of the API documentation.
|
||||||
3. Once complete, check `./dev/docs/doc` to check your API documentation. The
|
3. Once complete, check `./dev/docs/doc` to check your API documentation. The
|
||||||
search bar will not work locally, so open `./dev/docs/doc/index.html` to
|
search bar will not work locally, so open `./dev/docs/doc/index.html` to
|
||||||
navigate through the documentation, or search `./dev/docs/doc/flutter` for your
|
navigate through the documentation, or search `./dev/docs/doc/flutter` for
|
||||||
page of interest.
|
your page of interest.
|
||||||
|
|
||||||
|
Note that generating the sample output will not allow you to run your code in
|
||||||
|
DartPad, because DartPad pulls the code it runs from the appropriate docs server
|
||||||
|
(master or stable).
|
||||||
|
|
||||||
|
Copy the generated code and paste it into a regular DartPad instance to test if
|
||||||
|
it runs in DartPad. To get the code that will be produced by your documentation
|
||||||
|
changes, run sample analysis locally (see the next section) and paste the output
|
||||||
|
into a DartPad at https://dartpad.dartlang.org.
|
||||||
|
|
||||||
|
## Running sample analysis locally
|
||||||
|
|
||||||
|
If all you want to do is analyze the sample code you have written locally, then
|
||||||
|
generating the entire docs output takes a long time.
|
||||||
|
|
||||||
|
Instead, you can run the analysis locally with this command from the Flutter root:
|
||||||
|
|
||||||
|
```
|
||||||
|
TMPDIR=/tmp bin/cache/dart-sdk/bin/dart dev/bots/analyze-sample-code.dart --temp=samples
|
||||||
|
```
|
||||||
|
|
||||||
|
This will analyze the samples, and leave the output in /tmp/samples
|
||||||
|
Loading…
x
Reference in New Issue
Block a user