Skip to content

Commit 45f6f85

Browse files
asyncapi-botasyncapi-botderberg
authored
docs(generator): update latest generator documentation (#3602)
Co-authored-by: asyncapi-bot <[email protected]> Co-authored-by: Lukasz Gornicki <[email protected]>
1 parent a817f85 commit 45f6f85

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed

markdown/docs/tools/generator/file-templates.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ weight: 140
77

88
> **Note**: This section applies only to the Nunjucks render engine. For information on using the React render engine, refer to the [Generating files with the React render engine](#generating-files-with-the-react-render-engine) section below.
99
10+
> **Note**: Nunjucks renderer engine is deprecated and will be removed in the future. Use the React renderer engine instead. For more details read notes from release [@asyncapi/generator@2.6.0](https://github.com/asyncapi/generator/releases/tag/%40asyncapi%2Fgenerator%402.6.0).
11+
1012
It is possible to generate files for each specific object in your AsyncAPI documentation using the Nunjucks render engine. For example, you can specify a filename like `$$channel$$.js` to generate a file for each channel defined in your AsyncAPI. The following file-template names and extra variables are available:
1113

1214
- `$$channel$$`, within the template-file you have access to two variables [`channel`](https://github.com/asyncapi/parser-api/blob/master/docs/api.md#channel) and [`channelName`](https://github.com/asyncapi/parser-api/blob/master/docs/api.md#channels). Where the `channel` contains the current channel being rendered.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: "Migrating from `ag` CLI to AsyncAPI CLI"
3+
weight: 260
4+
---
5+
6+
This guide provides detailed instructions on how to transition from the old `ag` Generator CLI to the new AsyncAPI CLI.
7+
8+
## Options Overview
9+
10+
Here is a list of `ag` options and their equivalents in the AsyncAPI CLI:
11+
12+
- **-d, --disable-hook [hooks...]**
13+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --disable-hook <hookType>=<hookName>`
14+
15+
- **--debug**
16+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --debug`
17+
18+
- **-i, --install**
19+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --install`
20+
21+
- **-n, --no-overwrite &#x3C;glob&#x3E;**
22+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --no-overwrite <glob>`
23+
24+
- **-o, --output &#x3C;outputDir&#x3E;**
25+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --output <outputDir>`
26+
27+
- **-p, --param &#x3C;name&#x3D;value&#x3E;**
28+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --param <name=value>`
29+
30+
- **--force-write**
31+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --force-write`
32+
33+
- **--watch-template**
34+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --watch`
35+
36+
- **--map-base-url &#x3C;url:folder&#x3E;**
37+
- **AsyncAPI CLI equivalent:** `asyncapi generate fromTemplate <ASYNCAPI> <TEMPLATE> --map-base-url <url:folder>`
38+
39+
## Migration Steps
40+
41+
### 1. Install AsyncAPI CLI
42+
43+
There are multiple different artifacts that AsyncAPI CLI is provided as. Get familiar with the [official CLI installation guide](https://www.asyncapi.com/docs/tools/cli/installation).
44+
45+
### 2. Update Your Commands
46+
47+
Replace the deprecated `ag` commands with the AsyncAPI CLI equivalents. Below are examples of how to update your commands:
48+
49+
**Using `ag`**:
50+
```
51+
ag ./asyncapi.yaml ./template -o ./output -p param1=value1 --debug --install --disable-hook hookType=hookName
52+
```
53+
54+
**Using AsyncAPI CLI**:
55+
```
56+
asyncapi generate fromTemplate ./asyncapi.yaml ./template -o ./output -p param1=value1 --debug --install --disable-hook hookType=hookName
57+
```
58+
59+
Notice that the change basically related to changing from `ag` to `asyncapi generate fromTemplate`.
60+
61+
### 3. Verify and Test
62+
63+
Run the updated commands to ensure they work as expected and verify that the output files are generated correctly.
64+
65+
## Additional Resources
66+
67+
**CLI Documentation**: [AsyncAPI CLI Documentation](https://www.asyncapi.com/docs/tools/cli)
68+
**Installation**: [AsyncAPI CLI Installation](https://www.asyncapi.com/docs/tools/cli/installation)
69+
**Usage**: [AsyncAPI CLI Usage](https://www.asyncapi.com/docs/tools/cli/usage)
70+
**Support**: For any issues with CLI, create an issue in [CLI repository](https://github.com/asyncapi/cli).
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
title: "Migrating from Nunjucks to React render engine"
3+
weight: 250
4+
---
5+
6+
The AsyncAPI Generator is moving away from Nunjucks templates in favor of React templates. This guide will help you migrate your existing Nunjucks templates to React. For a comprehensive understanding of why we introduced React as an alternative in 2021 and why we're now removing Nunjucks entirely, please read our article [React as a Generator Engine](https://www.asyncapi.com/blog/react-as-generator-engine). The principles discussed in this article still apply to our current transition.
7+
8+
## Step-by-step migration guide
9+
10+
### 1. Update package.json
11+
12+
Change your template configuration in `package.json`:
13+
14+
```json
15+
{
16+
"generator": {
17+
"renderer": "react"
18+
}
19+
}
20+
```
21+
22+
Once the deprecation period has ended, and we remove the default Nunjucks, the React render engine will be used by default and this setting will no longer be needed to configure
23+
24+
### 2. Install dependencies
25+
26+
Install the necessary React dependencies:
27+
28+
```bash
29+
npm install @asyncapi/generator-react-sdk
30+
```
31+
32+
### 3. File naming
33+
34+
In Nunjucks, the template's filename directly corresponds to the output file. For example, a template named **index.html** will generate an **index.html** file.
35+
36+
In React, the filename of the generated file is not controlled by the file itself, but rather by the `File` component. The React component itself can be named anything with a `.js` extension because the output filename is controlled by the `name` attribute of the `File` component used inside the template file. Below you can see some examples of filenames:
37+
38+
Nunjucks: `index.html`
39+
React: `index.js` or `index.html.js` or `anything-you-want.js`
40+
41+
### 4. Basic template structure
42+
43+
Nunjucks:
44+
```js
45+
<h1>{{ asyncapi.info().title() }}</h1>
46+
<p>{{ asyncapi.info().description() }}</p>
47+
```
48+
49+
React:
50+
```js
51+
import { File } from '@asyncapi/generator-react-sdk';
52+
53+
export default function({ asyncapi }) {
54+
return (
55+
<File name="index.html">
56+
<h1>{asyncapi.info().title()}</h1>
57+
<p>{asyncapi.info().description()}</p>
58+
</File>
59+
);
60+
}
61+
```
62+
63+
### 5. Macros and Partials
64+
65+
Replace macros with React components:
66+
67+
Nunjucks:
68+
```js
69+
{% macro renderChannel(channel) %}
70+
<div class="channel">
71+
<h3>{{ channel.address() }}</h3>
72+
<p>{{ channel.description() }}</p>
73+
</div>
74+
{% endmacro %}
75+
76+
{{ renderChannel(someChannel) }}
77+
```
78+
79+
React:
80+
```js
81+
// components/Channel.js
82+
import { Text } from '@asyncapi/generator-react-sdk';
83+
84+
export function Channel({ channel }) {
85+
return (
86+
<Text>
87+
<div className="channel">
88+
<h3>{channel.address()}</h3>
89+
<p>{channel.description()}</p>
90+
</div>
91+
</Text>
92+
);
93+
}
94+
95+
// Main template
96+
import { File, Text } from '@asyncapi/generator-react-sdk';
97+
import { Channel } from './components/Channel';
98+
99+
export default function({ asyncapi }) {
100+
return (
101+
<File name="channels.html">
102+
<Text>
103+
<h2>Channels</h2>
104+
</Text>
105+
{asyncapi.channels().map(channel => (
106+
<Channel channel={channel} />
107+
))}
108+
</File>
109+
);
110+
}
111+
```
112+
113+
### 6. File template
114+
115+
Check the [detailed guide on file templates](file-templates.md) to learn what is the difference between templating multiple file outputs in Nunjucks and React.
116+
117+
### 7. Models generation
118+
119+
If you have a template written with Nunjucks, it is almost certain that you have your own custom models, classes, or types templates in place. Instead of migrating them to React render engine we strongly advise you to delegate models generation to AsyncAPI Modelina project. Learn more about [how to add models generation using Modelina](https://www.asyncapi.com/docs/tools/generator/model-generation).
120+
121+
## Additional Resources and Information
122+
123+
### Template Examples
124+
For a complete example of React features in use, please refer to the [AsyncAPI Template for Generator Templates](https://github.com/asyncapi/template-for-generator-templates). The `master` branch demonstrates all React features, while the `nunjucks` branch shows the old Nunjucks implementation. This comparison can be particularly helpful in understanding the differences and migration process.
125+
126+
### Filters to Helpers
127+
If you've been using Nunjucks filters placed in the `filters` directory, you can still use this functionality in React. However, they should be treated as normal functions that you import into your components. We recommend renaming the `filters` directory to `helpers` to better reflect their new usage in React.
128+
129+
### Hooks Remain Unchanged
130+
It's important to note that hooks remain unchanged in this migration process. Hooks are not related to the render engine functionality, so you can continue to use them as you have been.
131+
132+
### Testing your migration
133+
134+
After migrating, test your template thoroughly:
135+
136+
1. Run the generator using your new React template
137+
2. Compare the output with the previous Nunjucks template output
138+
3. Check for any missing or incorrectly rendered content
139+
140+
Consider implementing snapshot tests for your template before starting the migration. This will ease the review of changes in comparing the content rendered after render engine changes. Snapshot tests allow you to have tests that will persist expected output from Nunjucks template, and compare it with output generated after the migration. Check out an [example of such snapshot integration test for our internal react template we use for development and testing](https://github.com/asyncapi/generator/blob/master/apps/generator/test/integration.test.js#L66).
141+
142+
## Conclusion
143+
144+
Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable code. You can learn more about why we introduced the React render engine from article [React as a Generator Engine](https://www.asyncapi.com/blog/react-as-generator-engine).

markdown/docs/tools/generator/nunjucks-render-engine.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: "Nunjucks render engine"
33
weight: 120
44
---
55

6+
> **Note**: Nunjucks renderer engine is deprecated and will be removed in the future. Use the React renderer engine instead. For more details read notes from release [@asyncapi/generator@2.6.0](https://github.com/asyncapi/generator/releases/tag/%40asyncapi%2Fgenerator%402.6.0).
7+
68
[Nunjucks](https://mozilla.github.io/nunjucks) is the default render engine, however, we strongly recommend adopting the [React](#react) engine.
79

810
### Common assumptions

0 commit comments

Comments
 (0)