Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Modelina configuration file support #430

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import Command from '../../base';
import path from 'path';
import { load } from '../../models/SpecificationFile';
import { parse } from '../../utils/parser';
enum Languages {
Expand Down Expand Up @@ -33,6 +34,11 @@ export default class Models extends Command {
description: 'The output directory where the models should be written to. Omitting this flag will write the models to `stdout`.',
required: false
}),
configFile: Flags.string({
char: 'c',
description: 'Path to config file',
required: false
}),
/**
* TypeScript specific options
*/
Expand Down Expand Up @@ -74,6 +80,7 @@ export default class Models extends Command {
description: 'C# specific, define the namespace to use for the generated models. This is required when language is `csharp`.',
required: false
}),

};

async run() {
Expand All @@ -83,6 +90,14 @@ export default class Models extends Command {

const inputFile = await load(file) || await load();
const parsedInput = await parse(inputFile.text());
let defaultConfig = {};
if(flags.configFile !== undefined) {
try {
defaultConfig = await import(path.resolve(process.cwd(), flags.configFile));
} catch(e: any) {
this.error(`There was an error trying to load the Modelina configuration file, it will be ignored. The following error was triggered: ${e}`);
}
}
Logger.setLogger({
info: (message) => {
this.log(message);
Expand All @@ -102,25 +117,28 @@ export default class Models extends Command {
switch (language) {
case Languages.typescript:
fileGenerator = new TypeScriptFileGenerator({
modelType: tsModelType as undefined | 'class' | 'interface',
enumType: tsEnumType as undefined | 'enum' | 'union'
...defaultConfig,
...{
modelType: tsModelType as undefined | 'class' | 'interface',
enumType: tsEnumType as undefined | 'enum' | 'union'
}
});
fileOptions = {
moduleSystem: tsModuleSystem,
exportType: tsExportType
};
break;
case Languages.python:
fileGenerator = new PythonFileGenerator();
fileGenerator = new PythonFileGenerator(defaultConfig);
break;
case Languages.rust:
fileGenerator = new RustFileGenerator();
fileGenerator = new RustFileGenerator(defaultConfig);
break;
case Languages.csharp:
if (namespace === undefined) {
throw new Error('In order to generate models to C#, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.');
}
fileGenerator = new CSharpFileGenerator();
fileGenerator = new CSharpFileGenerator(defaultConfig);
fileOptions = {
namespace
};
Expand All @@ -129,7 +147,7 @@ export default class Models extends Command {
if (packageName === undefined) {
throw new Error('In order to generate models to Go, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new GoFileGenerator();
fileGenerator = new GoFileGenerator(defaultConfig);
fileOptions = {
packageName
};
Expand All @@ -138,19 +156,19 @@ export default class Models extends Command {
if (packageName === undefined) {
throw new Error('In order to generate models to Java, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new JavaFileGenerator();
fileGenerator = new JavaFileGenerator(defaultConfig);
fileOptions = {
packageName
};
break;
case Languages.javascript:
fileGenerator = new JavaScriptFileGenerator();
fileGenerator = new JavaScriptFileGenerator(defaultConfig);
break;
case Languages.dart:
if (packageName === undefined) {
throw new Error('In order to generate models to Dart, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}
fileGenerator = new DartFileGenerator();
fileGenerator = new DartFileGenerator(defaultConfig);
fileOptions = {
packageName
};
Expand Down
68 changes: 65 additions & 3 deletions test/commands/generate/__snapshots__/models.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,72 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`models for C# should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models for Dart should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models for Go should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models for Java should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models for JavaScript should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models for Python should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models for Rust should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models for TypeScript should handle configuration file 1`] = `
"Successfully generated the following models: CustomUserSignedUpPayload
"
`;

exports[`models should handle incorrect configuration file correctly 1`] = `
"Error: There was an error trying to load the Modelina configuration file, it will be ignored. The following error was triggered: Error:
× Expected ';', '}' or <eof>
╭─[/Users/lagoni/Documents/github/cli/test/commands/generate/models_configuration/incorrect_models.js:12:1]
12 │ WRONG SYNTAX
· ──────
╰────

Error:
☞ This is the expression part of an expression statement
╭─[/Users/lagoni/Documents/github/cli/test/commands/generate/models_configuration/incorrect_models.js:12:1]
12 │ WRONG SYNTAX
· ─────
╰────


Caused by:
0: failed to process input file
1: Syntax Error
"
`;

exports[`models works when generating in memory 1`] = `
"Successfully generated the following models:
## Model name: AnonymousSchema_1
## Model name: UserSignedUpPayload

class AnonymousSchema_1 {
class UserSignedUpPayload {
private _displayName?: string;
private _email?: string;
private _additionalProperties?: Map<string, any>;
Expand All @@ -28,7 +90,7 @@ class AnonymousSchema_1 {
get additionalProperties(): Map<string, any> | undefined { return this._additionalProperties; }
set additionalProperties(additionalProperties: Map<string, any> | undefined) { this._additionalProperties = additionalProperties; }
}
export default AnonymousSchema_1;
export default UserSignedUpPayload;


"
Expand Down
Loading