Skip to content

Commit 5dcb86f

Browse files
authoredMay 9, 2024··
[flutter_releases] Flutter stable 3.22.0 Framework Cherrypicks (#147950)
# Flutter stable 3.22.0 Framework ## Scheduled Cherrypicks
1 parent 87b6524 commit 5dcb86f

22 files changed

+4539
-12
lines changed
 

‎bin/internal/engine.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b4bfd459865a8d636f26aca0d330ae297c006c3c
1+
f6344b75dcf861d8bf1f1322780b8811f982e31a

‎dartdoc_options.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ dartdoc:
55
# The dev/bots/docs.sh script does this automatically.
66
tools:
77
snippet:
8-
command: ["bin/cache/dart-sdk/bin/dart", "pub", "global", "run", "snippets", "--output-directory=doc/snippets", "--type=snippet"]
8+
command: ["bin/cache/artifacts/snippets/snippets", "--output-directory=doc/snippets", "--type=snippet"]
99
description: "Creates sample code documentation output from embedded documentation samples."
1010
sample:
11-
command: ["bin/cache/dart-sdk/bin/dart", "pub", "global", "run", "snippets", "--output-directory=doc/snippets", "--type=sample"]
11+
command: ["bin/cache/artifacts/snippets/snippets", "--output-directory=doc/snippets", "--type=sample"]
1212
description: "Creates full application sample code documentation output from embedded documentation samples."
1313
dartpad:
14-
command: ["bin/cache/dart-sdk/bin/dart", "pub", "global", "run", "snippets", "--output-directory=doc/snippets", "--type=dartpad"]
14+
command: ["bin/cache/artifacts/snippets/snippets", "--output-directory=doc/snippets", "--type=dartpad"]
1515
description: "Creates full application sample code documentation output from embedded documentation samples and displays it in an embedded DartPad."
1616
errors:
1717
## Default errors of dartdoc:

‎dev/bots/docs.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,25 @@ function parse_args() {
107107
fi
108108
}
109109

110+
function build_snippets_tool() (
111+
local snippets_dir="$FLUTTER_ROOT/dev/snippets"
112+
local output_dir="$FLUTTER_BIN/cache/artifacts/snippets"
113+
echo "Building snippets tool executable."
114+
command cd "$snippets_dir"
115+
mkdir -p "$output_dir"
116+
dart pub get
117+
dart compile exe -o "$output_dir/snippets" bin/snippets.dart
118+
)
119+
110120
function generate_docs() {
111121
# Install and activate dartdoc.
112122
# When updating to a new dartdoc version, please also update
113123
# `dartdoc_options.yaml` to include newly introduced error and warning types.
114124
"$DART" pub global activate dartdoc 8.0.6
115125

116-
# Install and activate the snippets tool, which resides in the
117-
# assets-for-api-docs repo:
118-
# https://github.com/flutter/assets-for-api-docs/tree/main/packages/snippets
119-
"$DART" pub global activate snippets 0.4.3
126+
# Build and install the snippets tool, which resides in
127+
# the dev/docs/snippets directory.
128+
build_snippets_tool
120129

121130
# This script generates a unified doc set, and creates
122131
# a custom index.html, placing everything into DOC_DIR.

‎dev/snippets/README.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# Dartdoc Sample Generation
2+
3+
The Flutter API documentation contains code blocks that help provide context or
4+
a good starting point when learning to use any of Flutter's APIs.
5+
6+
To generate these code blocks, Flutter uses dartdoc tools to turn documentation
7+
in the source code into API documentation, as seen on [https://api.flutter.dev/]
8+
9+
## Table of Contents
10+
11+
- [Types of code blocks](#types-of-code-blocks)
12+
- [Snippet tool](#snippet-tool)
13+
- [Sample tool](#sample-tool)
14+
- [Skeletons](#skeletons)
15+
- [Test Doc Generation Workflow](#test-doc-generation-workflow)
16+
17+
## Types of code blocks
18+
19+
There are three kinds of code blocks.
20+
21+
- A `snippet`, which is a more or less context-free code snippet that we
22+
magically determine how to analyze.
23+
24+
- A `dartpad` sample, which gets placed into a full-fledged application, and can
25+
be executed inline in the documentation on the web page using
26+
DartPad.
27+
28+
- A `sample`, which gets placed into a full-fledged application, but isn't
29+
placed into DartPad in the documentation because it doesn't make sense to do
30+
so.
31+
32+
Ideally, every sample is a DartPad sample, but some samples don't have any visual
33+
representation and some just don't make sense that way (for example, sample
34+
code for setting the system UI's notification area color on Android won't do
35+
anything on the web).
36+
37+
### Snippet Tool
38+
39+
![Code snippet image](assets/code_snippet.png)
40+
41+
The code `snippet` tool generates a block containing a description and example
42+
code. Here is an example of the code `snippet` tool in use:
43+
44+
```dart
45+
/// {@tool snippet}
46+
///
47+
/// If the avatar is to have an image, the image should be specified in the
48+
/// [backgroundImage] property:
49+
///
50+
/// ```dart
51+
/// CircleAvatar(
52+
/// backgroundImage: NetworkImage(userAvatarUrl),
53+
/// )
54+
/// ```
55+
/// {@end-tool}
56+
```
57+
58+
This will generate sample code that can be copied to the clipboard and added to
59+
existing applications.
60+
61+
This uses the skeleton for `snippet` snippets when generating the HTML to put
62+
into the Dart docs. You can find this [template in the Flutter
63+
repo](https://github.com/flutter/flutter/blob/main/dev/snippets/config/skeletons/snippet.html).
64+
65+
#### Analysis
66+
67+
The
68+
[`analyze_sample_code.dart`](https://github.com/flutter/flutter/blob/main/dev/bots/analyze_sample_code.dart)
69+
script finds code inside the `@tool
70+
snippet` sections and uses the Dart analyzer to check them.
71+
72+
There are several kinds of sample code you can specify:
73+
74+
- Constructor calls, typically showing what might exist in a build method. These
75+
will be inserted into an assignment expression assigning to a variable of type
76+
"dynamic" and followed by a semicolon, for analysis.
77+
78+
- Class definitions. These start with "class", and are analyzed verbatim.
79+
80+
- Other code. It gets included verbatim, though any line that says `// ...` is
81+
considered to separate the block into multiple blocks to be processed
82+
individually.
83+
84+
The above means that it's tricky to include verbatim imperative code (e.g. a
85+
call to a method) since it won't be valid to have such code at the top level.
86+
Instead, wrap it in a function or even a whole class, or make it a valid
87+
variable declaration.
88+
89+
You can declare code that should be included in the analysis but not shown in
90+
the API docs by adding a comment "// Examples can assume:" to the file (usually
91+
at the top of the file, after the imports), following by one or more
92+
commented-out lines of code. That code is included verbatim in the analysis. For
93+
example:
94+
95+
```dart
96+
// Examples can assume:
97+
// final BuildContext context;
98+
// final String userAvatarUrl;
99+
```
100+
101+
You can assume that the entire Flutter framework and most common
102+
`dart:*` packages are imported and in scope; `dart:math` as `math` and
103+
`dart:ui` as `ui`.
104+
105+
### Sample Tool
106+
107+
![Code sample image](assets/code_sample.png)
108+
109+
The code `sample` and `dartpad` tools can expand sample code into full Flutter
110+
applications. These sample applications can be directly copied and used to
111+
demonstrate the API's functionality in a sample application, or used with the
112+
`flutter create` command to create a local project with the sample code. The
113+
`dartpad` samples are embedded into the API docs web page and are live
114+
applications in the API documentation.
115+
116+
This uses the skeleton for [application](https://github.com/flutter/flutter/blob/main/dev/snippets/config/skeletons/sample.html)
117+
snippets in the Flutter repo.
118+
119+
The `sample` and `dartpad` tools also allow for quick Flutter app generation
120+
using the following command:
121+
122+
```bash
123+
flutter create --sample=[directory.File.sampleNumber] [name_of_project_directory]
124+
```
125+
126+
This command is displayed as part of the sample in the API docs.
127+
128+
#### Sample Analysis
129+
130+
The [`../bots/analyze_sample_code.dart`](../bots/analyze_sample_code.dart)
131+
script finds code inside the `@tool sample` sections and uses the Dart analyzer
132+
to check the sample code.
133+
134+
## Skeletons
135+
136+
A skeleton (concerning this tool) is an HTML template into which the Dart
137+
code blocks and descriptions are interpolated.
138+
139+
There is currently one skeleton for
140+
[application](https://github.com/flutter/flutter/blob/main/dev/snippets/config/skeletons/sample.html)
141+
samples, one for
142+
[dartpad](https://github.com/flutter/flutter/blob/main/dev/snippets/config/skeletons/dartpad-sample.html),
143+
and one for
144+
[snippet](https://github.com/flutter/flutter/blob/main/dev/snippets/config/skeletons/snippet.html)
145+
code samples, but there could be more.
146+
147+
Skeletons use mustache notation (e.g. `{{code}}`) to mark where components will
148+
be interpolated into the template. It doesn't use the mustache
149+
package since these are simple string substitutions, but it uses the same
150+
syntax.
151+
152+
The code block generation tools that process the source input and emit HTML for
153+
output, which dartdoc places back into the documentation. Any options given to
154+
the `{@tool ...}` directive are passed on verbatim to the tool.
155+
156+
The `snippets` tool renders these examples through a combination of markdown
157+
and HTML using the `{@inject-html}` dartdoc directive.
158+
159+
## Test Doc Generation Workflow
160+
161+
If you are making changes to an existing code block or are creating a new code
162+
block, follow these steps to generate a local copy of the API docs and verify
163+
that your code blocks are showing up correctly:
164+
165+
1. Make an update to a code block or create a new code block.
166+
2. From the root directory, run `./dev/bots/docs.sh`. This should start
167+
generating a local copy of the API documentation.
168+
Supplying the "--output" argument allows you to specify the output zip file
169+
for the completed documentation. Defaults to `api_docs.zip`` in the current
170+
directory.
171+
3. Once complete, unzip the files to the desired location and open the `index.html`
172+
within.
173+
174+
Note that generating the sample output will not allow you to run your code in
175+
DartPad, because DartPad pulls the code it runs from the appropriate docs server
176+
(main or stable).
177+
178+
Copy the generated code and paste it into a regular DartPad instance to test if
179+
it runs in DartPad. To get the code that will be produced by your documentation
180+
changes, run sample analysis locally (see the next section) and paste the output
181+
into a DartPad at [https://dartpad.dartlang.org].
182+
183+
## Running sample analysis locally
184+
185+
If all you want to do is analyze the sample code you have written locally, then
186+
generating the entire docs output takes a long time.
187+
188+
Instead, you can run the analysis locally with this command from the Flutter root:
189+
190+
```bash
191+
TMPDIR=/tmp bin/cache/dart-sdk/bin/dart dev/bots/analyze_sample_code.dart --temp=samples
192+
```
193+
194+
This will analyze the samples, and leave the generated files in `/tmp/samples`
195+
196+
You can find the sample you are working on in `/tmp/samples`. It is named using the
197+
path to the file it is in, and the line of the file that the `{@tool ...}` directive
198+
is on.
199+
200+
For example, the file `sample.src.widgets.animated_list.52.dart` points to the sample
201+
in `packages/flutter/src/widgets/animated_list.dart` at line 52. You can then take the
202+
contents of that file, and paste it into [Dartpad](https://dartpad.dev) and see if it
203+
works. If the sample relies on new features that have just landed, it may not work
204+
until the features make it into the `dev` branch.

0 commit comments

Comments
 (0)
Please sign in to comment.