openapi2pas.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  4. Open API to pascal code generator
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program openapi2pas;
  12. {$mode objfpc}{$H+}
  13. uses
  14. Classes,
  15. SysUtils,
  16. CustApp,
  17. fpopenapi.objects,
  18. fpopenapi.reader,
  19. fpopenapi.codegen;
  20. type
  21. { TGenDTOApplication }
  22. TGenDTOApplication = class(TCustomApplication)
  23. private
  24. FQuiet : Boolean;
  25. FCodeGen : TOpenAPICodeGen;
  26. FServiceMapFile,
  27. FUUIDMapFile : String;
  28. protected
  29. Procedure DoLog(EventType : TEventType; const Msg : String); override;
  30. procedure DoRun; override;
  31. procedure ReadOpenAPi(const aInputFile: string; aApi: TOpenAPI);
  32. procedure WriteAPI(aApi: TOpenAPI; const aOutputFile: String);
  33. procedure WriteConfig(const aFileName: string);
  34. public
  35. constructor Create(TheOwner: TComponent); override;
  36. destructor Destroy; override;
  37. procedure Usage(const aMessage : string); virtual;
  38. end;
  39. { TGenDTOApplication }
  40. procedure TGenDTOApplication.WriteAPI(aApi: TOpenAPI; const aOutputFile : String) ;
  41. begin
  42. FCodeGen.OnLog:=@DoLog;
  43. FCodeGen.BaseOutputFileName:=aOutputFile;
  44. FCodeGen.API:=aAPI;
  45. if (FUUIDMapFile<>'') and FileExists(FUUIDMapFile) then
  46. FCodeGen.UUIDMap.LoadFromFile(FUUIDMapFile);
  47. if (FServiceMapFile<>'') then
  48. FCodeGen.ServiceMap.LoadFromFile(FServiceMapFile);
  49. FCodeGen.Execute;
  50. if FUUIDMapFile<>'' then
  51. FCodeGen.UUIDMap.SavetoFile(FUUIDMapFile);
  52. end;
  53. procedure TGenDTOApplication.ReadOpenAPi(const aInputFile : string; aApi: TOpenAPI);
  54. var
  55. lReader : TOpenAPIReader;
  56. begin
  57. lReader:=TOpenAPIReader.Create(Self);
  58. try
  59. lReader.ReadFromFile(aAPI,aInputFile);
  60. finally
  61. lReader.Free;
  62. end;
  63. end;
  64. procedure TGenDTOApplication.DoLog(EventType: TEventType; const Msg: String);
  65. begin
  66. if FQuiet then
  67. exit;
  68. Writeln(EventType,' : ',Msg);
  69. end;
  70. procedure TGenDTOApplication.WriteConfig(const aFileName : string);
  71. var
  72. lDir : String;
  73. begin
  74. lDir:=ExtractFilePath(aFileName);
  75. if lDir<>'' then
  76. If not ForceDirectories(lDir) then
  77. begin
  78. Writeln(StdErr,'Failed to create directory ',lDir);
  79. Exit;
  80. end;
  81. Log(etInfo,'Writing config file to %s',[aFileName]);
  82. FCodegen.SaveConfig(aFileName);
  83. end;
  84. procedure TGenDTOApplication.DoRun;
  85. const
  86. shortOpts = 'hi:o:dequ:s:varcC:bnw:';
  87. LongOpts : Array of string = ('help','input:','output:','delphi','uuid-map:','quiet','service-map','verbose-header','enumerated','async','server','client','config:','abstract','no-implementation','write-config:');
  88. var
  89. lAPI : TOpenAPI;
  90. lConfig, lOutputFile,lInputFile, ErrorMsg : String;
  91. begin
  92. Terminate;
  93. ErrorMsg:=CheckOptions(ShortOPts,LongOpts);
  94. if (ErrorMsg<>'') or HasOption('h','help') then
  95. begin
  96. Usage(ErrorMsg);
  97. Exit;
  98. end;
  99. lConfig:=GetOptionValue('C','config');
  100. if (lConfig<>'') then
  101. FCodeGen.LoadConfig(lConfig);
  102. FCodeGen.DelphiCode:=HasOption('d','delphi');
  103. FCodeGen.VerboseHeader:=HasOption('v','verbose-header');
  104. FCodeGen.UseEnums:=HasOption('e','enumerated');
  105. FCodeGen.AsyncService:=HasOption('a','async');
  106. lInputFile:=GetOptionValue('i','input');
  107. lOutputFile:=GetOptionValue('o','output');
  108. FUUIDMapFile:=GetOptionValue('u','uuid-map');
  109. FServiceMapFile:=GetOptionValue('s','service-map');
  110. FCodeGen.GenerateServer:=HasOption('r','server');
  111. FCodeGen.AbstractServiceCalls:=HasOption('b','abstract');
  112. FCodeGen.SkipServerServiceImplementationModule:=HasOption('n','no-implementation');
  113. FQuiet:=HasOption('q','quiet');
  114. if HasOption('w','write-config') then
  115. WriteConfig(GetOptionValue('w','write-config'))
  116. else
  117. begin
  118. if lOutputFile='' then
  119. lOutputFile:=ChangeFileExt(lInputFile,'');
  120. lAPI:=TOpenAPI.Create;
  121. try
  122. ReadOpenAPi(lInputFile,lAPI);
  123. WriteApi(lApi,lOutputFile);
  124. finally
  125. lApi.Free;
  126. end;
  127. end;
  128. end;
  129. constructor TGenDTOApplication.Create(TheOwner: TComponent);
  130. begin
  131. inherited Create(TheOwner);
  132. ExceptionExitCode:=1;
  133. StopOnException:=True;
  134. FCodeGen:=TOpenAPICodeGen.Create(Self);
  135. end;
  136. destructor TGenDTOApplication.Destroy;
  137. begin
  138. FreeAndNil(FCodeGen);
  139. inherited Destroy;
  140. end;
  141. procedure TGenDTOApplication.Usage(const aMessage: string);
  142. begin
  143. writeln('Usage: ', ExeName, '[options]');
  144. Writeln('Where options is one or more of:');
  145. Writeln('-a --async Generate asynchronous service calls.');
  146. Writeln('-b --abstract Split server in abstract handler and implementation modules (and units).');
  147. Writeln('-c --client Generate client-side service.');
  148. Writeln('-C --config=FILE Read config file with converter settings.');
  149. Writeln('-d --delphi Generate delphi code for DTO/Serializer/Service definitions.');
  150. Writeln('-e --enumerated Use enumerateds (default is to keep strings).');
  151. Writeln('-h --help This message.');
  152. Writeln('-i --input=FILE OpenAPI JSON File to use.');
  153. Writeln('-n --no-implementation Skip generation of server service module (only useful when -b is used).');
  154. Writeln('-o --output=FILE Base filename for output.');
  155. Writeln('-q --quiet Be less verbose.');
  156. Writeln('-s --service-map=FILE Read service and method name mapping from file.');
  157. Writeln('-u --uuid-map=FILE Read (and write) a file with UUIDs for interfaces.');
  158. Writeln('-v --verbose-header Add OpenAPI description to unit header.');
  159. Writeln('-w --write-config=FILE Write a configuration file with current settings and exit.');
  160. ExitCode:=Ord(aMessage<>'');
  161. end;
  162. var
  163. Application: TGenDTOApplication;
  164. begin
  165. Application:=TGenDTOApplication.Create(nil);
  166. Application.Title:='Generate DTO Application';
  167. Application.Run;
  168. Application.Free;
  169. end.