convcgi.lpr 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2022 by the Free Pascal development team
  4. Original author: Michael van Canneyt
  5. CGI TypeScript definitelytyped to pas2js code generator app
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program convcgi;
  13. { $DEFINE USEHTTPAPP}
  14. uses
  15. typinfo, sysutils, classes, cgutils, tstopas,
  16. {$IFDEF USEHTTPAPP} fphttpapp{$ELSE} fpcgi {$ENDIF},
  17. httpdefs, httproute;
  18. function GetBoolVal(R : TRequest; aName : String) : boolean;
  19. Var
  20. S : String;
  21. begin
  22. S:=R.QueryFields.Values[aName];
  23. Result:=(S='1') or (S='true') or (S='t');
  24. end;
  25. Procedure CreateJSONFileList(aDir : String; aFileName : string; aTextFileName : String = '' );
  26. Var
  27. L,O : TStrings;
  28. I : integer;
  29. S : String;
  30. begin
  31. O:=Nil;
  32. L:=TStringList.Create;
  33. try
  34. O:=TstringList.Create;
  35. GetDeclarationFileNames(aDir,aDir,L);
  36. TstringList(l).Sort;
  37. if aTextFileName<>'' then
  38. L.SaveToFile(aTextFileName);
  39. O.Add('var dtsfiles = [');
  40. for I:=0 to L.Count-1 do
  41. begin
  42. S:=L[i];
  43. S:=''''+StringReplace(S,'''','''''',[rfReplaceAll])+'''';
  44. if I<L.Count-1 then
  45. S:=S+',';
  46. O.Add(' '+S);
  47. end;
  48. O.Add(' ];');
  49. O.SaveToFile(aFileName);
  50. finally
  51. O.Free;
  52. L.Free;
  53. end;
  54. end;
  55. Procedure ConvertFile(const aFilename : string);
  56. Var
  57. S : TSettings;
  58. aPas : TStrings;
  59. FN,aLine : string;
  60. begin
  61. S:=GetSettings;
  62. aPas:=TStringList.Create;
  63. try
  64. if FileExists(aFileName) then
  65. FN:=ExtractRelativePath(S.BaseDir,aFilename)
  66. else
  67. FN:=aFileName;
  68. cgUtils.ConvertFile(S.BaseDir,FN,'','','',False,[],aPas,Nil);
  69. for aLine in aPas do
  70. writeln(aLine);
  71. Finally
  72. aPas.Free;
  73. end;
  74. end;
  75. procedure DoList(ARequest: TRequest; AResponse: TResponse);
  76. Var
  77. S : TSettings;
  78. aList : TStrings;
  79. isRaw : Boolean;
  80. begin
  81. S:=GetSettings;
  82. aList:=TstringList.Create;
  83. try
  84. IsRaw:=GetBoolVal(aRequest,'raw');
  85. if Not (FileExists(S.cachefile) and FileExists(S.rawcachefile)) then
  86. CreateJSONFileList(S.BaseDir,S.cachefile,S.rawcachefile);
  87. if isRaw then
  88. aList.LoadFromFile(S.rawcachefile)
  89. else
  90. aList.LoadFromFile(S.cachefile);
  91. aResponse.Content:=aList.text;
  92. aResponse.ContentLength:=Length(aResponse.Content);
  93. if IsRaw then
  94. aResponse.ContentType:='text/text'
  95. else
  96. aResponse.ContentType:='application/javascript';
  97. aResponse.SendResponse;
  98. finally
  99. aList.Free;
  100. end;
  101. end;
  102. function GetRequestOptions(ARequest: TRequest) : TConversionOptions;
  103. Var
  104. T : TConversionOption;
  105. N : String;
  106. begin
  107. Result:=[];
  108. For T in TConversionOption do
  109. begin
  110. N:=GetEnumName(TypeInfo(TConversionOption),Ord(T));
  111. if GetBoolVal(aRequest,N) then
  112. Include(Result,T);
  113. end;
  114. end;
  115. procedure DoConvertFile(ARequest: TRequest; AResponse: TResponse);
  116. Var
  117. S : TSettings;
  118. aPas,aLog : TStrings;
  119. aliases,aExtraUnits,aFileName,aUnitName,aOutput : string;
  120. Opts : TConversionOptions;
  121. skipweb : boolean;
  122. begin
  123. S:=GetSettings;
  124. aLog:=Nil;
  125. aPas:=TStringList.Create;
  126. try
  127. Opts:=GetRequestOptions(aRequest);
  128. aFileName:=aRequest.QueryFields.Values['file'];
  129. aUnitName:=aRequest.QueryFields.Values['unit'];
  130. aExtraUnits:=aRequest.QueryFields.Values['extraunits'];
  131. aliases:=aRequest.QueryFields.Values['aliases'];
  132. skipweb:=GetBoolVal(aRequest,'skipweb');
  133. if GetBoolVal(aRequest,'prependlog') then
  134. aLog:=TStringList.Create;
  135. cgUtils.ConvertFile(S.BaseDir,aFileName,aUnitName,aliases,aExtraUnits,skipweb,Opts,aPas,aLog);
  136. if Assigned(aLog) then
  137. aOutput:='(* // Conversion log:'+sLineBreak+aLog.Text+sLineBreak+'*)'+sLineBreak
  138. else
  139. aOutput:='';
  140. aOutput:=aOutput+aPas.text;
  141. aResponse.Content:=aOutput;
  142. aResponse.ContentLength:=Length(aResponse.Content);
  143. aResponse.ContentType:='text/x-pascal';
  144. aResponse.SendResponse;
  145. Finally
  146. aPas.Free;
  147. aLog.Free;
  148. end;
  149. end;
  150. begin
  151. if GetEnvironmentVariable('REQUEST_METHOD')='' then
  152. begin
  153. if ParamCount=2 then
  154. CreateJSONFileList(Paramstr(1),ParamStr(2))
  155. else if ParamCount=3 then
  156. CreateJSONFileList(Paramstr(1),ParamStr(2),ParamStr(3))
  157. else if ParamCount=1 then
  158. ConvertFile(Paramstr(1));
  159. end
  160. else
  161. begin
  162. HTTPRouter.RegisterRoute('list',rmGet,@DoList);
  163. HTTPRouter.RegisterRoute('convert',rmAll,@DoConvertFile);
  164. {$IFDEF USEHTTPAPP}
  165. Application.Port:=8080;
  166. {$ENDIF}
  167. Application.Title:='Typescript to pascal converter';
  168. Application.Initialize;
  169. Application.Run;
  170. end
  171. end.