* Improved code generator to take care of some corner cases

git-svn-id: trunk@33774 -
This commit is contained in:
michael 2016-05-24 08:32:10 +00:00
parent 345361beef
commit 2121a3ae39

View File

@ -54,7 +54,9 @@ Type
TGoogleAPIConverter = CLass(TCustomApplication)
private
FDownloadOnly: Boolean;
FKeepJSON: Boolean;
FUnitPrefix: String;
FVerbose: Boolean;
procedure ConversionLog(Sender: TObject; LogType: TCodegenLogType; const Msg: String);
procedure CreateFPMake(FileName: String; L: TAPIEntries);
@ -71,6 +73,8 @@ Type
Procedure DoRun; override;
Property KeepJSON : Boolean Read FKeepJSON Write FKeepJSON;
Property Verbose : Boolean Read FVerbose Write FVerbose;
Property DownloadOnly : Boolean Read FDownloadOnly Write FDownloadOnly;
Property UnitPrefix : String Read FUnitPrefix Write FUnitPrefix;
end;
{ TAPIEntries }
@ -85,19 +89,21 @@ begin
Result:=Add as TAPIEntry;
end;
constructor TGoogleAPIConverter.Create(AOwner: TComponent);
Constructor TGoogleAPIConverter.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
StopOnException:=True;
TDiscoveryJSONToPas.RegisterAllObjects;
UnitPrefix:='google';
end;
destructor TGoogleAPIConverter.Destroy;
Destructor TGoogleAPIConverter.Destroy;
begin
inherited Destroy;
end;
function TGoogleAPIConverter.HttpGetJSON(const URL: String; Response: TStream): Boolean;
Function TGoogleAPIConverter.HttpGetJSON(Const URL: String; Response: TStream
): Boolean;
Var
Webclient : TAbstractWebClient;
@ -116,6 +122,7 @@ begin
try
Req:=WebClient.CreateRequest;
Req.ResponseContent:=Response;
ConversionLog(Self,cltInfo,'Downloading: '+URL);
Resp:=WebClient.ExecuteRequest('GET',URL,Req);
Result:=(Resp<>Nil);
finally
@ -155,6 +162,10 @@ begin
Writeln('-u --url=URL URL to download the REST description from.');
Writeln('-v --serviceversion=v Service version to download the REST description for.');
Writeln('-V --verbose Write some diagnostic messages');
Writeln('-k --keepjson Keep the downloaded JSON files');
Writeln('-d --onlydownload Just download the files, do not actually convert.');
Writeln(' Only effective if -k or --keepjson is also specified.');
Writeln('-f --unitprefix Prefix for generated unit names. Default is "google"');
Writeln('If the outputfilename is empty and cannot be determined, an error is returned');
Halt(Ord(Msg<>''));
end;
@ -355,7 +366,7 @@ begin
if AllVersions or O.Get('preferred',false) then
begin
RU:=O.get('discoveryRestUrl');
LFN:=O.get('name');
LFN:=UnitPrefix+O.get('name');
if AllVersions then
LFN:=LFN+'_'+StringReplace(O.get('version'),'.','',[rfReplaceAll]);
if (OFN='') then
@ -377,33 +388,37 @@ begin
RS.Position:=0;
U:=UL.AddEntry;
U.FileName:=LFN;
DoConversion(RS,U);
if not DownloadOnly then
DoConversion(RS,U);
finally
RS.Free;
end;
end;
end;
if HasOption('R','register') then
RegisterUnit(GetOptionValue('R','register'),UL);
if HasOption('m','fpmake') then
CreateFpMake(GetOptionValue('m','fpmake'),UL);
if not DownloadOnly then
begin
if HasOption('R','register') then
RegisterUnit(GetOptionValue('R','register'),UL);
if HasOption('m','fpmake') then
CreateFpMake(GetOptionValue('m','fpmake'),UL);
end;
if HasOption('I','icon') then
For I:=0 to UL.Count-1 do
DownloadIcon(UL[i]);
finally
UL.Free;
D.Free;
end;
end;
procedure TGoogleAPIConverter.DoRun;
Procedure TGoogleAPIConverter.DoRun;
Const
MyO : Array[1..19] of ansistring
MyO : Array[1..21] of ansistring
= ('help','input:','output:','extraunits:','baseclass:','classprefix:',
'url:','service:','serviceversion:','resourcesuffix:','license:',
'All','all','register','icon','fpmake:','timestamp','verbose','keepjson');
'All','all','register','icon','fpmake:','timestamp','verbose','keepjson',
'onlydownload','unitprefix');
Var
O,NonOpts : TStrings;
@ -419,7 +434,7 @@ begin
try
O:=TStringList.Create;
For S in MyO do O.Add(S);
S:=Checkoptions('hi:o:e:b:p:u:s:v:r:L:aAR:Im:tVk',O,TStrings(Nil),NonOpts,True);
S:=Checkoptions('hi:o:e:b:p:u:s:v:r:L:aAR:Im:tVkdf',O,TStrings(Nil),NonOpts,True);
if NonOpts.Count>0 then
IFN:=NonOpts[0];
if NonOpts.Count>1 then
@ -430,6 +445,10 @@ begin
end;
FVerbose:=HasOption('V','verbose');
FKeepJSON:=HasOption('k','keepjson');
if HasOption('f','unitprefix') then
UnitPrefix:=GetOptionValue('f','unitprefix');
If FKeepJSON Then
FDownLoadOnly:=HasOption('d','onlydownload');
if (S<>'') or HasOption('h','help') then
Usage(S);
DoAllServices:=HasOption('a','all') or HasOption('A','All');
@ -455,7 +474,7 @@ begin
if (IFN<>'') then
OFN:=ChangeFileExt(IFN,'.pp')
else if getOptionValue('s','service')<>'' then
OFN:='google'+getOptionValue('s','service')+'.pp';
OFN:=UnitPrefix+getOptionValue('s','service')+'.pp';
if (OFN='') and Not DoAllServices then
Usage('Need an output filename');
if DoAllServices then
@ -480,15 +499,16 @@ begin
else
JS:=TFileStream.Create(IFN,fmOpenRead or fmShareDenyWrite);
try
APIEntry:=TAPIEntry.Create(Nil);
try
APIEntry.FileName:=OFN;
DoConversion(JS,APIEntry);
if HasOption('I','icon') then
DownloadIcon(APIEntry);
finally
APIEntry.Free;
end;
if not DownLoadOnly then
APIEntry:=TAPIEntry.Create(Nil);
try
APIEntry.FileName:=OFN;
DoConversion(JS,APIEntry);
if HasOption('I','icon') then
DownloadIcon(APIEntry);
finally
APIEntry.Free;
end;
finally
JS.Free;
end;
@ -517,7 +537,7 @@ begin
end;
end;
procedure TGoogleAPIConverter.DoConversion(JS: TStream; AEntry: TAPIEntry);
Procedure TGoogleAPIConverter.DoConversion(JS: TStream; AEntry: TAPIEntry);
Var
L: String;