mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-07 06:28:55 +02:00
* Extra options for more flexibility
This commit is contained in:
parent
1cd8c249b1
commit
dea81f4f60
@ -21,12 +21,13 @@ unit cgutils;
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, pascodegen, tstopas;
|
||||
Classes, SysUtils, pascodegen, tstopas, strutils;
|
||||
|
||||
Type
|
||||
TSettings = record
|
||||
basedir : string;
|
||||
cachefile : string;
|
||||
rawcachefile : string;
|
||||
end;
|
||||
|
||||
{ TLoggingConverter }
|
||||
@ -41,7 +42,7 @@ Type
|
||||
end;
|
||||
|
||||
Procedure GetDeclarationFileNames(const BaseDir,aDir : String; aList: TStrings);
|
||||
Procedure ConvertFile(Const BaseDir,aFileName,aUnitName : String; aOptions : TConversionOptions; aPascal,aLog : TStrings);
|
||||
Procedure ConvertFile(Const BaseDir,aFileName,aUnitName,aliases,extraunits : String; Skipweb : Boolean; aOptions : TConversionOptions; aPascal,aLog : TStrings);
|
||||
Function GetOutputUnitName(Const aFileName,aUnitName : String) : string;
|
||||
Function GetInputFileName(Const BaseDir,aFileName : String) : string;
|
||||
Function GetSettings : TSettings;
|
||||
@ -50,21 +51,32 @@ implementation
|
||||
|
||||
uses inifiles;
|
||||
|
||||
Function GetSettings : TSettings;
|
||||
function GetSettings: TSettings;
|
||||
|
||||
begin
|
||||
Result.BaseDir:=ExtractFilePath(ParamStr(0));
|
||||
Result.CacheFile:=GetTempDir(True)+'definitelytypedcache.lst';
|
||||
Result.RawCacheFile:=GetTempDir(True)+'definitelytypedrawcache.lst';
|
||||
With TIniFile.Create(GetAppConfigFile(True)) do
|
||||
try
|
||||
Result.BaseDir:=ReadString('Settings','BaseDir',Result.BaseDir);
|
||||
Result.cachefile:=ReadString('Settings','CacheDir',Result.CacheFile);
|
||||
Result.rawcachefile:=ReadString('Settings','RawCacheFile',Result.RawCacheFile);
|
||||
finally
|
||||
Free;
|
||||
end;
|
||||
With TStringList.Create do
|
||||
try
|
||||
Add(Result.BaseDir);
|
||||
Add(Result.cachefile);
|
||||
Add(Result.rawcachefile);
|
||||
SaveToFile('/tmp/settings.txt');
|
||||
finally
|
||||
Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
Procedure GetDeclarationFileNames(Const BaseDir,aDir : String; aList: TStrings);
|
||||
procedure GetDeclarationFileNames(const BaseDir, aDir: String; aList: TStrings);
|
||||
|
||||
Var
|
||||
Info : TSearchRec;
|
||||
@ -97,7 +109,7 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
Function GetInputFileName(Const BaseDir,aFileName : String) : string;
|
||||
function GetInputFileName(const BaseDir, aFileName: String): string;
|
||||
|
||||
Var
|
||||
BD,FN : String;
|
||||
@ -119,7 +131,7 @@ begin
|
||||
Result:=FN;
|
||||
end;
|
||||
|
||||
Function GetOutputUnitName(Const aFileName,aUnitName : String) : string;
|
||||
function GetOutputUnitName(const aFileName, aUnitName: String): string;
|
||||
|
||||
Var
|
||||
UN : String;
|
||||
@ -180,11 +192,12 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
Procedure ConvertFile(Const BaseDir,aFileName,aUnitName : String; aOptions : TConversionOptions; aPascal,aLog : TStrings);
|
||||
procedure ConvertFile(const BaseDir, aFileName, aUnitName, aliases, extraunits: String; Skipweb: Boolean;
|
||||
aOptions: TConversionOptions; aPascal, aLog: TStrings);
|
||||
|
||||
Var
|
||||
L : TLoggingConverter;
|
||||
UN,Fn : String;
|
||||
S,UN,Fn : String;
|
||||
|
||||
begin
|
||||
FN:=GetInputFileName(BaseDir,aFileName);
|
||||
@ -195,8 +208,12 @@ begin
|
||||
L.InputFileName:=FN;
|
||||
L.OutputUnitName:=UN;
|
||||
L.Logs:=aLog;
|
||||
L.ExtraUnits:=extraunits;
|
||||
for S in SplitString(Aliases,', ') do
|
||||
L.TypeAliases.Add(S);
|
||||
AddJSAliases(L.TypeAliases);
|
||||
AddWebAliases(L.TypeAliases);
|
||||
if not SkipWeb then
|
||||
AddWebAliases(L.TypeAliases);
|
||||
L.Execute;
|
||||
aPascal.Assign(L.Source);
|
||||
finally
|
||||
|
@ -22,7 +22,17 @@ uses
|
||||
{$IFDEF USEHTTPAPP} fphttpapp{$ELSE} fpcgi {$ENDIF},
|
||||
httpdefs, httproute;
|
||||
|
||||
Procedure CreateJSONFileList(aDir : String; aFileName : string);
|
||||
function GetBoolVal(R : TRequest; aName : String) : boolean;
|
||||
|
||||
Var
|
||||
S : String;
|
||||
|
||||
begin
|
||||
S:=R.QueryFields.Values[aName];
|
||||
Result:=(S='1') or (S='true') or (S='t');
|
||||
end;
|
||||
|
||||
Procedure CreateJSONFileList(aDir : String; aFileName : string; aTextFileName : String = '' );
|
||||
|
||||
Var
|
||||
L,O : TStrings;
|
||||
@ -37,6 +47,8 @@ begin
|
||||
O:=TstringList.Create;
|
||||
GetDeclarationFileNames(aDir,aDir,L);
|
||||
TstringList(l).Sort;
|
||||
if aTextFileName<>'' then
|
||||
L.SaveToFile(aTextFileName);
|
||||
O.Add('var dtsfiles = [');
|
||||
for I:=0 to L.Count-1 do
|
||||
begin
|
||||
@ -69,7 +81,7 @@ begin
|
||||
FN:=ExtractRelativePath(S.BaseDir,aFilename)
|
||||
else
|
||||
FN:=aFileName;
|
||||
cgUtils.ConvertFile(S.BaseDir,FN,'',[],aPas,Nil);
|
||||
cgUtils.ConvertFile(S.BaseDir,FN,'','','',False,[],aPas,Nil);
|
||||
for aLine in aPas do
|
||||
writeln(aLine);
|
||||
Finally
|
||||
@ -81,17 +93,25 @@ procedure DoList(ARequest: TRequest; AResponse: TResponse);
|
||||
Var
|
||||
S : TSettings;
|
||||
aList : TStrings;
|
||||
isRaw : Boolean;
|
||||
|
||||
begin
|
||||
S:=GetSettings;
|
||||
aList:=TstringList.Create;
|
||||
try
|
||||
if Not FileExists(S.cachefile) then
|
||||
CreateJSONFileList(S.BaseDir,S.cachefile);
|
||||
aList.LoadFromFile(S.cachefile);
|
||||
IsRaw:=GetBoolVal(aRequest,'raw');
|
||||
if Not (FileExists(S.cachefile) and FileExists(S.rawcachefile)) then
|
||||
CreateJSONFileList(S.BaseDir,S.cachefile,S.rawcachefile);
|
||||
if isRaw then
|
||||
aList.LoadFromFile(S.rawcachefile)
|
||||
else
|
||||
aList.LoadFromFile(S.cachefile);
|
||||
aResponse.Content:=aList.text;
|
||||
aResponse.ContentLength:=Length(aResponse.Content);
|
||||
aResponse.ContentType:='application/javascript';
|
||||
if IsRaw then
|
||||
aResponse.ContentType:='text/text'
|
||||
else
|
||||
aResponse.ContentType:='application/javascript';
|
||||
aResponse.SendResponse;
|
||||
finally
|
||||
aList.Free;
|
||||
@ -102,15 +122,14 @@ function GetRequestOptions(ARequest: TRequest) : TConversionOptions;
|
||||
|
||||
Var
|
||||
T : TConversionOption;
|
||||
S,N : String;
|
||||
N : String;
|
||||
|
||||
begin
|
||||
Result:=[];
|
||||
For T in TConversionOption do
|
||||
begin
|
||||
N:=GetEnumName(TypeInfo(TConversionOption),Ord(T));
|
||||
S:=aRequest.QueryFields.Values[N];
|
||||
if (S='1') or (S='true') then
|
||||
if GetBoolVal(aRequest,N) then
|
||||
Include(Result,T);
|
||||
end;
|
||||
end;
|
||||
@ -120,8 +139,9 @@ procedure DoConvertFile(ARequest: TRequest; AResponse: TResponse);
|
||||
Var
|
||||
S : TSettings;
|
||||
aPas,aLog : TStrings;
|
||||
aFileName,aUnitName,aOutput : string;
|
||||
aliases,aExtraUnits,aFileName,aUnitName,aOutput : string;
|
||||
Opts : TConversionOptions;
|
||||
skipweb : boolean;
|
||||
|
||||
begin
|
||||
S:=GetSettings;
|
||||
@ -131,9 +151,12 @@ begin
|
||||
Opts:=GetRequestOptions(aRequest);
|
||||
aFileName:=aRequest.QueryFields.Values['file'];
|
||||
aUnitName:=aRequest.QueryFields.Values['unit'];
|
||||
if aRequest.QueryFields.Values['prependlog']='1' then
|
||||
aExtraUnits:=aRequest.QueryFields.Values['extraunits'];
|
||||
aliases:=aRequest.QueryFields.Values['aliases'];
|
||||
skipweb:=GetBoolVal(aRequest,'skipweb');
|
||||
if GetBoolVal(aRequest,'prependlog') then
|
||||
aLog:=TStringList.Create;
|
||||
cgUtils.ConvertFile(S.BaseDir,aFileName,aUnitName,Opts,aPas,aLog);
|
||||
cgUtils.ConvertFile(S.BaseDir,aFileName,aUnitName,aliases,aExtraUnits,skipweb,Opts,aPas,aLog);
|
||||
if Assigned(aLog) then
|
||||
aOutput:='(* // Conversion log:'+sLineBreak+aLog.Text+sLineBreak+'*)'+sLineBreak
|
||||
else
|
||||
@ -154,12 +177,15 @@ begin
|
||||
begin
|
||||
if ParamCount=2 then
|
||||
CreateJSONFileList(Paramstr(1),ParamStr(2))
|
||||
else if ParamCount=3 then
|
||||
CreateJSONFileList(Paramstr(1),ParamStr(2),ParamStr(3))
|
||||
else if ParamCount=1 then
|
||||
ConvertFile(Paramstr(1));
|
||||
end
|
||||
else
|
||||
begin
|
||||
HTTPRouter.RegisterRoute('list',rmGet,@DoList);
|
||||
|
||||
HTTPRouter.RegisterRoute('convert',rmAll,@DoConvertFile);
|
||||
{$IFDEF USEHTTPAPP}
|
||||
Application.Port:=8080;
|
||||
|
Loading…
Reference in New Issue
Block a user