From 9b1f7e30c753547791866fb59af5979b4befdf8a Mon Sep 17 00:00:00 2001 From: Ashish Padhy <100484401+Shurtu-gal@users.noreply.github.com> Date: Tue, 23 May 2023 00:06:16 +0530 Subject: [PATCH 1/4] feat: add php support --- src/commands/generate/models.ts | 16 +++++++++++++--- test/commands/generate/models.test.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/commands/generate/models.ts b/src/commands/generate/models.ts index ffd33d44954..5c576246a17 100644 --- a/src/commands/generate/models.ts +++ b/src/commands/generate/models.ts @@ -1,4 +1,4 @@ -import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET } from '@asyncapi/modelina'; +import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator } from '@asyncapi/modelina'; import { Flags } from '@oclif/core'; import Command from '../../base'; import { load } from '../../models/SpecificationFile'; @@ -15,7 +15,8 @@ enum Languages { dart = 'dart', python = 'python', rust = 'rust', - kotlin='kotlin' + kotlin='kotlin', + php='php' } const possibleLanguageValues = Object.values(Languages).join(', '); @@ -91,7 +92,7 @@ export default class Models extends Command { * C# specific options */ namespace: Flags.string({ - description: 'C# specific, define the namespace to use for the generated models. This is required when language is `csharp`.', + description: 'C# and Php specific, define the namespace to use for the generated models. This is required when language is `csharp` or `php`.', required: false }), @@ -227,6 +228,15 @@ export default class Models extends Command { packageName }; break; + case Languages.php: + if (namespace === undefined) { + throw new Error('In order to generate models to PHP, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.'); + } + fileGenerator = new PhpFileGenerator(); + fileOptions = { + namespace + }; + break; default: throw new Error(`Could not determine generator for language ${language}, are you using one of the following values ${possibleLanguageValues}?`); } diff --git a/test/commands/generate/models.test.ts b/test/commands/generate/models.test.ts index 77f7329c8d2..83fd121742f 100644 --- a/test/commands/generate/models.test.ts +++ b/test/commands/generate/models.test.ts @@ -21,7 +21,7 @@ describe('models', () => { .stdout() .command([...generalOptions, 'random', './test/specification.yml', `-o=${ path.resolve(outputDir, './random')}`]) .it('fails when it dont know the language', (ctx, done) => { - expect(ctx.stderr).toEqual('Error: Expected random to be one of: typescript, csharp, golang, java, javascript, dart, python, rust, kotlin\nSee more help with --help\n'); + expect(ctx.stderr).toEqual('Error: Expected random to be one of: typescript, csharp, golang, java, javascript, dart, python, rust, kotlin, php\nSee more help with --help\n'); expect(ctx.stdout).toEqual(''); done(); }); @@ -248,7 +248,29 @@ describe('models', () => { done(); }); }); - + + describe('for Php', () => { + test + .stderr() + .stdout() + .command([...generalOptions, 'php', './test/specification.yml', `-o=${ path.resolve(outputDir, './php')}`, '--namespace=\'asyncapi.models\'']) + .it('works when file path is passed', (ctx, done) => { + expect(ctx.stderr).toEqual(''); + expect(ctx.stdout).toContain( + 'Successfully generated the following models: ' + ); + done(); + }); + test + .stderr() + .stdout() + .command([...generalOptions, 'php', './test/specification.yml', `-o=${ path.resolve(outputDir, './php')}`]) + .it('fails when no namespace defined', (ctx, done) => { + expect(ctx.stderr).toEqual('Error: In order to generate models to PHP, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.\n'); + expect(ctx.stdout).toEqual(''); + done(); + }); + }); describe('with logging diagnostics', () => { test .stderr() From 23f87a1eeea2ea9ba69eb4a505cb90c338bbb239 Mon Sep 17 00:00:00 2001 From: Ashish Padhy <100484401+Shurtu-gal@users.noreply.github.com> Date: Tue, 23 May 2023 16:52:04 +0530 Subject: [PATCH 2/4] fix: PHP casing --- src/commands/generate/models.ts | 2 +- test/commands/generate/models.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/generate/models.ts b/src/commands/generate/models.ts index 5c576246a17..fce68e8447d 100644 --- a/src/commands/generate/models.ts +++ b/src/commands/generate/models.ts @@ -92,7 +92,7 @@ export default class Models extends Command { * C# specific options */ namespace: Flags.string({ - description: 'C# and Php specific, define the namespace to use for the generated models. This is required when language is `csharp` or `php`.', + description: 'C# and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp` or `php`.', required: false }), diff --git a/test/commands/generate/models.test.ts b/test/commands/generate/models.test.ts index 83fd121742f..60352d0e936 100644 --- a/test/commands/generate/models.test.ts +++ b/test/commands/generate/models.test.ts @@ -249,7 +249,7 @@ describe('models', () => { }); }); - describe('for Php', () => { + describe('for PHP', () => { test .stderr() .stdout() From df10e7f7082f9c0f0ca76b4ff6c8c936ebfd3f8c Mon Sep 17 00:00:00 2001 From: Ashish Padhy <100484401+Shurtu-gal@users.noreply.github.com> Date: Tue, 23 May 2023 19:16:28 +0530 Subject: [PATCH 3/4] fix: model tests --- test/commands/generate/models.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/commands/generate/models.test.ts b/test/commands/generate/models.test.ts index 06e23fb49e9..a7c48e1471c 100644 --- a/test/commands/generate/models.test.ts +++ b/test/commands/generate/models.test.ts @@ -21,7 +21,7 @@ describe('models', () => { .stdout() .command([...generalOptions, 'random', './test/specification.yml', `-o=${ path.resolve(outputDir, './random')}`]) .it('fails when it dont know the language', (ctx, done) => { - expect(ctx.stderr).toEqual('Error: Expected random to be one of: typescript, csharp, golang, java, javascript, dart, python, rust, kotlin, cplusplus, php\nSee more help with --help\n'); + expect(ctx.stderr).toEqual('Error: Expected random to be one of: typescript, csharp, golang, java, javascript, dart, python, rust, kotlin, php, cplusplus\nSee more help with --help\n'); expect(ctx.stdout).toEqual(''); done(); }); From b3b1c5fb90af15e4059f4ca3685a0b5f4efbfc11 Mon Sep 17 00:00:00 2001 From: Ashish Padhy <100484401+Shurtu-gal@users.noreply.github.com> Date: Tue, 23 May 2023 19:20:51 +0530 Subject: [PATCH 4/4] feat: update docs --- docs/usage.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/usage.md b/docs/usage.md index 523438e8758..2bc42daf1a8 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -6,11 +6,13 @@ weight: 40 The AsyncAPI CLI makes it easier to work with AsyncAPI documents. To get **help**, run this command in your terminal: + ```sh asyncapi --help ``` It should print something similar to this: + ```sh All in one CLI for all AsyncAPI tools @@ -39,5 +41,7 @@ COMMANDS dart generate the models for Dart rust generate the models for Rust kotlin generate the models for Kotlin - fromTemplate generate whatever you want using templates compatible with AsyncAPI Generator + php generate the models for PHP + cplusplus generate the models for C++ + fromTemplate generate whatever you want using templates compatible with AsyncAPI Generator ```