Skip to content

Commit aaf5164

Browse files
authored
add code snippet doc (Azure#27481)
* add code snippet doc * Update
1 parent 099878e commit aaf5164

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

doc/dev/code_snippets.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Code Snippets
2+
3+
All code snippets in README should be up to date and runnable. And we want to have single source of truth hence we only need to maintain one copy. To achieve it, we want to have:
4+
5+
- [Store samples in samples folder and setup the pipeline to run them to make sure no sample is broken](#create-samples)
6+
- [In README, refer the code snippet from the samples](#refer-samples)
7+
- [Run `python_snippet_updater.py` to keep them in sync](#python_snippet_updater-tool)
8+
9+
## Create samples
10+
11+
Samples need to store in the samples folder under each service. In each sample file, we use `# [START snippet_name]` & `# [END snippet_name]` to note a code snippet.
12+
13+
A sample code snippet could be like:
14+
15+
```python
16+
# [START asyncio]
17+
from azure.core.pipeline.transport import AsyncioRequestsTransport
18+
19+
async with AsyncPipeline(AsyncioRequestsTransport(), policies=policies) as pipeline:
20+
response = await pipeline.run(request)
21+
# [END asyncio]
22+
```
23+
24+
https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/samples/test_example_async.py#L63-L68
25+
26+
## Refer samples
27+
28+
Instead of copying the code snippet into README which is hard to maintain and validate, we add reference to the sample snippet in README. We use the annotation <!-- SNIPPET:file_name.snippet_name--> to refer the code snippet in README file.
29+
30+
It is like:
31+
32+
<!-- SNIPPET:test_example_async.asyncio -->
33+
```python
34+
from azure.core.pipeline.transport import AsyncioRequestsTransport
35+
36+
async with AsyncPipeline(AsyncioRequestsTransport(), policies=policies) as pipeline:
37+
response = await pipeline.run(request)
38+
```
39+
<!-- END SNIPPET -->
40+
41+
## Run python_snippet_updater tool
42+
43+
```powershell
44+
python <azure-sdk-for-python>/tools/azure-sdk-tools/ci_tools/snippet_update/python_snippet_updater.py <path_to_the_service>
45+
```
46+
47+
The script scans the snippets in samples folder and auto replace the snippets in README with the one from samples folder.
48+
49+
**NOTE: the snippets in README will be overwritten!**
50+
51+
Now you are all set!

tools/azure-sdk-tools/ci_tools/snippet_update/python_snippet_updater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,6 @@ def update_snippet(file: str) -> None:
120120
except UnicodeDecodeError:
121121
pass
122122
if not_up_to_date:
123-
_LOGGER.error(f'Error: code snippets are out of sync. Please run Python PythonSnippetUpdater.py "{path}" to fix it.')
123+
_LOGGER.error(f'Error: code snippets are out of sync. Please run Python python_snippet_updater.py "{path}" to fix it.')
124124
exit(1)
125125
_LOGGER.info(f"README.md under {path} is up to date.")

0 commit comments

Comments
 (0)