* Timestamp+KeepJSON options

git-svn-id: trunk@30832 -
This commit is contained in:
michael 2015-05-09 15:09:49 +00:00
parent 76ba3a1f90
commit 8982f5a22e
2 changed files with 50 additions and 21 deletions

View File

@ -38,27 +38,22 @@
<Unit0> <Unit0>
<Filename Value="googleapiconv.pp"/> <Filename Value="googleapiconv.pp"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="googleapiconv"/>
</Unit0> </Unit0>
<Unit1> <Unit1>
<Filename Value="../../src/googleadexchangebuyer.pp"/> <Filename Value="../../src/googleadexchangebuyer.pp"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="googleadexchangebuyer"/>
</Unit1> </Unit1>
<Unit2> <Unit2>
<Filename Value="../../src/googlebase.pp"/> <Filename Value="../../src/googlebase.pp"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="googlebase"/>
</Unit2> </Unit2>
<Unit3> <Unit3>
<Filename Value="../../src/googleclient.pp"/> <Filename Value="../../src/googleclient.pp"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="googleclient"/>
</Unit3> </Unit3>
<Unit4> <Unit4>
<Filename Value="../../src/googlediscoverytopas.pp"/> <Filename Value="../../src/googlediscoverytopas.pp"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="googlediscoverytopas"/>
</Unit4> </Unit4>
</Units> </Units>
</ProjectOptions> </ProjectOptions>

View File

@ -1,16 +1,23 @@
{$mode objfpc} {$mode objfpc}
{$h+} {$h+}
{ $define USESYNAPSE}
{ $DEFINE USESYNAPSE}
{$IFDEF VER2_6}
{$DEFINE USESYNAPSE}
{$ENDIF}
program googleapiconv; program googleapiconv;
uses uses
custapp, classes, sysutils, fpjson, jsonparser, googleadexchangebuyer, custapp, classes, sysutils, fpjson, jsonparser, fpwebclient,
googlebase, googleclient, googlediscoverytopas, fpwebclient,
{$IFDEF USESYNAPSE} {$IFDEF USESYNAPSE}
ssl_openssl, ssl_openssl,
synapsewebclient, synapsewebclient,
{$ELSE} {$ELSE}
fphttpwebclient; fphttpwebclient,
{$ENDIF}
googlediscoverytopas, googleservice, restbase, restcodegen;
Const Const
BaseDiscoveryURL = 'https://www.googleapis.com/discovery/v1/apis/'; BaseDiscoveryURL = 'https://www.googleapis.com/discovery/v1/apis/';
@ -47,6 +54,9 @@ Type
TGoogleAPIConverter = CLass(TCustomApplication) TGoogleAPIConverter = CLass(TCustomApplication)
private private
FKeepJSON: Boolean;
FVerbose: Boolean;
procedure ConversionLog(Sender: TObject; LogType: TCodegenLogType; const Msg: String);
procedure CreateFPMake(FileName: String; L: TAPIEntries); procedure CreateFPMake(FileName: String; L: TAPIEntries);
procedure DoAll(LocalFile, URL, OFN: String; AllVersions: Boolean); procedure DoAll(LocalFile, URL, OFN: String; AllVersions: Boolean);
Procedure DoConversion(JS: TStream; AEntry : TAPIEntry) ; Procedure DoConversion(JS: TStream; AEntry : TAPIEntry) ;
@ -59,6 +69,8 @@ Type
Constructor Create(AOwner: TComponent); override; Constructor Create(AOwner: TComponent); override;
Destructor Destroy; override; Destructor Destroy; override;
Procedure DoRun; override; Procedure DoRun; override;
Property KeepJSON : Boolean Read FKeepJSON Write FKeepJSON;
Property Verbose : Boolean Read FVerbose Write FVerbose;
end; end;
{ TAPIEntries } { TAPIEntries }
@ -139,8 +151,10 @@ begin
Writeln('-p --classprefix=prefix Prefix to use in class names for all classes.'); Writeln('-p --classprefix=prefix Prefix to use in class names for all classes.');
Writeln('-r --resourcesuffix=suffix Suffix to use for resource names. Default is Resource.'); Writeln('-r --resourcesuffix=suffix Suffix to use for resource names. Default is Resource.');
Writeln('-R --register=unit Register unit for Lazarus.'); Writeln('-R --register=unit Register unit for Lazarus.');
Writeln('-t --timestamp Add timestamp to generated unit.');
Writeln('-u --url=URL URL to download the REST description from.'); 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 --serviceversion=v Service version to download the REST description for.');
Writeln('-V --verbose Write some diagnostic messages');
Writeln('If the outputfilename is empty and cannot be determined, an error is returned'); Writeln('If the outputfilename is empty and cannot be determined, an error is returned');
Halt(Ord(Msg<>'')); Halt(Ord(Msg<>''));
end; end;
@ -242,6 +256,13 @@ begin
end; end;
end; end;
procedure TGoogleAPIConverter.ConversionLog(Sender: TObject;
LogType: TCodegenLogType; const Msg: String);
begin
if Verbose then
Writeln(StdErr,Msg);
end;
procedure TGoogleAPIConverter.CreateFPMake(FileName :String; L : TAPIEntries); procedure TGoogleAPIConverter.CreateFPMake(FileName :String; L : TAPIEntries);
Var Var
@ -320,6 +341,7 @@ Var
I : Integer; I : Integer;
begin begin
Writeln('DoAll(',LocalFile,',',URL,',', OFN ,',',AllVersions ,')');
E:=ExtractFileExt(OFN); E:=ExtractFileExt(OFN);
if (E='') then if (E='') then
E:='.pp'; E:='.pp';
@ -345,14 +367,14 @@ begin
try try
if not HttpGetJSON(RU,RS) then if not HttpGetJSON(RU,RS) then
Raise Exception.Create('Could not download rest description from URL: '+RU); Raise Exception.Create('Could not download rest description from URL: '+RU);
Writeln('Converting service "',O.get('name'),'" to unit: ',LFN); ConversionLog(Self,cltInfo,Format('Converting service "%s" to unit: %s',[O.get('name'),LFN]));
With TFIleStream.Create(ChangeFileExt(LFN,'.json'),fmCreate) do if KeepJSON then
try With TFIleStream.Create(ChangeFileExt(LFN,'.json'),fmCreate) do
CopyFrom(RS,0); try
finally CopyFrom(RS,0);
Free; finally
end; Free;
// Writeln('Downloaded : ',RS.DataString); end;
RS.Position:=0; RS.Position:=0;
U:=UL.AddEntry; U:=UL.AddEntry;
U.FileName:=LFN; U.FileName:=LFN;
@ -379,10 +401,10 @@ end;
procedure TGoogleAPIConverter.DoRun; procedure TGoogleAPIConverter.DoRun;
Const Const
MyO : Array[1..16] of ansistring MyO : Array[1..19] of ansistring
= ('help','input:','output:','extraunits:','baseclass:','classprefix:', = ('help','input:','output:','extraunits:','baseclass:','classprefix:',
'url:','service:','serviceversion:','resourcesuffix:','license:', 'url:','service:','serviceversion:','resourcesuffix:','license:',
'All','all','register','icon','fpmake:'); 'All','all','register','icon','fpmake:','timestamp','verbose','keepjson');
Var Var
O,NonOpts : TStrings; O,NonOpts : TStrings;
@ -398,7 +420,7 @@ begin
try try
O:=TStringList.Create; O:=TStringList.Create;
For S in MyO do O.Add(S); For S in MyO do O.Add(S);
S:=Checkoptions('hi:o:e:b:p:u:s:v:r:L:aAR:Im:',O,TStrings(Nil),NonOpts,True); S:=Checkoptions('hi:o:e:b:p:u:s:v:r:L:aAR:Im:tVk',O,TStrings(Nil),NonOpts,True);
if NonOpts.Count>0 then if NonOpts.Count>0 then
IFN:=NonOpts[0]; IFN:=NonOpts[0];
if NonOpts.Count>1 then if NonOpts.Count>1 then
@ -407,6 +429,8 @@ begin
O.Free; O.Free;
NonOpts.Free; NonOpts.Free;
end; end;
FVerbose:=HasOption('V','verbose');
FKeepJSON:=HasOption('k','keepjson');
if (S<>'') or HasOption('h','help') then if (S<>'') or HasOption('h','help') then
Usage(S); Usage(S);
DoAllServices:=HasOption('a','all') or HasOption('A','All'); DoAllServices:=HasOption('a','all') or HasOption('A','All');
@ -445,6 +469,13 @@ begin
JS:=TMemoryStream.Create; JS:=TMemoryStream.Create;
if not HttpGetJSON(URL,JS) then if not HttpGetJSON(URL,JS) then
Raise Exception.Create('Could not download from URL: '+URL); Raise Exception.Create('Could not download from URL: '+URL);
if KeepJSON then
With TFIleStream.Create(ChangeFileExt(OFN,'.json'),fmCreate) do
try
CopyFrom(JS,0);
finally
Free;
end;
JS.POsition:=0; JS.POsition:=0;
end end
else else
@ -478,7 +509,8 @@ begin
FN:=ExtractFilePath(APIEntry.FileName)+APIEntry.APIName+ExtractFileExt(APIEntry.APIIcon); FN:=ExtractFilePath(APIEntry.FileName)+APIEntry.APIName+ExtractFileExt(APIEntry.APIIcon);
FS:=TFileStream.Create(FN,fmCreate); FS:=TFileStream.Create(FN,fmCreate);
try try
Writeln(Format('Downloading icon %s to %s',[APIEntry.APIIcon,FN])); if HasOption('V','verbose') then
Writeln(Format('Downloading icon %s to %s',[APIEntry.APIIcon,FN]));
HttpGetJSON(APIEntry.APIIcon,FS); HttpGetJSON(APIEntry.APIIcon,FS);
finally finally
FS.Free; FS.Free;
@ -506,6 +538,7 @@ begin
LicenseText.LoadFromFile(L); LicenseText.LoadFromFile(L);
end; end;
end; end;
OnLog:=@ConversionLog;
ExtraUnits:=GetOptionValue('e','extraunits'); ExtraUnits:=GetOptionValue('e','extraunits');
if HasOption('b','baseclass') then if HasOption('b','baseclass') then
BaseClassName:=GetOptionValue('b','baseclass'); BaseClassName:=GetOptionValue('b','baseclass');
@ -513,6 +546,7 @@ begin
ClassPrefix:=GetOptionValue('p','classprefix'); ClassPrefix:=GetOptionValue('p','classprefix');
if HasOption('r','resourcesuffix') then if HasOption('r','resourcesuffix') then
ResourceSuffix:=GetOptionValue('r','resourcesuffix'); ResourceSuffix:=GetOptionValue('r','resourcesuffix');
AddTimeStamp:=HasOption('t','timestamp');
LoadFromStream(JS); LoadFromStream(JS);
AEntry.APIUnitName:=ChangeFileExt(ExtractFileName(AEntry.FileName),''); AEntry.APIUnitName:=ChangeFileExt(ExtractFileName(AEntry.FileName),'');
AEntry.APIName:=APIClassName; AEntry.APIName:=APIClassName;