forked from synopse/mORMot2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmopenapi.dpr
More file actions
111 lines (101 loc) · 3.26 KB
/
mopenapi.dpr
File metadata and controls
111 lines (101 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/// Command Line OpenAPI/Swagger Client Code Generation Tool
// - this program is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
program mopenapi;
{
*****************************************************************************
Command-Line Tool to Generate client .pas from OpenAPI/Swagger .json specs
- e.g. ./mopenapi --help
mopenapi swagger.json PetStore
mopenapi OpenApiAuth.json /concise
./mopenapi test.json --options=DtoNoExample,DtoNoPattern
*****************************************************************************
}
{$I mormot.defines.inc}
{$ifdef OSWINDOWS}
{$apptype console}
{$R ..\..\mormot.win.default.manifest.res}
{$endif OSWINDOWS}
{$R *.res}
uses
{$I mormot.uses.inc}
classes,
sysutils,
mormot.core.base,
mormot.core.os,
mormot.core.text,
mormot.core.unicode,
mormot.core.rtti,
mormot.net.openapi;
type
// use RTTI for command line options
TOptions = class(TSynPersistent)
protected
fDestinationFolder: TFileName;
fOptions: TOpenApiParserOptions;
published
property DestinationFolder: TFileName
read fDestinationFolder write fDestinationFolder;
property Options: TOpenApiParserOptions
read fOptions write fOptions;
end;
var
c: TExecutableCommandLine;
o: TOptions;
oa: TOpenApiParser;
name: RawUtf8;
source: TFileName;
i: PtrInt;
begin
o := TOptions.Create;
try
// validate command line parameters
c := Executable.Command;
source := c.ArgFile(0, 'Source json #filename', {optional=}false);
name := TrimControlChars(c.ArgU(1, 'Short description #name of the service'));
SetObjectFromExecutableCommandLine(o, '', '');
if o.DestinationFolder <> '' then
o.DestinationFolder := c.CheckFileName(o.DestinationFolder, {folder=}true);
if c.Option('&concise', 'Generate a single API unit') then
o.Options := o.Options + OPENAPI_CONCISE;
if c.ConsoleHelpFailed('mORMot ' + SYNOPSE_FRAMEWORK_VERSION +
' OpenAPI/Swagger Code Generator') then
begin
ExitCode := 1;
exit;
end;
if name = '' then
begin
// if no short description name is supplied, extract from specs file name
name := GetFileNameWithoutExtOrPath(source);
for i := length(name) downto 1 do
if name[i] in ['A' .. 'Z'] then
begin
delete(name, 1, i - 1); // 'OpenApiTest' -> 'Test'
break;
end;
end;
try
// do the actual specs parsing and code generation
oa := TOpenApiParser.Create(name, o.Options);
try
oa.ParseFile(source);
oa.ExportToDirectory(o.DestinationFolder);
if not (opoGenerateSingleApiUnit in oa.Options) then
ConsoleWrite(['Generated ',oa.DtoUnitName, '.pas']);
ConsoleWrite(['Generated ', oa.ClientUnitName, '.pas for ',
oa.ClientClassName]);
finally
oa.Free;
end;
except
on E: Exception do
begin
ConsoleShowFatalException(E, {waitforenterkey=}false);
ExitCode := 2; // interrupted
end;
end;
finally
o.Free;
end;
end.