2
0

convcgi.lpr 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Procedure CreateJSONFileList(aDir : String; aFileName : string);
  19. Var
  20. L,O : TStrings;
  21. I : integer;
  22. S : String;
  23. begin
  24. O:=Nil;
  25. L:=TStringList.Create;
  26. try
  27. O:=TstringList.Create;
  28. GetDeclarationFileNames(aDir,aDir,L);
  29. TstringList(l).Sort;
  30. O.Add('var dtsfiles = [');
  31. for I:=0 to L.Count-1 do
  32. begin
  33. S:=L[i];
  34. S:=''''+StringReplace(S,'''','''''',[rfReplaceAll])+'''';
  35. if I<L.Count-1 then
  36. S:=S+',';
  37. O.Add(' '+S);
  38. end;
  39. O.Add(' ];');
  40. O.SaveToFile(aFileName);
  41. finally
  42. O.Free;
  43. L.Free;
  44. end;
  45. end;
  46. Procedure ConvertFile(const aFilename : string);
  47. Var
  48. S : TSettings;
  49. aPas : TStrings;
  50. FN,aLine : string;
  51. begin
  52. S:=GetSettings;
  53. aPas:=TStringList.Create;
  54. try
  55. if FileExists(aFileName) then
  56. FN:=ExtractRelativePath(S.BaseDir,aFilename)
  57. else
  58. FN:=aFileName;
  59. cgUtils.ConvertFile(S.BaseDir,FN,'',[],aPas,Nil);
  60. for aLine in aPas do
  61. writeln(aLine);
  62. Finally
  63. aPas.Free;
  64. end;
  65. end;
  66. procedure DoList(ARequest: TRequest; AResponse: TResponse);
  67. Var
  68. S : TSettings;
  69. aList : TStrings;
  70. begin
  71. S:=GetSettings;
  72. aList:=TstringList.Create;
  73. try
  74. if Not FileExists(S.cachefile) then
  75. CreateJSONFileList(S.BaseDir,S.cachefile);
  76. aList.LoadFromFile(S.cachefile);
  77. aResponse.Content:=aList.text;
  78. aResponse.ContentLength:=Length(aResponse.Content);
  79. aResponse.ContentType:='application/javascript';
  80. aResponse.SendResponse;
  81. finally
  82. aList.Free;
  83. end;
  84. end;
  85. function GetRequestOptions(ARequest: TRequest) : TConversionOptions;
  86. Var
  87. T : TConversionOption;
  88. S,N : String;
  89. begin
  90. Result:=[];
  91. For T in TConversionOption do
  92. begin
  93. N:=GetEnumName(TypeInfo(TConversionOption),Ord(T));
  94. S:=aRequest.QueryFields.Values[N];
  95. if (S='1') or (S='true') then
  96. Include(Result,T);
  97. end;
  98. end;
  99. procedure DoConvertFile(ARequest: TRequest; AResponse: TResponse);
  100. Var
  101. S : TSettings;
  102. aPas,aLog : TStrings;
  103. aFileName,aUnitName,aOutput : string;
  104. Opts : TConversionOptions;
  105. begin
  106. S:=GetSettings;
  107. aLog:=Nil;
  108. aPas:=TStringList.Create;
  109. try
  110. Opts:=GetRequestOptions(aRequest);
  111. aFileName:=aRequest.QueryFields.Values['file'];
  112. aUnitName:=aRequest.QueryFields.Values['unit'];
  113. if aRequest.QueryFields.Values['prependlog']='1' then
  114. aLog:=TStringList.Create;
  115. cgUtils.ConvertFile(S.BaseDir,aFileName,aUnitName,Opts,aPas,aLog);
  116. if Assigned(aLog) then
  117. aOutput:='(* // Conversion log:'+sLineBreak+aLog.Text+sLineBreak+'*)'+sLineBreak
  118. else
  119. aOutput:='';
  120. aOutput:=aOutput+aPas.text;
  121. aResponse.Content:=aOutput;
  122. aResponse.ContentLength:=Length(aResponse.Content);
  123. aResponse.ContentType:='text/x-pascal';
  124. aResponse.SendResponse;
  125. Finally
  126. aPas.Free;
  127. aLog.Free;
  128. end;
  129. end;
  130. begin
  131. if GetEnvironmentVariable('REQUEST_METHOD')='' then
  132. begin
  133. if ParamCount=2 then
  134. CreateJSONFileList(Paramstr(1),ParamStr(2))
  135. else if ParamCount=1 then
  136. ConvertFile(Paramstr(1));
  137. end
  138. else
  139. begin
  140. HTTPRouter.RegisterRoute('list',rmGet,@DoList);
  141. HTTPRouter.RegisterRoute('convert',rmAll,@DoConvertFile);
  142. {$IFDEF USEHTTPAPP}
  143. Application.Port:=8080;
  144. {$ENDIF}
  145. Application.Title:='Typescript to pascal converter';
  146. Application.Initialize;
  147. Application.Run;
  148. end
  149. end.