cgutils.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. unit cgutils;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, pascodegen, tstopas;
  6. Type
  7. TSettings = record
  8. basedir : string;
  9. cachefile : string;
  10. end;
  11. { TLoggingConverter }
  12. TLoggingConverter = Class(TTypescriptToPas)
  13. private
  14. FLogs: TStrings;
  15. procedure DoMyLog(Sender: TObject; LogType: TCodegenLogType; const Msg: String);
  16. Public
  17. Constructor Create(Aowner: TComponent); override;
  18. Property Logs : TStrings Read FLogs Write Flogs;
  19. end;
  20. Procedure GetDeclarationFileNames(const BaseDir,aDir : String; aList: TStrings);
  21. Procedure ConvertFile(Const BaseDir,aFileName,aUnitName : String; aOptions : TConversionOptions; aPascal,aLog : TStrings);
  22. Function GetOutputUnitName(Const aFileName,aUnitName : String) : string;
  23. Function GetInputFileName(Const BaseDir,aFileName : String) : string;
  24. Function GetSettings : TSettings;
  25. implementation
  26. uses inifiles;
  27. Function GetSettings : TSettings;
  28. begin
  29. Result.BaseDir:=ExtractFilePath(ParamStr(0));
  30. Result.CacheFile:=GetTempDir(True)+'definitelytypedcache.lst';
  31. With TIniFile.Create(GetAppConfigFile(True)) do
  32. try
  33. Result.BaseDir:=ReadString('Settings','BaseDir',Result.BaseDir);
  34. Result.cachefile:=ReadString('Settings','CacheDir',Result.CacheFile);
  35. finally
  36. Free;
  37. end;
  38. end;
  39. Procedure GetDeclarationFileNames(Const BaseDir,aDir : String; aList: TStrings);
  40. Var
  41. Info : TSearchRec;
  42. D,FN : string;
  43. begin
  44. D:=IncludeTrailingPathDelimiter(aDir);
  45. if FindFirst(D+'*.d.ts',0,Info)=0 then
  46. try
  47. Repeat
  48. FN:=Info.Name;
  49. if (FN<>'.') and (FN<>'..') then
  50. Alist.Add(ExtractRelativePath(BaseDir,D+FN));
  51. until FindNext(Info)<>0;
  52. finally
  53. FindClose(Info);
  54. end;
  55. if FindFirst(D+AllFilesMask,faDirectory,Info)=0 then
  56. try
  57. Repeat
  58. if (Info.Attr and faDirectory)<>0 then
  59. begin
  60. FN:=Info.Name;
  61. if (FN<>'.') and (FN<>'..') then
  62. GetDeclarationFileNames(BaseDir,D+FN,aList);
  63. end;
  64. until FindNext(Info)<>0;
  65. finally
  66. FindClose(Info);
  67. end;
  68. end;
  69. Function GetInputFileName(Const BaseDir,aFileName : String) : string;
  70. Var
  71. BD,FN : String;
  72. begin
  73. BD:=IncludeTrailingPathDelimiter(BaseDir);
  74. FN:=BD+aFileName;
  75. if Not FileExists(FN) then
  76. if Not DirectoryExists(BD+aFileName) then
  77. FN:=''
  78. else
  79. begin
  80. FN:=IncludeTrailingPathDelimiter(BD+aFileName)+'index.d.ts';
  81. if not FileExists(FN) then
  82. Fn:='';
  83. end;
  84. if FN='' then
  85. Raise ETSToPas.Create('No such file: '+aFileName);
  86. Result:=FN;
  87. end;
  88. Function GetOutputUnitName(Const aFileName,aUnitName : String) : string;
  89. Var
  90. UN : String;
  91. begin
  92. UN:=aUnitName;
  93. if aUnitName='' then
  94. begin
  95. UN:=ChangeFileExt(ChangeFileExt(ExtractFileName(aFilename),''),'');
  96. if UN='index' then
  97. UN:=ExtractFileName(ExcludeTrailingPathDelimiter(ExtractFilePath(aFileName)));
  98. end;
  99. Result:=UN;
  100. end;
  101. Procedure ConvertFile(Const BaseDir,aFileName,aUnitName : String; aOptions : TConversionOptions; aPascal,aLog : TStrings);
  102. Var
  103. L : TLoggingConverter;
  104. UN,Fn : String;
  105. begin
  106. FN:=GetInputFileName(BaseDir,aFileName);
  107. UN:=GetOutputUnitName(FN,aUnitName);
  108. L:=TLoggingConverter.Create(Nil);
  109. try
  110. L.Options:=aOptions;
  111. L.InputFileName:=FN;
  112. L.OutputUnitName:=UN;
  113. L.Logs:=aLog;
  114. L.Execute;
  115. aPascal.Assign(L.Source);
  116. finally
  117. L.Free;
  118. end;
  119. end;
  120. { TLoggingConverter }
  121. procedure TLoggingConverter.DoMyLog(Sender: TObject; LogType: TCodegenLogType; const Msg: String);
  122. Var
  123. S : String;
  124. begin
  125. Str(LogType,S);
  126. If Assigned(Flogs) then
  127. Flogs.Add('['+S+']: '+Msg);
  128. end;
  129. constructor TLoggingConverter.Create(Aowner: TComponent);
  130. begin
  131. inherited Create(Aowner);
  132. OnLog:=@DoMyLog;
  133. end;
  134. end.