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 ``` diff --git a/src/commands/generate/models.ts b/src/commands/generate/models.ts index 8557d1cc1d4..b248f2f906e 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, CplusplusFileGenerator } 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, CplusplusFileGenerator } from '@asyncapi/modelina'; import { Flags } from '@oclif/core'; import Command from '../../base'; import { load } from '../../models/SpecificationFile'; @@ -16,6 +16,7 @@ enum Languages { python = 'python', rust = 'rust', kotlin='kotlin', + php='php', cplusplus='cplusplus' } const possibleLanguageValues = Object.values(Languages).join(', '); @@ -93,7 +94,7 @@ export default class Models extends Command { * C++ and C# specific namespace to use for the generated models */ namespace: Flags.string({ - description: 'C++ and C# specific, define the namespace to use for the generated models. This is required when language is `cplusplus` or `csharp`.', + description: 'C#, C++ and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp`,`c++` or `php`.', required: false }), @@ -240,6 +241,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 06d62af2f1d..a7c48e1471c 100644 --- a/test/commands/generate/models.test.ts +++ b/test/commands/generate/models.test.ts @@ -21,11 +21,10 @@ 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\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(); }); - test .stderr() .stdout() @@ -271,7 +270,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()