Skip to content

Commit

Permalink
rebased the pr
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmin2 committed Aug 11, 2024
1 parent ebbf27e commit 54cc339
Show file tree
Hide file tree
Showing 20 changed files with 385 additions and 73 deletions.
22 changes: 22 additions & 0 deletions .archivist/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"projectId": "b4228f2e-a928-445e-83d8-6bc7a05c420e",
"ignoredFiles": [
".DS_Store",
".env",
".archivist/",
".idea/",
".vscode/",
"env/",
"venv/",
".git/",
".gitignore",
"__pycache__/",
"__init__.py",
"dist/",
"node_modules/",
"package-lock.json",
"yarn.lock",
"*.config.js",
".next/"
]
}
1 change: 0 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"linked": [],
"access": "public",
"baseBranch": "master",
"ignore": ["@asyncapi/nunjucks-filters"],
"updateInternalDependencies": "patch",
"privatePackages": {
"version": true,
Expand Down
5 changes: 0 additions & 5 deletions .changeset/proud-brooms-accept.md

This file was deleted.

6 changes: 5 additions & 1 deletion .github/workflows/release-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ jobs:
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha


- name: Check out the repository
uses: actions/checkout@v4

- name: Update Docker Hub Readme
uses: meeDamian/sync-readme@v1.0.6
with:
user: ${{ secrets.DOCKER_USERNAME }}
pass: ${{ secrets.DOCKER_PASSWORD }}
slug: asyncapi/generator
readme: ./apps/generator/README.md
description: Use your AsyncAPI definition to generate literally anything. Markdown documentation, Node.js code, HTML documentation, anything!
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ coverage
# Turbo
.turbo

/.idea
18 changes: 18 additions & 0 deletions apps/generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# @asyncapi/generator

## 2.3.0

### Minor Changes

- 44fcc33: ts-node is registered only when it's actually needed

## 2.2.0

### Minor Changes

- 81dfd0c: Enable `noOverwriteGlobs` option for templates based on react rendering engine.

## 2.1.3

### Patch Changes

- 93fb8e8: Updated the method for importing the Nunjucks filter dependency

## 2.1.2

### Patch Changes
Expand Down
28 changes: 28 additions & 0 deletions apps/generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Generator is a tool that you can use to generate whatever you want basing on the AsyncAPI specification file as an input. For more information [read the docs](https://www.asyncapi.com/docs/tools/generator).

There is a large number of templates that are ready to use and are officially supported by the AsyncAPI Initiative.

## List of official generator templates

<!-- templates list is validated with GitHub Actions do not remove list markers -->
<!-- TEMPLATES-LIST:START -->

| Template Name | Description | Source code |
| --------------------------------------------- | --------------------------------------------------------------- | --------------------------------------------------------------------------- |
| `@asyncapi/nodejs-template` | Generates Nodejs service that uses Hermes package | [click here](https://github.com/asyncapi/nodejs-template) |
| `@asyncapi/nodejs-ws-template` | Generates Nodejs service that supports WebSockets protocol only | [click here](https://github.com/asyncapi/nodejs-ws-template) |
| `@asyncapi/java-template` | Generates Java JMS application | [click here](https://github.com/asyncapi/java-template) |
| `@asyncapi/java-spring-template` | Generates Java Spring service | [click here](https://github.com/asyncapi/java-spring-template) |
| `@asyncapi/java-spring-cloud-stream-template` | Generates Java Spring Cloud Stream service | [click here](https://github.com/asyncapi/java-spring-cloud-stream-template) |
| `@asyncapi/python-paho-template` | Generates Python service that uses Paho library | [click here](https://github.com/asyncapi/python-paho-template) |
| `@asyncapi/html-template` | Generates HTML documentation site | [click here](https://github.com/asyncapi/html-template) |
| `@asyncapi/markdown-template` | Generates documentation in Markdown file | [click here](https://github.com/asyncapi/markdown-template) |
| `@asyncapi/ts-nats-template` | Generates TypeScript NATS client | [click here](https://github.com/asyncapi/ts-nats-template/) |
| `@asyncapi/go-watermill-template` | Generates Go client using Watermill | [click here](https://github.com/asyncapi/go-watermill-template) |
| `@asyncapi/dotnet-nats-template` | Generates .NET C# client using NATS | [click here](https://github.com/asyncapi/dotnet-nats-template) |
| `@asyncapi/php-template` | Generates PHP client using RabbitMQ | [click here](https://github.com/asyncapi/php-template) |
| `@asyncapi/dotnet-rabbitmq-template` | Generates .NET C# client using RabbitMQ | [click here](https://github.com/asyncapi/dotnet-rabbitmq-template) |

<!-- TEMPLATES-LIST:END -->

You can find above templates and the ones provided by the community in **[this list](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate)**
121 changes: 121 additions & 0 deletions apps/generator/docs/depreciate-nunjucks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
title: "Depreciation of nunjucks render engine"
weight: 170
---

# Migration Guide from nunjucks render engine to react render engine

## Introduction

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.

## Step-by-Step Migration Guide

### 1. Update package.json

Change your template configuration in `package.json`:

```json
{
"generator": {
"renderer": "react"
}
}
```

### 2. Install Dependencies

Install the necessary React dependencies:

```bash
npm install @asyncapi/generator-react-sdk
```

### 3. Basic Template Structure

Nunjucks:
```jsx
<h1>{{ asyncapi.info().title() }}</h1>
<p>{{ asyncapi.info().description() }}</p>
```

React:
```jsx
import { File } from '@asyncapi/generator-react-sdk';

export default function({ asyncapi }) {
return (
<File name="index.html">
<h1>{asyncapi.info().title()}</h1>
<p>{asyncapi.info().description()}</p>
</File>
);
}
```

### 4. Macros

Replace macros with React components:

Nunjucks:
```jsx
{% macro renderChannel(channel) %}
<div class="channel">
<h3>{{ channel.address() }}</h3>
<p>{{ channel.description() }}</p>
</div>
{% endmacro %}

{{ renderChannel(someChannel) }}
```

React:
```jsx
// components/Channel.js
import { Text } from '@asyncapi/generator-react-sdk';

export function Channel({ channel }) {
return (
<Text>
<div className="channel">
<h3>{channel.address()}</h3>
<p>{channel.description()}</p>
</div>
</Text>
);
}

// Main template
import { File, Text } from '@asyncapi/generator-react-sdk';
import { Channel } from './components/Channel';

export default function({ asyncapi }) {
return (
<File name="channels.html">
<Text>
<h2>Channels</h2>
</Text>
{asyncapi.channels().map(channel => (
<Channel key={channel.address()} channel={channel} />
))}
</File>
);
}
```

### 5. File template

//TODO: we can add a link to Florence docs once it is merged

## Testing Your Migration

After migrating, test your template thoroughly:

1. Run the generator with your new React template
2. Compare the output with the previous Nunjucks template output
3. Check for any missing or incorrectly rendered content

## Conclusion

Migrating from Nunjucks to React templates may require some initial effort, but it will result in more maintainable. You can read why we introduced react render engine [here](https://www.asyncapi.com/blog/react-as-generator-engine)

50 changes: 50 additions & 0 deletions apps/generator/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const path = require("path");
const Generator = require("./lib/generator");
const { promises: fsPromise } = require("fs");
const reactTemplate = 'test/test-templates/react-template';

// const reactTemplate = path.resolve(__dirname, "./test/test-templates/react-template");
// console.log("reactTemplate", reactTemplate);
const dummySpecPath = path.resolve(__dirname, './test/docs/dummy.yml');

async function main() {
const outputDir = path.resolve(__dirname, "test/hello");
console.log("outputDir", outputDir);
// await fsPromise.mkdir(outputDir, { recursive: true });

// Create temp.md.js file dynamically
const tempJsContent = `
import { File, Text } from '@asyncapi/generator-react-sdk';
export default function() {
return (
<File name="temp.md">
<Text>Test</Text>
</File>
);
}
`;
// const reactTemplate = path.resolve(__dirname, "./test/test-templates/react-template");
console.log("reactTemplate", reactTemplate);
const transpiledPath = path.join(reactTemplate, '__transpiled');

// await fsPromise.unlink(path.join(transpiledPath, 'temp.md.js'))
// await fsPromise.unlink(path.join(transpiledPath, 'temp.md.js.map'))

const tempJsPath = path.join(reactTemplate, "template","temp.md.js");
await fsPromise.writeFile(tempJsPath, tempJsContent);
console.log("tempJsPath", tempJsPath);

const generator = new Generator(reactTemplate, outputDir, {
forceWrite: true,
debug: true,
compile: false,
});
await generator.generateFromFile(dummySpecPath);

// Check if temp.md is created in the output directory
const tempMdPath = path.join(outputDir, "temp.md");
console.log("tempMdPath", tempMdPath);
}

main()
9 changes: 7 additions & 2 deletions apps/generator/lib/filtersRegistry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const fs = require('fs');
const xfs = require('fs.extra');
const { isAsyncFunction } = require('./utils');
const { isAsyncFunction, registerTypeScript } = require('./utils');
const nunjucksFilters = require('@asyncapi/nunjucks-filters');

/**
Expand Down Expand Up @@ -38,7 +38,12 @@ function registerLocalFilters(nunjucks, templateDir, filtersDir) {

walker.on('file', async (root, stats, next) => {
try {
const filePath = path.resolve(templateDir, path.resolve(root, stats.name));
const filePath = path.resolve(
templateDir,
path.resolve(root, stats.name)
);

registerTypeScript(filePath);
// If it's a module constructor, inject dependencies to ensure consistent usage in remote templates in other projects or plain directories.
delete require.cache[require.resolve(filePath)];
const mod = require(filePath);
Expand Down
10 changes: 5 additions & 5 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
isReactTemplate,
isJsFile,
registerSourceMap,
registerTypeScript,
getTemplateDetails,
convertCollectionToObject,
} = require('./utils');
Expand Down Expand Up @@ -59,7 +58,6 @@ const shouldIgnoreDir = dirPath =>
|| dirPath.startsWith(`.git${path.sep}`);

registerSourceMap();
registerTypeScript();

class Generator {
/**
Expand Down Expand Up @@ -854,7 +852,7 @@ class Generator {
if (renderContent === undefined) {
return;
} else if (isReactTemplate(this.templateConfig)) {
await saveRenderedReactContent(renderContent, outputpath);
await saveRenderedReactContent(renderContent, outputpath, this.noOverwriteGlobs);
} else {
await writeFile(outputpath, renderContent);
}
Expand Down Expand Up @@ -933,9 +931,11 @@ class Generator {
async renderFile(asyncapiDocument, filePath, extraTemplateData = {}) {
if (isReactTemplate(this.templateConfig)) {
return await renderReact(asyncapiDocument, filePath, extraTemplateData, this.templateDir, this.templateContentDir, TRANSPILED_TEMPLATE_LOCATION, this.templateParams, this.debug, this.originalAsyncAPI);
} else {
console.warn('Deprecation Warning: Nunjucks templates are deprecated. Please migrate to React templates.');
const templateString = await readFile(filePath, 'utf8');
return renderNunjucks(asyncapiDocument, templateString, filePath, extraTemplateData, this.templateParams, this.originalAsyncAPI, this.nunjucks);
}
const templateString = await readFile(filePath, 'utf8');
return renderNunjucks(asyncapiDocument, templateString, filePath, extraTemplateData, this.templateParams, this.originalAsyncAPI, this.nunjucks);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion apps/generator/lib/hooksRegistry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const xfs = require('fs.extra');
const { exists } = require('./utils');
const { exists, registerTypeScript } = require('./utils');

/**
* Registers all template hooks.
Expand Down Expand Up @@ -37,6 +37,9 @@ async function registerLocalHooks(hooks, templateDir, hooksDir) {
walker.on('file', async (root, stats, next) => {
try {
const filePath = path.resolve(templateDir, path.resolve(root, stats.name));

registerTypeScript(filePath);

delete require.cache[require.resolve(filePath)];
const mod = require(filePath);

Expand Down
Loading

0 comments on commit 54cc339

Please sign in to comment.